我正在将斯坦福 CoreNLP 库用于 Java 项目。我创建了一个名为 StanleyNLP 的类,并实例化了两个不同的对象,并使用不同的字符串作为参数初始化了构造函数。我正在使用词性标注器来获取形容词-名词序列。但是,程序的输出仅向我显示第一个对象的结果。每个斯坦福NLP 对象都使用不同的字符串进行初始化,但每个对象都返回与第一个对象相同的结果。我是 Java 新手,所以我无法判断我的代码是否有问题,或者运行它的 HPC 集群是否有问题。
我尝试使用 getter,而不是从斯坦福自然语言处理类方法返回字符串列表。我还尝试将第一个斯坦福NLP 对象设置为 null,这样它就不会引用任何内容,然后创建其他对象。没有任何效果。
/* in main */
List<String> pos_tokens0 = new ArrayList<String>();
List<String> pos_tokens1 = new ArrayList<String>();
String text0 = "Mary little lamb white fleece like snow"
StanfordNLP snlp0 = new StanfordNLP(text0);
pos_tokens0 = snlp0.process();
String text1 = "Everywhere little Mary went fluffy lamb ate green grass"
StanfordNLP snlp1 = new StanfordNLP(text1);
pos_tokens1 = snlp1.process();
/* in StanfordNLP.java */
public class StanfordNLP {
private static List<String> pos_adjnouns = new ArrayList<String>();
private String documentText = "";
public StanfordNLP() {}
public StanfordNLP(String text) { this.documentText = text; }
public List<String> process() {
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, depparse");
props.setProperty("coref.algorithm", "neural");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Annotation document = new Annotation(documentText);
pipeline.annotate(document);
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
List<String[]> corpus_temp = new ArrayList<String[]>();
int count = 0;
for(CoreMap sentence: sentences) {
for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
String[] data = new String[2];
String word = token.get(TextAnnotation.class);
String pos = token.get(PartOfSpeechAnnotation.class);
count ++;
data[0] = word;
data[1] = pos;
corpus_temp.add(data);
}
}
String[][] corpus = corpus_temp.toArray(new String[count][2]);
// corpus contains string arrays with a word and its part-of-speech.
for (int i=0; i<(corpus.length-3); i++) {
String word = corpus[i][0];
String pos = corpus[i][1];
String word2 = corpus[i+1][0];
String pos2 = corpus[i+1][1];
// find adjectives and nouns (eg, "fast car")
if (pos.equals("JJ")) {
if (pos2.equals("NN") || pos2.equals("NNP") || pos2.equals("NNPS")) {
word = word + " " + word2;
pos_adjnouns.add(word);
}
}
}
return pos_adjnouns;
}
pos_tokens0 的预期输出是“小羊羔,白羊毛”。 pos_tokens1 的预期输出是“小玛丽,蓬松的羊羔,绿草”。但两个变量的实际输出都是“小羊羔,白羊毛”。
知道为什么会发生这种情况吗?我在 HPC 服务器上运行了一个带有 main.java 和 myclass.java 的简单 Java jar 文件,但无法复制此问题。因此,HPC 服务器似乎不存在同一类的多个对象的问题。
最佳答案
问题看起来很简单,就是你的pos_adjnouns
变量是 static
,因此在 StanfordNLP
的所有实例之间共享……尝试删除 static
关键字,看看事情是否按您的预期工作。
但是这样仍然不对,因为您有一个实例变量并且多次调用 process()
,事情会不断添加到 pos_adjnouns
中列表。您应该做的另外两件事是:
- 制造
pos_adjnouns
process()
中的方法变量方法 - 相反,初始化 StanleyCoreNLP 管道的成本很高,因此您应该将其移出
process()
方法并在类构造函数中执行它。事情可能完全相反,构造函数初始化管道,而process()
可能会更好。采取String
的方法进行分析。
关于machine-learning - 运行斯坦福 CoreNLP 时,某些 HPC 集群是否只缓存一个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56180504/