linsheng0116 1 year ago
parent
commit
fd5b53ebb5
  1. 2
      kcui/src/view/Graph.vue
  2. 189
      src/main/java/com/main/woka/Common/util/RelationUtil.java
  3. 188
      src/main/java/com/main/woka/Common/util/RelationUtil1.java
  4. 23
      src/main/java/com/main/woka/Web/Service/impl/KcTlFileServiceImpl.java

2
kcui/src/view/Graph.vue

@ -350,7 +350,7 @@ export default {
{size: 250, width: 250, height: 250, color: '30,255,0', font: 'normal 40px Arial', fontColor: '0,0,0'},
{size: 200, width: 200, height: 200, color: '248,143,248', font: 'normal 32px Arial', fontColor: '255,255,255'},
{size: 150, width: 150, height: 150, color: '65,154,255', font: 'normal 30px Arial', fontColor: '255,255,255'},
{size: 100, width: 100, height: 100, color: '0,228,255', font: 'normal 28px Arial', fontColor: '0,0,0'}
{size: 120, width: 120, height: 120, color: '0,228,255', font: 'normal 28px Arial', fontColor: '0,0,0'}
];
return styles[group] || {};

189
src/main/java/com/main/woka/Common/util/RelationUtil.java

@ -1,9 +1,14 @@
package com.main.woka.Common.util;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.util.CoreMap;
import java.util.Properties;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.StringUtils;
import org.springframework.stereotype.Component;
@ -11,123 +16,8 @@ import java.util.*;
@Component
public class RelationUtil {
//标题分词
public String analyzeRelationship(String title1, String title2) {
//载入properties 文件
String[] args = new String[]{"-props", "CoreNLP-chinese.properties"};
Properties properties = StringUtils.argsToProperties(args);
properties.setProperty("annotators", "tokenize, ssplit, pos");
properties.setProperty("tokenize.language", "zh"); // 设置为中文
StanfordCoreNLP pipline = new StanfordCoreNLP(properties);
// 分析第一个短语 "低温"
String phrase1 = title1;
List<Map> ll = analyzePhrase(pipline, phrase1);
//循环第一个短语的分词
String useTitle = "";
for(int i=0;i<ll.size();i++){
if (ll.get(i).get("pos").equals("NN") || ll.get(i).get("pos").equals("JJ")){
useTitle = useTitle+ll.get(i).get("text");
}else {
break;
}
}
// 分析第二个短语 "低温的影响"
String phrase2 = title2;
String result = analyzePhrase1(pipline, phrase2, useTitle);
return result;
}
public List<Map> analyzePhrase(StanfordCoreNLP pipeline, String phrase) {
List<Map> list = new ArrayList<>();
Annotation document = new Annotation(phrase);
pipeline.annotate(document);
List<CoreLabel> tokens = document.get(CoreAnnotations.TokensAnnotation.class);
for (CoreLabel token : tokens) {
String word = token.get(CoreAnnotations.TextAnnotation.class);
String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
System.out.println(word + ": " + pos);
Map mm = new HashMap();
mm.put("text",word);
mm.put("pos",pos);
list.add(mm);
}
return list;
}
public String analyzePhrase1(StanfordCoreNLP pipeline, String phrase,String useTitle) {
Annotation document = new Annotation(phrase);
pipeline.annotate(document);
String title= "";
String result ="";
Integer index = 0;
System.out.println(useTitle);
List<CoreLabel> tokens = document.get(CoreAnnotations.TokensAnnotation.class);
for(int i=0;i<tokens.size();i++){
CoreLabel token = tokens.get(i);
String word = token.get(CoreAnnotations.TextAnnotation.class);
String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
title= title + word;
if(useTitle.equals(title)){
//有相同词汇有关系
index = i+1;
break;
}
}
System.out.println(index);
if (index==0){
return "属于";
}else {
for(int j=index;j<tokens.size();j++){
CoreLabel token1 = tokens.get(j);
String word1 = token1.get(CoreAnnotations.TextAnnotation.class);
String pos1 = token1.get(CoreAnnotations.PartOfSpeechAnnotation.class);
if(j==index){
System.out.println(pos1);
if(word1.equals("的")){
System.out.println("aaaaaaaaaaaaaaaa");
}else {
if(pos1.equals("PU")){
result = "属于";
break;
}
if (!pos1.equals("CC") && !pos1.equals("DEG")){
result= result + word1;
}
}
}else {
result= result + word1;
}
}
if(result.equals("")){
result = "属于";
}
return result;
}
}
//将一句话分成多个词语
public List<String> analyzeRelationshipLong(String title1) {
List<String> list = new ArrayList<>();
public String analyzeRelationship(String title1, String title2) {
String[] args = new String[]{"-props", "CoreNLP-chinese.properties"};
Properties properties = StringUtils.argsToProperties(args);
properties.setProperty("annotators", "tokenize, ssplit, pos");
@ -135,53 +25,38 @@ public class RelationUtil {
StanfordCoreNLP pipline = new StanfordCoreNLP(properties);
// 分析第一个短语 "低温"
String phrase1 = title1;
List<Map> ll = analyzePhrase(pipline, phrase1);
for(int i=0;i<ll.size();i++){
if (ll.get(i).get("pos").equals("NN")){
list.add(String.valueOf(ll.get(i).get("text")));
}
// 对两个标题进行NLP处理
CoreDocument doc1 = new CoreDocument(title1);
CoreDocument doc2 = new CoreDocument(title2);
pipline.annotate(doc1);
pipline.annotate(doc2);
// 提取名词短语作为核心概念
String concept1 = extractKeyConcept(doc1);
String concept2 = extractKeyConcept(doc2);
System.out.println(concept1);
System.out.println(concept2);
// 简单检查是否第二个标题是第一个标题的定义
if (title2.contains(concept1) && title2.contains("定义")) {
return "定义";
} else if (title1.contains(concept2) && title1.contains("现象")) {
return "实例";
}
return list;
return "属于";
}
public List<String> analyzeRelationshipLong1(String title1) {
List<String> list = new ArrayList<>();
String[] args = new String[]{"-props", "CoreNLP-chinese.properties"};
Properties properties = StringUtils.argsToProperties(args);
properties.setProperty("annotators", "tokenize, ssplit, pos");
properties.setProperty("tokenize.language", "zh"); // 设置为中文
StanfordCoreNLP pipline = new StanfordCoreNLP(properties);
// 分析第一个短语 "低温"
String phrase1 = title1;
List<Map> ll = analyzePhrase(pipline, phrase1);
for(int i=0;i<ll.size();i++){
if (ll.get(i).get("pos").equals("NN")){
if(String.valueOf(ll.get(i).get("text")).charAt(0)!='<' && String.valueOf(ll.get(i).get("text")).charAt(0)!='&' && String.valueOf(ll.get(i).get("text")).charAt(String.valueOf(ll.get(i).get("text")).length()-1)!='t'){
int hh = 0;
for(int b=0;b<list.size();b++){
if(list.get(b).equals(String.valueOf(ll.get(i).get("text")))){
hh = 1;
break;
}
}
if (hh == 0){
list.add(String.valueOf(ll.get(i).get("text")));
}
private static String extractKeyConcept(CoreDocument document) {
StringBuilder keyConcept = new StringBuilder();
for (CoreSentence sentence : document.sentences()) {
for (int i = 0; i < sentence.tokens().size(); i++) {
String pos = sentence.tokens().get(i).get(CoreAnnotations.PartOfSpeechAnnotation.class);
if (pos.startsWith("NN")) { // 包括名词和专有名词
keyConcept.append(sentence.tokens().get(i).originalText()).append(" ");
}
}
}
return list;
return keyConcept.toString().trim();
}
}

188
src/main/java/com/main/woka/Common/util/RelationUtil1.java

@ -0,0 +1,188 @@
package com.main.woka.Common.util;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.util.StringUtils;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class RelationUtil1 {
//标题分词
public String analyzeRelationship(String title1, String title2) {
//载入properties 文件
String[] args = new String[]{"-props", "CoreNLP-chinese.properties"};
Properties properties = StringUtils.argsToProperties(args);
properties.setProperty("annotators", "tokenize, ssplit, pos");
properties.setProperty("tokenize.language", "zh"); // 设置为中文
StanfordCoreNLP pipline = new StanfordCoreNLP(properties);
// 分析第一个短语 "低温"
String phrase1 = title1;
List<Map> ll = analyzePhrase(pipline, phrase1);
//循环第一个短语的分词
String useTitle = "";
for(int i=0;i<ll.size();i++){
if (ll.get(i).get("pos").equals("NN") || ll.get(i).get("pos").equals("JJ")){
useTitle = useTitle+ll.get(i).get("text");
}else {
break;
}
}
// 分析第二个短语 "低温的影响"
String phrase2 = title2;
String result = analyzePhrase1(pipline, phrase2, useTitle);
return result;
}
public List<Map> analyzePhrase(StanfordCoreNLP pipeline, String phrase) {
List<Map> list = new ArrayList<>();
Annotation document = new Annotation(phrase);
pipeline.annotate(document);
List<CoreLabel> tokens = document.get(CoreAnnotations.TokensAnnotation.class);
for (CoreLabel token : tokens) {
String word = token.get(CoreAnnotations.TextAnnotation.class);
String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
System.out.println(word + ": " + pos);
Map mm = new HashMap();
mm.put("text",word);
mm.put("pos",pos);
list.add(mm);
}
return list;
}
public String analyzePhrase1(StanfordCoreNLP pipeline, String phrase,String useTitle) {
Annotation document = new Annotation(phrase);
pipeline.annotate(document);
String title= "";
String result ="";
Integer index = 0;
System.out.println(useTitle);
List<CoreLabel> tokens = document.get(CoreAnnotations.TokensAnnotation.class);
for(int i=0;i<tokens.size();i++){
CoreLabel token = tokens.get(i);
String word = token.get(CoreAnnotations.TextAnnotation.class);
String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
title= title + word;
if(useTitle.equals(title)){
//有相同词汇有关系
index = i+1;
break;
}
}
System.out.println(index);
if (index==0){
return "属于";
}else {
for(int j=index;j<tokens.size();j++){
CoreLabel token1 = tokens.get(j);
String word1 = token1.get(CoreAnnotations.TextAnnotation.class);
String pos1 = token1.get(CoreAnnotations.PartOfSpeechAnnotation.class);
if(j==index){
System.out.println(pos1);
if(word1.equals("的")){
System.out.println("aaaaaaaaaaaaaaaa");
}else {
if(pos1.equals("PU")){
result = "属于";
break;
}
if (!pos1.equals("CC") && !pos1.equals("DEG")){
result= result + word1;
}
}
}else {
result= result + word1;
}
}
if(result.equals("")){
result = "属于";
}
return result;
}
}
//将一句话分成多个词语
public List<String> analyzeRelationshipLong(String title1) {
List<String> list = new ArrayList<>();
String[] args = new String[]{"-props", "CoreNLP-chinese.properties"};
Properties properties = StringUtils.argsToProperties(args);
properties.setProperty("annotators", "tokenize, ssplit, pos");
properties.setProperty("tokenize.language", "zh"); // 设置为中文
StanfordCoreNLP pipline = new StanfordCoreNLP(properties);
// 分析第一个短语 "低温"
String phrase1 = title1;
List<Map> ll = analyzePhrase(pipline, phrase1);
for(int i=0;i<ll.size();i++){
if (ll.get(i).get("pos").equals("NN")){
list.add(String.valueOf(ll.get(i).get("text")));
}
}
return list;
}
public List<String> analyzeRelationshipLong1(String title1) {
List<String> list = new ArrayList<>();
String[] args = new String[]{"-props", "CoreNLP-chinese.properties"};
Properties properties = StringUtils.argsToProperties(args);
properties.setProperty("annotators", "tokenize, ssplit, pos");
properties.setProperty("tokenize.language", "zh"); // 设置为中文
StanfordCoreNLP pipline = new StanfordCoreNLP(properties);
// 分析第一个短语 "低温"
String phrase1 = title1;
List<Map> ll = analyzePhrase(pipline, phrase1);
for(int i=0;i<ll.size();i++){
if (ll.get(i).get("pos").equals("NN")){
if(String.valueOf(ll.get(i).get("text")).charAt(0)!='<' && String.valueOf(ll.get(i).get("text")).charAt(0)!='&' && String.valueOf(ll.get(i).get("text")).charAt(String.valueOf(ll.get(i).get("text")).length()-1)!='t'){
int hh = 0;
for(int b=0;b<list.size();b++){
if(list.get(b).equals(String.valueOf(ll.get(i).get("text")))){
hh = 1;
break;
}
}
if (hh == 0){
list.add(String.valueOf(ll.get(i).get("text")));
}
}
}
}
return list;
}
}

23
src/main/java/com/main/woka/Web/Service/impl/KcTlFileServiceImpl.java

@ -30,6 +30,9 @@ public class KcTlFileServiceImpl {
RelationUtil relationUtil;
@Autowired
RelationUtil1 relationUtil1;
@Autowired
Neo4jUtil neo4jUtil;
@Autowired
@ -86,8 +89,8 @@ public class KcTlFileServiceImpl {
for(int i=0;i<list1.size();i++){
String target = list1.get(i).getName();
// String aa = relationUtil.analyzeRelationship(source,target);
String aa = "属于";
String aa = relationUtil1.analyzeRelationship(source,target);
// String aa = "属于";
// System.out.println(aa);
//获取两个的Id
Long TwoId = list1.get(i).getId();
@ -110,8 +113,8 @@ public class KcTlFileServiceImpl {
for(int i=0;i<list1.size();i++){
String source = list2.get(a1).getName();
String target = list1.get(i).getName();
// String aa = relationUtil.analyzeRelationship(source,target);
String aa = "属于";
String aa = relationUtil1.analyzeRelationship(source,target);
// String aa = "属于";
System.out.println(aa);
//获取两个的Id
Long TwoId = list1.get(i).getId();
@ -135,8 +138,8 @@ public class KcTlFileServiceImpl {
for(int i=0;i<list1.size();i++){
String source = list3.get(a2).getName();
String target = list1.get(i).getName();
String aa = "属于";
// String aa = relationUtil.analyzeRelationship(source,target);
// String aa = "属于";
String aa = relationUtil1.analyzeRelationship(source,target);
System.out.println(aa);
//获取两个的Id
Long TwoId = list1.get(i).getId();
@ -161,8 +164,8 @@ public class KcTlFileServiceImpl {
for(int i=0;i<list1.size();i++){
String source = list4.get(a2).getName();
String target = list1.get(i).getName();
String aa = "属于";
// String aa = relationUtil.analyzeRelationship(source,target);
// String aa = "属于";
String aa = relationUtil1.analyzeRelationship(source,target);
System.out.println(aa);
//获取两个的Id
Long TwoId = list1.get(i).getId();
@ -186,8 +189,8 @@ public class KcTlFileServiceImpl {
for(int i=0;i<list1.size();i++){
String source = list5.get(a2).getName();
String target = list1.get(i).getName();
String aa = "属于";
// String aa = relationUtil.analyzeRelationship(source,target);
// String aa = "属于";
String aa = relationUtil1.analyzeRelationship(source,target);
System.out.println(aa);
//获取两个的Id
Long TwoId = list1.get(i).getId();

Loading…
Cancel
Save