Browse Source

更新分词显示效果

MarkHuang 6 years ago
parent
commit
f0fdd7e5c2

+ 5 - 2
nlp-web/src/main/java/org/diagbot/nlp/controller/RelationExtractionController.java

@@ -3,6 +3,7 @@ package org.diagbot.nlp.controller;
 import org.diagbot.nlp.dao.model.TaggingResult;
 import org.diagbot.nlp.feature.FeatureType;
 import org.diagbot.nlp.relation.analyze.RelationAnalyze;
+import org.diagbot.nlp.relation.analyze.StructureAnalyze;
 import org.diagbot.nlp.relation.util.OutputInfo;
 import org.diagbot.pub.api.Response;
 import org.diagbot.pub.jdbc.MysqlJdbc;
@@ -30,8 +31,10 @@ public class RelationExtractionController {
     public Response extraction(String content) throws Exception {
         Response response = new Response();
         response.start();
-        RelationAnalyze relationAnalyze = new RelationAnalyze();
-        List<OutputInfo> outputInfos = relationAnalyze.analyze(content, FeatureType.parse("1"));
+//        RelationAnalyze relationAnalyze = new RelationAnalyze();
+//        List<OutputInfo> outputInfos = relationAnalyze.analyze(content, FeatureType.parse("1"));
+        StructureAnalyze structureAnalyze = new StructureAnalyze();
+        List<OutputInfo> outputInfos = structureAnalyze.extract(content);
         response.setData(outputInfos);
         return response;
     }

+ 411 - 0
nlp/src/main/java/org/diagbot/nlp/relation/analyze/StructureAnalyze.java

@@ -0,0 +1,411 @@
+package org.diagbot.nlp.relation.analyze;
+
+import org.algorithm.core.cnn.AlgorithmCNNExecutor;
+import org.algorithm.core.cnn.entity.Lemma;
+import org.algorithm.core.cnn.entity.Triad;
+import org.algorithm.core.cnn.model.impl.RelationExtractionModelImpl;
+import org.diagbot.nlp.feature.FeatureType;
+import org.diagbot.nlp.participle.word.Lexeme;
+import org.diagbot.nlp.participle.word.LexemePath;
+import org.diagbot.nlp.relation.extract.*;
+import org.diagbot.nlp.relation.module.*;
+import org.diagbot.nlp.relation.module.cell.*;
+import org.diagbot.nlp.relation.util.LemmaUtil;
+import org.diagbot.nlp.relation.util.OutputInfo;
+import org.diagbot.nlp.util.Constants;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.diagbot.nlp.participle.ParticipleUtil.participle;
+
+/**
+ * @ClassName org.diagbot.nlp.relation.StructureAnalyze
+ * @Description 结构分析入口
+ * @Author Mark Huang
+ * @Date 2019/4/30 9:07
+ * @Version 1.0
+ **/
+public class StructureAnalyze {
+
+    private List<OutputInfo> outputInfos = new ArrayList<>();
+//    private static OutputInfo outputInfo = null;
+    private static boolean updated = false;
+    private static Object current = null;
+    private static String num = "";
+
+    /**
+     * 分词,提取词性,封装
+     */
+    public List<OutputInfo> extract(String content) {
+
+        Lexeme lexeme = null;
+        String timestamp = "";
+        String[] tokens = content.split(",|。|;");
+
+        try {
+//            outputInfos.add(initOutputInfo());
+            for (String token:tokens) {
+                updated = false;
+                outputInfos.add(initOutputInfo());
+                OutputInfo outputInfo = outputInfos.get(outputInfos.size()-1);
+                System.out.println(token);
+                LexemePath<Lexeme> lexemePath = participle(token, false);
+                for (int i = 0; i < lexemePath.size(); i++) {
+                    lexeme = lexemePath.get(i);
+                    updateLexeme(lexeme);
+                    updateInfo(outputInfo, lexeme);
+                }
+//                outputInfo.getSymptoms().get(outputInfo.getSymptoms().size()-1).setSymptomName(token);
+
+                Symptom symptom = outputInfos.get(outputInfos.size() - 1).getSymptoms().get(0);
+//                if (symptom.getNegative() != null && symptom.getSymptomName() == "") {
+//                    outputInfos.get(outputInfos.size() - 1).setSymptoms(new ArrayList<>());
+//                }
+                if (updated == false) {
+                    outputInfos.remove(outputInfos.size() - 1);
+                }
+            }
+        }
+        catch (IOException ioe) {
+            ioe.printStackTrace();
+        }
+        finally {
+            current = null;
+            return outputInfos;
+        }
+    }
+
+    public void updateLexeme(Lexeme lexeme) {
+        String prop = lexeme.getProperty();
+        String[] lexprop = lexeme.getProperty().split(",");
+
+        if (lexprop.length > 1) {
+            prop = lexprop[0];
+            for (int i=0;i<lexprop.length;i++) {
+                if (Integer.parseInt(lexprop[i]) < Integer.parseInt(prop)) {
+                    prop = lexprop[i];
+                }
+            }
+        }
+        lexeme.setProperty(prop);
+    }
+
+    public void updateInfo(OutputInfo outputInfo, Lexeme lexeme) {
+        String con = "伴";
+        Symptom symptom = outputInfo.getSymptoms().get(outputInfo.getSymptoms().size() - 1);;
+        Lis lis = null;
+        Pacs pacs = null;
+        Vital vital = null;
+        Treat treat = null;
+        PD pd1 = null;
+
+        try {
+
+            switch (lexeme.getProperty()) {
+                case Constants.word_property_cause:
+                    Cause cause = new Cause();
+                    cause.setCauseName(lexeme.getText());
+                    if (current instanceof Symptom) {
+                        symptom = (Symptom)current;
+                    }
+                    symptom.setCause(cause);
+                    current = symptom;
+                    updated = true;
+                    break;
+                case Constants.word_property_symptom:
+                case Constants.word_property_vital_idx:
+                    current = updateClinicalInfo(outputInfo, lexeme.getText(), lexeme.getProperty());
+                    updated = true;
+                    break;
+                case Constants.word_property_prop:
+                    Property property = new Property();
+                    property.setPropertyName(lexeme.getText());
+                    if (symptom != null) {
+                        symptom.setProperty(property);
+                    }
+                    updated = true;
+                    break;
+                case Constants.word_property_degree:
+                    Degree degree = new Degree();
+                    degree.setDegreeName(lexeme.getText());
+                    if (current instanceof Symptom) {
+                        ((Symptom)current).setDegree(degree);
+                    }
+                    updated = true;
+                    break;
+                case Constants.word_property_LIS:
+                case Constants.word_property_LIS_Detail:
+                    lis = new Lis();
+                    lis.setLisName(lexeme.getText());
+                    outputInfo.getLises().add(lis);
+                    current = lis;
+                    updated = true;
+                    break;
+                case Constants.word_property_LIS_Result:
+                    if (current instanceof Lis) {
+                        if (((Lis)current).getPd() == null) {
+                            ((Lis)current).setPd(new PD());
+                            pd1 = ((Lis)current).getPd();
+                        }
+                        pd1.setValue(lexeme.getText());
+                        pd1.setUnit("");
+                        updated = true;
+                    }
+                    break;
+                case Constants.word_property_PACS:
+                    pacs = new Pacs();
+                    pacs.setPacsName(lexeme.getText());
+                    outputInfo.getPacses().add(pacs);
+                    current = pacs;
+                    updated = true;
+                    break;
+                case Constants.word_property_PACS_Result:
+                    if (current instanceof Pacs) {
+                        if (((Pacs) current).getValue() == null) {
+                            ((Pacs) current).setValue("");
+                        }
+                        ((Pacs) current).setValue(((Pacs) current).getValue() + lexeme.getText() + ", ");
+                        updated = true;
+                    }
+                    break;
+                case Constants.word_property_bodypart:
+                case Constants.word_property_direction:
+                    if (current instanceof Pacs) {
+                        if (((Pacs)current).getBodypart()==null) {
+                            ((Pacs)current).setBodypart("");
+                        }
+                        ((Pacs) current).setBodypart(((Pacs)current).getBodypart()+lexeme.getText() + ", ");
+                        updated = true;
+                    }
+                    break;
+                case Constants.word_property_neg:
+                    Negative negative = new Negative();
+                    negative.setNegaName(lexeme.getText());
+                    symptom.setNegative(negative);
+                    updated = true;
+                    break;
+                case Constants.word_property_timestamp:
+                    PD pd = new PD();
+                    pd.setValue(lexeme.getText());
+                    pd.setUnit("");
+                    List<PD> pds = new ArrayList<>();
+                    pds.add(pd);
+                    if (current instanceof Symptom) {
+                        symptom = (Symptom)current;
+                    }
+                    if (symptom != null) {
+                        symptom.setTimestamp(pds);
+                        updated = true;
+                        current = symptom;
+                    }
+                    break;
+                case Constants.word_property_freq:
+                case Constants.word_property_number:
+                    num = lexeme.getText();
+
+                    updated = true;
+
+                    break;
+                case Constants.word_property_unit:
+                    if (num.length() > 0) {
+                        if (current instanceof Symptom) {
+                            symptom = (Symptom) current;
+                            if (symptom.getPds() == null) {
+                                List<PD> pds1 = new ArrayList<>();
+                                pds1.add(new PD());
+                                symptom.setPds(pds1);
+                            }
+                            pd1 = symptom.getPds().get(0);
+                        } else if (current instanceof Vital) {
+                            vital = (Vital) current;
+                            if (vital.getPd() == null) {
+                                vital.setPd(new PD());
+                            }
+                            pd1 = vital.getPd();
+                        } else if (current instanceof Treat) {
+                            treat = (Treat)current;
+                            if (treat.getPds() == null) {
+                                List<PD> pds1 = new ArrayList<>();
+                                pds1.add(new PD());
+                                treat.setPds(pds1);
+                            }
+                            pd1 = treat.getPds().get(0);
+                        } else if (current instanceof Lis) {
+                            lis = (Lis)current;
+                            if (lis.getPd() == null) {
+                                lis.setPd(new PD());
+                            }
+                            pd1 = lis.getPd();
+                        }
+                    }
+                    pd1.setValue(num);
+                    pd1.setUnit(lexeme.getText());
+                    updated = true;
+                    num = "";
+                    break;
+                case Constants.word_property_time:
+                    if (num.length() > 0) {
+                        if (symptom.getTimestamp() == null) {
+                            List<PD> pds1 = new ArrayList<>();
+                            pds1.add(new PD());
+                            symptom.setTimestamp(pds1);
+                        }
+                        pd1 = symptom.getTimestamp().get(0);
+                        pd1.setValue(num);
+                        pd1.setUnit(lexeme.getText());
+                        updated = true;
+                        num = "";
+                        current = symptom;
+                    }
+                    break;
+                case Constants.word_property_diagnose:
+                    current = updateClinicalInfo(outputInfo, lexeme.getText(), lexeme.getProperty());
+                    updated = true;
+                    break;
+                case Constants.word_property_med:
+                    if (!(current instanceof Treat)) {
+                        current = updateClinicalInfo(outputInfo, "", Constants.word_property_diagnose);
+                    }
+                    treat = (Treat) current;
+
+                    if (treat.getTreatName() == null || treat.getTreatName().trim().length() == 0) {
+                        treat.setTreatName(lexeme.getText());
+                    }
+                    else {
+                        treat.setTreatName(treat.getTreatName() + "," + lexeme.getText());
+                    }
+                    updated = true;
+                    break;
+                case Constants.word_property_treat:
+                    if (current instanceof Treat) {
+                        treat = (Treat)current;
+                        if (treat.getValue() == null || treat.getValue().trim().length() == 0) {
+                            treat.setValue(lexeme.getText());
+                        }
+                        else {
+                            treat.setValue(treat.getValue() + "," + lexeme.getText());
+                        }
+                        updated = true;
+                    }
+                    break;
+            }
+        }
+        catch (Exception ex) {
+            ex.printStackTrace();
+        }
+    }
+
+    public OutputInfo initOutputInfo() {
+        OutputInfo outputInfo = new OutputInfo();
+
+        outputInfo.setSymptoms(new ArrayList<>());
+        outputInfo.getSymptoms().add(new Symptom());
+        outputInfo.getSymptoms().get(outputInfo.getSymptoms().size()-1).setSymptomName("");
+
+        outputInfo.setVitals(new ArrayList<>());
+
+        outputInfo.setLises(new ArrayList<>());
+
+        outputInfo.setPacses(new ArrayList<>());
+
+        outputInfo.setTreats(new ArrayList<>());
+
+        return outputInfo;
+    }
+
+
+    public Object updateClinicalInfo(OutputInfo outputInfo, String name, String type) {
+
+        Object obj = null;
+
+        switch (type) {
+            case Constants.word_property_symptom:
+                Symptom symptom = outputInfo.getSymptoms().get(outputInfo.getSymptoms().size() - 1);
+
+                if (symptom.getSymptomName() == null || symptom.getSymptomName().trim().length() == 0) {
+                    symptom.setSymptomName(name);
+                }
+                else {
+                    symptom.setSymptomName(symptom.getSymptomName() + ", " + name);
+                }
+
+                obj = symptom;
+                break;
+            case Constants.word_property_vital_idx:
+                if (outputInfo.getVitals().size() == 0) {
+                    outputInfo.getVitals().add(new Vital());
+                }
+
+                Vital vital = outputInfo.getVitals().get(outputInfo.getVitals().size()-1);
+
+                if (vital.getVitalName() == null || vital.getVitalName().trim().length() == 0) {
+                    vital.setVitalName(name);
+                }
+                else {
+                    vital.setVitalName(vital.getVitalName() + "," + name);
+                }
+
+                obj = vital;
+                break;
+            case Constants.word_property_diagnose:
+                if (outputInfo.getTreats().size() == 0) {
+                    outputInfo.getTreats().add(new Treat());
+                }
+
+                Treat treat = outputInfo.getTreats().get(outputInfo.getTreats().size()-1);
+
+                if (treat.getDiagnose() == null || treat.getDiagnose().trim().length() == 0) {
+                    treat.setDiagnose(name);
+                }
+                else {
+                    treat.setDiagnose(treat.getDiagnose() + "," + name);
+                }
+                obj = treat;
+                break;
+        }
+        /*
+//        if (symptom.getSymptomName().trim().length() == 0) {
+            if (outputInfos.size() >= 2 ) {
+                if (symptom.getNegative() == null &&
+                        (outputInfos.get(outputInfos.size() - 2).getSymptoms().get(0).getSymptomName().length() > 0 ||
+                        outputInfos.get(outputInfos.size() - 2).getSymptoms().get(0).getNegative() != null)) {
+                    symptom = outputInfos.get(outputInfos.size() - 2).getSymptoms().get(0);
+                    if (symptom.getSymptomName().indexOf(name) == -1) {
+                        symptom.setSymptomName(symptom.getSymptomName() + ", " + name);
+                    }
+                    outputInfos.remove(outputInfos.get(outputInfos.size() - 1));
+                }
+                else if (symptom.getNegative() != null &&
+                        outputInfos.get(outputInfos.size() - 2).getSymptoms().get(0).getNegative() != null) {
+                    symptom = outputInfos.get(outputInfos.size() - 2).getSymptoms().get(0);
+                    if (symptom.getSymptomName().indexOf(name) == -1) {
+                        symptom.setSymptomName(symptom.getSymptomName() + ", " + name);
+                    }
+                    outputInfos.remove(outputInfos.get(outputInfos.size() - 1));
+                }
+                else {
+                    symptom.setSymptomName(name);
+                }
+            }
+            else {
+
+                if (symptom.getSymptomName().trim().length() == 0) {
+                    symptom.setSymptomName(name);
+                }
+                else {
+                    symptom.setSymptomName(symptom.getSymptomName() + ", " + name);
+                }
+
+            }
+//        }
+//        else {
+//            symptom.setSymptomName(symptom.getSymptomName() + ", " + name);
+//        }
+*/
+//        current = symptom;
+        return obj;
+    }
+
+}

+ 5 - 0
nlp/src/main/java/org/diagbot/nlp/relation/module/Pacs.java

@@ -10,6 +10,7 @@ package org.diagbot.nlp.relation.module;
 public class Pacs {
     private String pacsName;
     private String value;
+    private String bodypart;
 
     public String getPacsName() {
         return pacsName;
@@ -26,4 +27,8 @@ public class Pacs {
     public void setValue(String value) {
         this.value = value;
     }
+
+    public String getBodypart() { return bodypart; }
+
+    public void setBodypart(String bodypart) { this.bodypart = bodypart; }
 }

+ 6 - 0
nlp/src/main/java/org/diagbot/nlp/relation/module/Symptom.java

@@ -20,6 +20,7 @@ public class Symptom {
     private Cause cause;
     private Property property;
     private List<PD> pds;
+    private List<PD> timestamp;
 
     public String getSymptomName() {
         return symptomName;
@@ -76,4 +77,9 @@ public class Symptom {
     public void setPds(List<PD> pds) {
         this.pds = pds;
     }
+
+    public List<PD> getTimestamp() { return timestamp; }
+
+    public void setTimestamp(List<PD> timestamp) { this.timestamp = timestamp; }
+
 }

+ 14 - 0
nlp/src/main/java/org/diagbot/nlp/relation/module/Treat.java

@@ -1,5 +1,9 @@
 package org.diagbot.nlp.relation.module;
 
+import org.diagbot.nlp.relation.module.cell.PD;
+
+import java.util.List;
+
 /**
  * @ClassName org.diagbot.nlp.relation.module.Treat
  * @Description TODO
@@ -8,8 +12,14 @@ package org.diagbot.nlp.relation.module;
  * @Version 1.0
  **/
 public class Treat {
+    private String diagnose;
     private String treatName;
     private String value;
+    private List<PD> pds;
+
+    public String getDiagnose() { return diagnose; }
+
+    public void setDiagnose(String diagname) { this.diagnose = diagname; }
 
     public String getTreatName() {
         return treatName;
@@ -26,4 +36,8 @@ public class Treat {
     public void setValue(String value) {
         this.value = value;
     }
+
+    public List<PD> getPds() { return pds; }
+
+    public void setPds(List<PD> pds) { this.pds = pds; }
 }

+ 20 - 0
nlp/src/main/java/org/diagbot/nlp/util/Constants.java

@@ -46,4 +46,24 @@ public class Constants {
     public static String[] vital_filter_data = new String[] {
             "神清","神志清","睡眠可","精神可","精神佳","二便无殊","体重无明显减轻","睡眠一般","小便无殊","大便无殊","胃纳可","食欲可"
     };
+
+    public final static String word_property_timestamp = "74";
+    public final static String word_property_symptom = "1";
+    public final static String word_property_bodypart = "3";
+    public final static String word_property_prop = "4";
+    public final static String word_property_cause = "5";
+    public final static String word_property_degree = "6";
+    public final static String word_property_neg = "7";
+    public final static String word_property_med = "10";
+    public final static String word_property_treat = "11";
+    public final static String word_property_LIS = "13";
+    public final static String word_property_LIS_Detail = "14";
+    public final static String word_property_LIS_Result = "15";
+    public final static String word_property_PACS = "17";
+    public final static String word_property_PACS_Result = "18";
+    public final static String word_property_diagnose = "19";
+    public final static String word_property_direction = "21";
+    public final static String word_property_freq = "33";
+    public final static String word_property_vital_idx = "60";
+
 }

+ 38 - 8
push-web/src/main/resources/static/pages/relation/sample.html

@@ -144,7 +144,7 @@
                     <!-- /.box -->
                 </div>
 
-                <div class="col-md-12" style="height: 300px;overflow: auto">
+                <div class="col-md-12" style="height: 400px;overflow: auto">
                     <!-- TABLE: LATEST ORDERS -->
                     <div class="box box-info">
                         <div class="box-header with-border">
@@ -259,7 +259,8 @@
                                     <tr>
                                         <th width="20%">项目名称</th>
                                         <th width="20%">结果信息</th>
-                                        <th width="60%">其他</th>
+                                        <th width="20%">部位</th>
+                                        <th width="40%">其他</th>
                                     </tr>
                                     </thead>
                                     <tbody id="pacs_extract_id">
@@ -287,9 +288,10 @@
                                 <table class="table no-margin">
                                     <thead>
                                     <tr>
+                                        <th width="20%">诊断</th>
                                         <th width="20%">用药</th>
                                         <th width="20%">结果</th>
-                                        <th width="60%">其他</th>
+                                        <th width="40%">其他</th>
                                     </tr>
                                     </thead>
                                     <tbody id="treat_extract_id">
@@ -343,9 +345,9 @@
                                         h += symptom.symptomName + "</strong></td>"
 
                                         h += "<td>";
-                                        if (symptom.pds != null) {
-                                            $.each(symptom.pds, function (pd_index, pd) {
-                                                h += pd.value + pd.unit + " ";
+                                        if (symptom.timestamp != null) {
+                                            $.each(symptom.timestamp, function (ts_index, ts) {
+                                                h += ts.value + ts.unit + " ";
                                             });
                                         }
                                         h += "</td>";
@@ -380,7 +382,15 @@
                                                 h += symptom.bodyPart.partBodyName;
                                             }
                                         }
-                                        h += "</td></tr>";
+                                        h += "</td>";
+
+                                        h += "<td>";
+                                        if (symptom.pds != null) {
+                                            $.each(symptom.pds, function (pd_index, pd) {
+                                                h += pd.value + pd.unit + " ";
+                                            });
+                                        }
+                                        h += "</td>";
                                     });
                                 }
 
@@ -462,6 +472,12 @@
                                         if (pacs.value != null) {
                                             pacs_h += pacs.value;
                                         }
+                                        pacs_h += "</td>";
+
+                                        pacs_h += "<td>";
+                                        if (pacs.bodypart != null) {
+                                            pacs_h += pacs.bodypart;
+                                        }
                                         pacs_h += "</td><td></td></tr>";
                                     });
                                 }
@@ -470,6 +486,12 @@
                                 if (treats != null) {
                                     $.each(treats, function (treat_index, treat) {
                                         treat_h += "<tr><td>";
+                                        if (treat.diagnose != null) {
+                                            treat_h += treat.diagnose;
+                                        }
+                                        treat_h += "</td>";
+
+                                        treat_h += "<td>";
                                         if (treat.treatName != null) {
                                             treat_h += treat.treatName;
                                         }
@@ -479,7 +501,15 @@
                                         if (treat.value != null) {
                                             treat_h += treat.value;
                                         }
-                                        treat_h += "</td><td></td></tr>";
+                                        treat_h += "</td>";
+
+                                        treat_h += "<td>";
+                                        if (treat.pds != null) {
+                                            $.each(treat.pds, function (pd_index, pd) {
+                                                treat_h += pd.value + pd.unit + " ";
+                                            });
+                                        }
+                                        treat_h += "</td></tr>";
                                     });
                                 }
                             });