|
@@ -1,203 +0,0 @@
|
|
-package com.lantone.qc.kernel.structure.ai.process;
|
|
|
|
-
|
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
|
-import com.lantone.qc.kernel.structure.ai.model.CrfOut;
|
|
|
|
-import com.lantone.qc.kernel.structure.ai.model.EntityEnum;
|
|
|
|
-import com.lantone.qc.pub.model.entity.*;
|
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
|
-
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
-import java.util.regex.Matcher;
|
|
|
|
-import java.util.regex.Pattern;
|
|
|
|
-
|
|
|
|
-public class EntityProcessMethod {
|
|
|
|
- //获取诊断
|
|
|
|
- public List<Diag> extractDiagEntity(JSONObject outputs) {
|
|
|
|
- List<Diag> diagnose = new ArrayList<>();
|
|
|
|
- Diag diag = null;
|
|
|
|
- List<Map<String, String>> diagEntityList = processJson(outputs, EntityEnum.DIEASE.toString());
|
|
|
|
- for (Map<String, String> diagEntityMap : diagEntityList) {
|
|
|
|
- if (StringUtils.isEmpty(diagEntityMap.get(EntityEnum.DIEASE.toString()))) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- diag = new Diag();
|
|
|
|
- for (String key : diagEntityMap.keySet()) {
|
|
|
|
- String value = StringUtils.isEmpty(diagEntityMap.get(key)) ? "" : diagEntityMap.get(key);
|
|
|
|
- switch (EntityEnum.parseOfValue(key)) {
|
|
|
|
- case DIEASE:
|
|
|
|
- diag.setName(value);
|
|
|
|
- break;
|
|
|
|
- case POSSIBLE:
|
|
|
|
- Possible possible = new Possible();
|
|
|
|
- possible.setName(value);
|
|
|
|
- diag.setPossible(possible);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- diagnose.add(diag);
|
|
|
|
- }
|
|
|
|
- return diagnose;
|
|
|
|
- }
|
|
|
|
- //获取临床表现
|
|
|
|
- public List<Clinical> extractClinicalEntity(JSONObject outputs) {
|
|
|
|
- List<Clinical> clinicals =new ArrayList<>();
|
|
|
|
- Clinical clinical;
|
|
|
|
- List<Map<String, String>> symptomEntityList = processJson(outputs, EntityEnum.CLINICAL_FEATURE.toString());
|
|
|
|
- for (Map<String, String> symptomEntityMap : symptomEntityList) {
|
|
|
|
- if (StringUtils.isEmpty(symptomEntityMap.get(EntityEnum.CLINICAL_FEATURE.toString()))) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- clinical = new Clinical();
|
|
|
|
- for (String key : symptomEntityMap.keySet()) {
|
|
|
|
- String entity = StringUtils.isEmpty(symptomEntityMap.get(key)) ? "" : symptomEntityMap.get(key);
|
|
|
|
- switch (EntityEnum.parseOfValue(key)){
|
|
|
|
- case CLINICAL_FEATURE:
|
|
|
|
- clinical.setName(symptomEntityMap.get(key));
|
|
|
|
- break;
|
|
|
|
- case NEGATIVE:
|
|
|
|
- Negative negative = new Negative();
|
|
|
|
- negative.setName(entity);
|
|
|
|
- clinical.setNegative(negative);
|
|
|
|
- break;
|
|
|
|
- case BODY:
|
|
|
|
- BodyPart bodyPart = new BodyPart();
|
|
|
|
- bodyPart.setName(entity);
|
|
|
|
- clinical.setBodyPart(bodyPart);
|
|
|
|
- case TREND:
|
|
|
|
- Trend trend = new Trend();
|
|
|
|
- trend.setName(entity);
|
|
|
|
- clinical.setTrend(trend);
|
|
|
|
- case CAUSE:
|
|
|
|
- Cause cause = new Cause();
|
|
|
|
- cause.setName(entity);
|
|
|
|
- clinical.setCause(cause);
|
|
|
|
- case TIME:
|
|
|
|
- List<PD> timestamp = new ArrayList<>();
|
|
|
|
- PD pd = new PD();
|
|
|
|
- String[] val_unit = new String[2];
|
|
|
|
- if(entity.trim().length()>0){
|
|
|
|
- val_unit = extract_digit(entity);
|
|
|
|
- }
|
|
|
|
- pd.setValue(val_unit[0]);
|
|
|
|
- pd.setUnit(val_unit[1]);
|
|
|
|
- timestamp.add(pd);
|
|
|
|
- clinical.setTimestamp(timestamp);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- clinicals.add(clinical);
|
|
|
|
- }
|
|
|
|
- return clinicals;
|
|
|
|
- }
|
|
|
|
- /**
|
|
|
|
- * 处理关系抽取输出的json
|
|
|
|
- *
|
|
|
|
- * @param outputs 关系抽取输出的json
|
|
|
|
- * @param entityType 需要处理的实体类别
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public List<Map<String, String>> processJson(JSONObject outputs, String entityType) {
|
|
|
|
- List<Map<String, String>> connectEntityList = new ArrayList<>();
|
|
|
|
- Map<String, String> connectEntity = null;
|
|
|
|
- JSONObject annotation = outputs.getJSONObject("annotation");
|
|
|
|
- JSONArray entitys = annotation.getJSONArray("T");
|
|
|
|
- JSONArray relations = annotation.getJSONArray("R");
|
|
|
|
- for (int i = 0; i < entitys.size(); i++) {
|
|
|
|
- if (StringUtils.isEmpty(entitys.get(i).toString())) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- JSONObject entity = entitys.getJSONObject(i);
|
|
|
|
- if (entityType.equals(entity.getString("name"))) {
|
|
|
|
- int id = entity.getIntValue("id");
|
|
|
|
- List<Integer> connectEntityIdList = getConnectEntityIdList(id, relations);
|
|
|
|
- if (connectEntityIdList.size() == 0) {
|
|
|
|
- connectEntity = new HashMap<>();
|
|
|
|
- connectEntity.put(entity.getString("name"), entity.getString("value"));
|
|
|
|
- connectEntityList.add(connectEntity);
|
|
|
|
- } else {
|
|
|
|
- connectEntity = getConnectEntity(connectEntityIdList, entitys);
|
|
|
|
- connectEntity.put(entity.getString("name"), entity.getString("value"));
|
|
|
|
- connectEntityList.add(connectEntity);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return connectEntityList;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取与传入实体有关系实体的id列表(List)
|
|
|
|
- *
|
|
|
|
- * @param entityId 传入实体的id
|
|
|
|
- * @param relations 关系抽取出的关系对
|
|
|
|
- * @return connectEntityIdList 有关系实体的id列表(List)
|
|
|
|
- */
|
|
|
|
- public List<Integer> getConnectEntityIdList(int entityId, JSONArray relations) {
|
|
|
|
- List<Integer> connectEntityIdList = new ArrayList<>();
|
|
|
|
- for (int i = 0; i < relations.size(); i++) {
|
|
|
|
- if (StringUtils.isEmpty(relations.get(i).toString())) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- JSONObject relation = relations.getJSONObject(i);
|
|
|
|
- if (relation.getIntValue("from") == entityId) {
|
|
|
|
- connectEntityIdList.add(relation.getIntValue("to"));
|
|
|
|
- }
|
|
|
|
- if (relation.getIntValue("to") == entityId) {
|
|
|
|
- connectEntityIdList.add(relation.getIntValue("from"));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return connectEntityIdList;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取实体id列表对应的所有实体类型及实体值
|
|
|
|
- *
|
|
|
|
- * @param connectEntityIdList 实体id列表
|
|
|
|
- * @param entitys 关系抽取的实体列表
|
|
|
|
- * @return entityRelationPair 实体id列表对应的所有实体类型及实体值
|
|
|
|
- */
|
|
|
|
- public Map<String, String> getConnectEntity(List<Integer> connectEntityIdList, JSONArray entitys) {
|
|
|
|
- Map<String, String> entityRelationPair = new HashMap<>();
|
|
|
|
- for (int connectEntityId : connectEntityIdList) {
|
|
|
|
- for (int i = 0; i < entitys.size(); i++) {
|
|
|
|
- if (StringUtils.isEmpty(entitys.get(i).toString())) {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- JSONObject entity = entitys.getJSONObject(i);
|
|
|
|
- if (connectEntityId == entity.getIntValue("id")) {
|
|
|
|
- if (entityRelationPair.containsKey(entity.getString("name"))) {
|
|
|
|
- entityRelationPair.put(entity.getString("name"),
|
|
|
|
- entityRelationPair.get(entity.getString("name")) + "," + entity.getString("value"));
|
|
|
|
- } else {
|
|
|
|
- entityRelationPair.put(entity.getString("name"), entity.getString("value"));
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return entityRelationPair;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- public String[] extract_digit(String value) {
|
|
|
|
- String[] res = new String[2];
|
|
|
|
- try {
|
|
|
|
- String reg_time = "([\\d]+)([\\u4e00-\\u9fa5]+)";
|
|
|
|
- Pattern pattern = Pattern.compile(reg_time);
|
|
|
|
- Matcher matcher = pattern.matcher(value);
|
|
|
|
- if (matcher.find(0)) {
|
|
|
|
- res[0] = matcher.group(1);
|
|
|
|
- res[1] = matcher.group(2);
|
|
|
|
- } else {
|
|
|
|
- res[0] = value;
|
|
|
|
- res[1] = "";
|
|
|
|
- }
|
|
|
|
- } catch (Exception ex) {
|
|
|
|
- ex.printStackTrace();
|
|
|
|
- }
|
|
|
|
- return res;
|
|
|
|
- }
|
|
|
|
-}
|
|
|