Browse Source

hannlp试做

gaodm 4 years atrás
parent
commit
4536ba0dae

+ 27 - 20
src/main/java/com/diagbot/config/CacheDeleteInit.java

@@ -1,6 +1,7 @@
 package com.diagbot.config;
 
 import com.diagbot.facade.CacheFacade;
+import com.diagbot.facade.ConceptInfoFacade;
 import com.diagbot.facade.NeoFacade;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -22,30 +23,36 @@ public class CacheDeleteInit implements CommandLineRunner {
     CacheFacade cacheFacade;
     @Autowired
     NeoFacade neoFacade;
+    @Autowired
+    private ConceptInfoFacade conceptInfoFacade;
+
 
     @Override
-    public void run(String... args) throws Exception {
+    public void run(String... args) {
         // 服务启动清除redis缓存
-        cacheFacade.clear();
-        log.info("CDSS-CORE服务启动清除redis缓存成功!");
-
-        cacheFacade.loadDrugTypeCache();
-        log.info("CDSS-CORE服务启动加载药品类型对应关系缓存成功!");
-
-        cacheFacade.getDiseaseCorrespondCache();
-        log.info("CDSS-CORE服务启动加载疾病对应ICD10缓存成功!");
-
-        cacheFacade.getdiseaseFilterCache();
-        log.info("CDSS-CORE服务启动加载疾病过滤缓存成功!");
-
-         cacheFacade.loadAllRuleCache();
-         log.info("CDSS-CORE服务启动加载医学知识库中的所有规则缓存成功!");
-
-        cacheFacade.loadAllBaseDiagnoseCache();
-        log.info("CDSS-CORE服务启动加载诊断依据缓存成功!");
+//        cacheFacade.clear();
+//        log.info("CDSS-CORE服务启动清除redis缓存成功!");
+//
+//        cacheFacade.loadDrugTypeCache();
+//        log.info("CDSS-CORE服务启动加载药品类型对应关系缓存成功!");
+//
+//        cacheFacade.getDiseaseCorrespondCache();
+//        log.info("CDSS-CORE服务启动加载疾病对应ICD10缓存成功!");
+//
+//        cacheFacade.getdiseaseFilterCache();
+//        log.info("CDSS-CORE服务启动加载疾病过滤缓存成功!");
+//
+//         cacheFacade.loadAllRuleCache();
+//         log.info("CDSS-CORE服务启动加载医学知识库中的所有规则缓存成功!");
+//
+//        cacheFacade.loadAllBaseDiagnoseCache();
+//        log.info("CDSS-CORE服务启动加载诊断依据缓存成功!");
+//
+//        cacheFacade.loadHostipalPush();
+//        log.info("CDSS-CORE服务启动加载医院大数据推送类型成功!");
 
-        cacheFacade.loadHostipalPush();
-        log.info("CDSS-CORE服务启动加载医院大数据推送类型成功!");
+        conceptInfoFacade.loadCustomDictionary();
+        log.info("CDSS-CORE服务启动加载NLP分词字典成功!");
 
 //        cacheFacade.getDiseaseCorrespondCache();
 //        log.info("CDSS-CORE服务启动加载疾病对应ICD10缓存成功!");

+ 16 - 0
src/main/java/com/diagbot/dto/CustomDictionaryDTO.java

@@ -0,0 +1,16 @@
+package com.diagbot.dto;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @time: 2021/5/25 20:03
+ */
+@Getter
+@Setter
+public class CustomDictionaryDTO {
+    private String name;
+    private String type;
+}

+ 12 - 0
src/main/java/com/diagbot/facade/ConceptInfoFacade.java

@@ -1,6 +1,7 @@
 package com.diagbot.facade;
 
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.diagbot.dto.CustomDictionaryDTO;
 import com.diagbot.dto.ReverseDTO;
 import com.diagbot.entity.ConceptInfo;
 import com.diagbot.enums.IsDeleteEnum;
@@ -8,6 +9,7 @@ import com.diagbot.service.impl.ConceptInfoServiceImpl;
 import com.diagbot.util.ListUtil;
 import com.diagbot.vo.ReverseVO;
 import com.google.common.collect.Lists;
+import com.hankcs.hanlp.dictionary.CustomDictionary;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
@@ -49,4 +51,14 @@ public class ConceptInfoFacade extends ConceptInfoServiceImpl {
         }
         return Lists.newArrayList();
     }
+
+    public void loadCustomDictionary(){
+        CustomDictionary.reload();
+        List<CustomDictionaryDTO> words = this.getAllWord();
+        if (ListUtil.isNotEmpty(words)){
+            for (CustomDictionaryDTO dictionaryDTO: words){
+                CustomDictionary.insert(dictionaryDTO.getName().trim().replaceAll(" ",""), dictionaryDTO.getType());
+            }
+        }
+    }
 }

+ 17 - 0
src/main/java/com/diagbot/facade/NLPFacade.java

@@ -0,0 +1,17 @@
+package com.diagbot.facade;
+
+import com.hankcs.hanlp.HanLP;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @time: 2021/5/25 20:13
+ */
+@Component
+public class NLPFacade {
+
+    public String segment(String text) {
+        return HanLP.segment(text).toString();
+    }
+}

+ 4 - 1
src/main/java/com/diagbot/mapper/ConceptInfoMapper.java

@@ -1,6 +1,7 @@
 package com.diagbot.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.diagbot.dto.CustomDictionaryDTO;
 import com.diagbot.dto.ReverseDTO;
 import com.diagbot.entity.ConceptInfo;
 import com.diagbot.vo.ReverseVO;
@@ -17,5 +18,7 @@ import java.util.List;
  */
 public interface ConceptInfoMapper extends BaseMapper<ConceptInfo> {
 
-    public List<ReverseDTO> getReverse(ReverseVO reverseVO);
+    List<ReverseDTO> getReverse(ReverseVO reverseVO);
+
+    List<CustomDictionaryDTO> getAllWord();
 }

+ 4 - 1
src/main/java/com/diagbot/service/ConceptInfoService.java

@@ -1,6 +1,7 @@
 package com.diagbot.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.diagbot.dto.CustomDictionaryDTO;
 import com.diagbot.dto.ReverseDTO;
 import com.diagbot.entity.ConceptInfo;
 import com.diagbot.vo.ReverseVO;
@@ -17,5 +18,7 @@ import java.util.List;
  */
 public interface ConceptInfoService extends IService<ConceptInfo> {
 
-    public List<ReverseDTO> getReverse(ReverseVO reverseVO);
+    List<ReverseDTO> getReverse(ReverseVO reverseVO);
+
+    List<CustomDictionaryDTO> getAllWord();
 }

+ 6 - 0
src/main/java/com/diagbot/service/impl/ConceptInfoServiceImpl.java

@@ -1,6 +1,7 @@
 package com.diagbot.service.impl;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.diagbot.dto.CustomDictionaryDTO;
 import com.diagbot.dto.ReverseDTO;
 import com.diagbot.entity.ConceptInfo;
 import com.diagbot.mapper.ConceptInfoMapper;
@@ -24,4 +25,9 @@ public class ConceptInfoServiceImpl extends ServiceImpl<ConceptInfoMapper, Conce
     public List<ReverseDTO> getReverse(ReverseVO reverseVO) {
         return baseMapper.getReverse(reverseVO);
     }
+
+    @Override
+    public List<CustomDictionaryDTO> getAllWord(){
+        return baseMapper.getAllWord();
+    }
 }

+ 0 - 25
src/main/java/com/diagbot/util/NLPUtil.java

@@ -1,25 +0,0 @@
-package com.diagbot.util;
-
-import com.hankcs.hanlp.tokenizer.SpeedTokenizer;
-
-/**
- * @Description: NLP工具类
- * @author: gaodm
- * @time: 2021/5/25 17:13
- */
-public class NLPUtil {
-
-    public static void main(String[] args)
-    {
-        String text = "江西鄱阳湖干枯,中国最大淡水湖变成大草原";
-        System.out.println(SpeedTokenizer.segment(text));
-//        long start = System.currentTimeMillis();
-//        int pressure = 1000000;
-//        for (int i = 0; i < pressure; ++i)
-//        {
-//            SpeedTokenizer.segment(text);
-//        }
-//        double costTime = (System.currentTimeMillis() - start) / (double)1000;
-//        System.out.printf("分词速度:%.2f字每秒", text.length() * pressure / costTime);
-    }
-}

+ 14 - 0
src/main/java/com/diagbot/web/CacheController.java

@@ -3,6 +3,8 @@ package com.diagbot.web;
 import com.diagbot.annotation.SysLogger;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.facade.CacheFacade;
+import com.diagbot.facade.ConceptInfoFacade;
+import com.hankcs.hanlp.dictionary.CustomDictionary;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
@@ -28,6 +30,8 @@ public class CacheController {
 
     @Autowired
     CacheFacade cacheFacade;
+    @Autowired
+    private ConceptInfoFacade conceptInfoFacade;
 
     @ApiOperation(value = "清除启动加载类缓存[by:zhoutg]",
             notes = "")
@@ -40,6 +44,7 @@ public class CacheController {
         cacheFacade.getdiseaseFilterCache();
         cacheFacade.loadAllRuleCache();
         cacheFacade.loadAllBaseDiagnoseCache();
+        conceptInfoFacade.loadCustomDictionary();
         //无用的缓存
 //        cacheFacade.loadDiseaseTypeCache();
 //        cacheFacade.getSymptomCache();
@@ -127,4 +132,13 @@ public class CacheController {
         cacheFacade.loadHostipalPush();
         return RespDTO.onSuc(true);
     }
+
+    @ApiOperation(value = "重新加载NLP分词字典[by:gaodm]",
+            notes = "")
+    @PostMapping("/reloadCustomDictionary")
+    @SysLogger("reloadCustomDictionary")
+    public RespDTO<Boolean> reloadCustomDictionary() {
+        conceptInfoFacade.loadCustomDictionary();
+        return RespDTO.onSuc(true);
+    }
 }

+ 36 - 0
src/main/java/com/diagbot/web/NLPController.java

@@ -0,0 +1,36 @@
+package com.diagbot.web;
+
+import com.diagbot.annotation.SysLogger;
+import com.diagbot.dto.RespDTO;
+import com.diagbot.facade.NLPFacade;
+import com.hankcs.hanlp.seg.common.Term;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @time: 2021/5/25 20:09
+ */
+@RequestMapping("/nlp")
+@RestController
+@SuppressWarnings("unchecked")
+@Api(value = "NLP API", tags = { "NLP API" })
+public class NLPController {
+    @Autowired
+    private NLPFacade nlpFacade;
+
+    @ApiOperation(value = "NLP分词[by:gaodm]",
+            notes = "")
+    @PostMapping("/segment")
+    @SysLogger("segment")
+    public RespDTO<String> segment(String text){
+        return RespDTO.onSuc(nlpFacade.segment(text));
+    }
+}

+ 17 - 0
src/main/resources/mapper/ConceptInfoMapper.xml

@@ -72,4 +72,21 @@
         t.order_no
     </select>
 
+    <select id="getAllWord" resultType="com.diagbot.dto.CustomDictionaryDTO">
+        SELECT
+            t1.`name` AS `name`,
+            CONCAT_WS(" ", t1.type_id, (1200 - t1.type_id)) AS type
+        FROM
+            `kl_library_info` t1,
+            kl_concept t2
+        WHERE
+            t1.concept_id = t2.id
+        AND t1.is_deleted = "N"
+        AND t2.is_deleted = "N"
+        AND t2.`status` = 1
+        AND t1.type_id IN (103, 122, 126, 129)
+        ORDER BY
+            t1.type_id;
+    </select>
+
 </mapper>