Преглед изворни кода

更新分词显示效果,更新对辅检的处理

MarkHuang пре 6 година
родитељ
комит
00d83ebdc3
1 измењених фајлова са 176 додато и 32 уклоњено
  1. 176 32
      nlp/src/main/java/org/diagbot/nlp/relation/analyze/StructureAnalyze.java

+ 176 - 32
nlp/src/main/java/org/diagbot/nlp/relation/analyze/StructureAnalyze.java

@@ -35,10 +35,14 @@ public class StructureAnalyze {
     private static boolean updated = false;
     private static Object current = null;
     private static Object current_treat = null;
+    private static String current_diag = "";
     private static BodyPart bodyPart = null;
     private static String num = "";
-    private static List<String> diag_con = Arrays.asList("门诊","诊断");
-    private static List<String> past_con = Arrays.asList("既往");
+    private static String pacs_content = "";
+    private static List<String> diag_con = Arrays.asList("门诊","诊断","入院");
+    private static List<String> past_con = Arrays.asList("既往","史");
+    private static String colon = ":|:|提示|示";
+    private static String note = "";
 
     /**
      * 分词,提取词性,封装
@@ -51,26 +55,38 @@ public class StructureAnalyze {
 
         try {
 //            outputInfos.add(initOutputInfo());
+            initialize();
             for (String token:tokens) {
+
+                System.out.println(current);
+
+                note = content.substring(content.indexOf(token)+token.length(),
+                        content.indexOf(token)+token.length()+1);
+                System.out.println(token + "\t" + note);
+
                 updated = false;
+
+                preprocess(token, note);
+
                 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);
-                }
 
-//                Symptom symptom = outputInfos.get(outputInfos.size() - 1).getSymptoms().get(0);
+                    if (current instanceof Pacs) {
+                        if (lexeme.getProperty().equals(Constants.word_property_PACS)) {
+                            String wd = lexeme.getText();
+                            pacs_content = token.substring(token.indexOf(wd) + wd.length());
+                        }
+                    }
 
-                if (updated == false ||
-                        (getPropsize(outputInfo) == 1 && outputInfo.getSymptoms().get(0).getSymptomName().trim().length() == 0)){
-                    outputInfos.remove(outputInfos.size() - 1);
                 }
 
-                bodyPart = null;
+                postprocess(token, outputInfo);
             }
         }
         catch (IOException ioe) {
@@ -82,6 +98,41 @@ public class StructureAnalyze {
         }
     }
 
+    public void initialize() {
+        updated = false;
+        current = null;
+        current_treat = null;
+        current_diag = "";
+        bodyPart = null;
+        num = "";
+        pacs_content = "";
+    }
+
+
+    public void preprocess(String token, String note) {
+    }
+
+    public void postprocess(String token, OutputInfo outputInfo) {
+        if (updated == false ||
+                (getPropsize(outputInfo) == 1 && outputInfo.getSymptoms().get(0).getSymptomName().trim().length() == 0)){
+            outputInfos.remove(outputInfos.size() - 1);
+        }
+
+        bodyPart = null;
+        if (pacs_content.length() > 0) {
+            current = outputInfo.getPacses().get(outputInfo.getPacses().size()-1);
+            String[] arr = pacs_content.split(colon);
+            pacs_content = arr[arr.length-1];
+            outputInfo.getPacses().get(outputInfo.getPacses().size()-1).setValue(pacs_content);
+            System.out.println(pacs_content);
+            pacs_content = "";
+        }
+        else if (outputInfos.get(outputInfos.size()-1).getPacses().size() >= 1 && isUseful(token)) {
+            current = outputInfos.get(outputInfos.size()-1).getPacses().get(0);
+            ((Pacs)current).setValue(((Pacs)current).getValue() + "," + token);
+        }
+    }
+
     public void updateLexeme(Lexeme lexeme) {
         String prop = lexeme.getProperty();
         String[] lexprop = lexeme.getProperty().split(",");
@@ -182,32 +233,47 @@ public class StructureAnalyze {
                 case Constants.word_property_PACS:
                     pacs = new Pacs();
                     pacs.setPacsName(lexeme.getText());
+                    if (bodyPart != null) {
+                        pacs.setPacsName(bodyPart.getPartBodyName() + "+" + pacs.getPacsName());
+                        bodyPart = null;
+                    }
                     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_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:
                     if (bodyPart == null) {
                         bodyPart = new BodyPart();
                         bodyPart.setPartBodyName(lexeme.getText());
-                        updated = true;
                     }
+                    else {
+                        bodyPart.setPartBodyName(bodyPart.getPartBodyName() + "+" + lexeme.getText());
+                    }
+
+                    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_direction:
                     if (current instanceof Pacs) {
                         if (((Pacs)current).getBodypart()==null) {
                             ((Pacs)current).setBodypart("");
                         }
-                        ((Pacs) current).setBodypart(((Pacs)current).getBodypart()+lexeme.getText() + ", ");
+                        ((Pacs) current).setBodypart(((Pacs)current).getBodypart()+lexeme.getText());
                         updated = true;
                     }
                     else if (current instanceof Symptom) {
@@ -236,6 +302,9 @@ public class StructureAnalyze {
                     if (current instanceof Symptom) {
                         symptom = (Symptom)current;
                     }
+                    else if (current == null && outputInfo.getSymptoms().get(0).getSymptomName() == "") {
+                        symptom = outputInfo.getSymptoms().get(0);
+                    }
                     if (symptom != null) {
                         symptom.setTimestamp(pds);
                         updated = true;
@@ -258,7 +327,7 @@ public class StructureAnalyze {
                                 pds1.add(new PD());
                                 symptom.setPds(pds1);
                             }
-                            pd1 = symptom.getPds().get(0);
+                            pd1 = symptom.getPds().get(symptom.getPds().size()-1);
                         } else if (current instanceof Vital) {
                             vital = (Vital) current;
                             if (vital.getPd() == null) {
@@ -296,17 +365,33 @@ public class StructureAnalyze {
                     break;
                 case Constants.word_property_time:
                     if (num.length() > 0) {
-                        if (symptom.getTimestamp() == null) {
+                        if (current instanceof Past) {
                             List<PD> pds1 = new ArrayList<>();
                             pds1.add(new PD());
-                            symptom.setTimestamp(pds1);
+                            ((Past)current).setPds(pds1);
+                            pd1 = pds1.get(0);
+                            pd1.setValue(num);
+                            pd1.setUnit(lexeme.getText());
+                            updated = true;
+                            num = "";
+                            current = null;
+                        }
+                        else {
+                            if (symptom.getTimestamp() == null) {
+                                List<PD> pds1 = new ArrayList<>();
+                                pds1.add(new PD());
+                                symptom.setTimestamp(pds1);
+                            }
+                            else {
+                                symptom.getTimestamp().add(new PD());
+                            }
+                            pd1 = symptom.getTimestamp().get(symptom.getTimestamp().size()-1);
+                            pd1.setValue(num);
+                            pd1.setUnit(lexeme.getText());
+                            updated = true;
+                            num = "";
+                            current = symptom;
                         }
-                        pd1 = symptom.getTimestamp().get(0);
-                        pd1.setValue(num);
-                        pd1.setUnit(lexeme.getText());
-                        updated = true;
-                        num = "";
-                        current = symptom;
                     }
                     break;
                 case Constants.word_property_diagnose:
@@ -317,10 +402,23 @@ public class StructureAnalyze {
                     break;
                 case Constants.word_property_other:
                     if (diag_con.indexOf(lexeme.getText()) >= 0) {
-                        current = new Treat();
+                        outputInfo.getTreats().add(new Treat());
+                        treat = outputInfo.getTreats().get(outputInfo.getTreats().size() - 1);
+                        if (current_diag.length() > 0) {
+                            treat.setDiagnose(current_diag);
+                            current_diag = "";
+                            current = null;
+                        }
                     }
                     else if (past_con.indexOf(lexeme.getText()) >= 0) {
-                        current = new Past();
+                        outputInfo.getPasts().add(new Past());
+                        past = outputInfo.getPasts().get(outputInfo.getPasts().size() - 1);
+                        if (current_diag.length() > 0) {
+                            past.setDiagnose(current_diag);
+                            current = past;
+                            current_diag = "";
+                        }
+
                     }
                     break;
             }
@@ -366,6 +464,7 @@ public class StructureAnalyze {
         Treat treat = null;
         Past past = null;
 
+        System.out.println(name + "\t" + type);
         switch (type) {
             case Constants.word_property_symptom:
                 symptom = outputInfo.getSymptoms().get(outputInfo.getSymptoms().size() - 1);
@@ -409,9 +508,10 @@ public class StructureAnalyze {
                     obj = past;
                     current_treat = past;
                 }
-                else {
+                else //{
                     if (current instanceof Treat) {
                         treat = (Treat) current;
+                        /*
                     } else if (outputInfo.getTreats().size() == 0) {
                         outputInfo.getTreats().add(new Treat());
                         treat = outputInfo.getTreats().get(outputInfo.getTreats().size() - 1);
@@ -422,9 +522,13 @@ public class StructureAnalyze {
                     } else {
                         treat.setDiagnose(treat.getDiagnose() + "," + name);
                     }
+                    */
                     obj = treat;
                     current_treat = treat;
                 }
+                else {
+                    current_diag = name;
+                    }
                 break;
             case Constants.word_property_cause:
                 Cause cause = new Cause();
@@ -546,7 +650,47 @@ public class StructureAnalyze {
                 break;
         }
 
+//        printcurrentobj(obj);
         return obj;
     }
 
+    public Boolean isUseful(String token) {
+        Boolean useful = false;
+        try {
+            LexemePath<Lexeme> lexemes = participle(token, false);
+            for (Lexeme lexeme : lexemes) {
+                if (!lexeme.getProperty().equals("99")) {
+                    useful = true;
+                    break;
+                }
+            }
+        }
+        catch (IOException ioe) {
+            ioe.printStackTrace();
+        }
+        catch (Exception ex) {
+            ex.printStackTrace();
+        }
+        finally {
+            return useful;
+        }
+    }
+
+    public void printcurrentobj(Object obj) {
+        if (obj instanceof Symptom) {
+            Symptom symptom = (Symptom)obj;
+            System.out.println("Symptom:\t" + symptom.getSymptomName() );
+        }
+        else if (obj instanceof Vital) {
+            Vital vital = (Vital)obj;
+            System.out.println("Vital:\t" + vital.getVitalName() );
+        }
+        else if (obj instanceof Treat) {
+            Treat treat = (Treat)obj;
+            System.out.println("Treat:\t" + treat.getTreatName() + "\t" + treat.getDiagnose() );
+        }
+        else {
+            System.out.println(obj);
+        }
+    }
 }