|
@@ -4,6 +4,8 @@ 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.kernel.structure.ai.model.Lemma;
|
|
|
+import com.lantone.qc.pub.model.entity.DiagInfectious;
|
|
|
import com.lantone.qc.pub.model.entity.Negative;
|
|
|
import org.apache.commons.beanutils.BeanUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -21,6 +23,70 @@ import java.util.regex.Pattern;
|
|
|
* @Date: 2020/2/2 10:07
|
|
|
*/
|
|
|
public class EntityProcess {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 树形结构存储三元组关系,返回传入实体类型为节点的所有关联实体对象
|
|
|
+ * @param aiOut
|
|
|
+ * @param entityType
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<Lemma> createEntityTree(JSONObject aiOut, String entityType) {
|
|
|
+ List<Lemma> resultLemmaList = new ArrayList<>();
|
|
|
+ JSONObject annotation = aiOut.getJSONObject("annotation");
|
|
|
+ JSONArray entitys = annotation.getJSONArray("T");
|
|
|
+ JSONArray relations = annotation.getJSONArray("R");
|
|
|
+
|
|
|
+ List<Lemma> allLemmaList = loadAllLemmaList(entitys);
|
|
|
+ for (Lemma l : allLemmaList) {
|
|
|
+ if (entityType.equals(l.getProperty())) {
|
|
|
+ findRelationLemma(l, allLemmaList, relations);
|
|
|
+ resultLemmaList.add(l);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return resultLemmaList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查找所有实体对象
|
|
|
+ * @param entitys
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<Lemma> loadAllLemmaList(JSONArray entitys) {
|
|
|
+ List<Lemma> allLemmaList = new ArrayList<>();
|
|
|
+ //所有实体读取
|
|
|
+ for (int i = 0; i < entitys.size(); i++) {
|
|
|
+ if (StringUtils.isEmpty(entitys.get(i).toString())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ JSONObject entity = entitys.getJSONObject(i);
|
|
|
+ Lemma lemma = new Lemma();
|
|
|
+ lemma.setId(entity.getIntValue("id"));
|
|
|
+ lemma.setText(entity.getString("value"));
|
|
|
+ lemma.setProperty(entity.getString("name"));
|
|
|
+ lemma.setPosition(entity.getString("start"));
|
|
|
+ allLemmaList.add(lemma);
|
|
|
+ }
|
|
|
+ return allLemmaList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归查询所有子实体信息
|
|
|
+ * @param lemma
|
|
|
+ * @param allLemmaList
|
|
|
+ * @param relations
|
|
|
+ */
|
|
|
+ private void findRelationLemma(Lemma lemma, List<Lemma> allLemmaList, JSONArray relations) {
|
|
|
+ List<Integer> connectEntityIdList = getConnectEntityIdList(lemma.getId(), relations);
|
|
|
+ for (Lemma l : allLemmaList) {
|
|
|
+ for (Integer reId : connectEntityIdList) {
|
|
|
+ if (l.getId() == reId) {
|
|
|
+ findRelationLemma(l, allLemmaList, relations);
|
|
|
+ lemma.addRelationLemmas(l);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 处理关系抽取输出的json
|
|
|
*
|
|
@@ -111,18 +177,18 @@ public class EntityProcess {
|
|
|
|
|
|
public <T> List<T> addEntity(JSONObject aiOut, EntityEnum entityType, Class<T> t) throws Exception {
|
|
|
List<T> list = new ArrayList<>();
|
|
|
- List<Map<String, String>> pastEntityList = processJson(aiOut, entityType.toString());
|
|
|
- for (Map<String, String> pastEntityMap : pastEntityList) {
|
|
|
- if (StringUtils.isEmpty(pastEntityMap.get(entityType.toString()))) {
|
|
|
+ List<Map<String, String>> entityList = processJson(aiOut, entityType.toString());
|
|
|
+ for (Map<String, String> entityMap : entityList) {
|
|
|
+ if (StringUtils.isEmpty(entityMap.get(entityType.toString()))) {
|
|
|
continue;
|
|
|
}
|
|
|
T o = t.newInstance();
|
|
|
- for (String key : pastEntityMap.keySet()) {
|
|
|
+ for (String key : entityMap.keySet()) {
|
|
|
if (key.equals(entityType.toString())) {
|
|
|
- BeanUtils.copyProperty(o, "name", pastEntityMap.get(key));
|
|
|
+ BeanUtils.copyProperty(o, "name", entityMap.get(key));
|
|
|
} else if (key.equals(EntityEnum.NEGATIVE.toString())) {
|
|
|
Negative negative = new Negative();
|
|
|
- negative.setName(StringUtils.isEmpty(pastEntityMap.get(key)) ? "" : pastEntityMap.get(key));
|
|
|
+ negative.setName(StringUtils.isEmpty(entityMap.get(key)) ? "" : entityMap.get(key));
|
|
|
BeanUtils.copyProperty(o, "negative", negative);
|
|
|
}
|
|
|
}
|