machine-learning - 运行 Stanford CoreNLP 时,某些 HPC 集群是否只缓存一个结果?
问题描述
我正在将斯坦福 CoreNLP 库用于 Java 项目。我创建了一个名为 StanfordNLP 的类,并实例化了两个不同的对象,并使用不同的字符串作为参数初始化了构造函数。我正在使用 POS 标记器来获取形容词-名词序列。但是,程序的输出只显示了第一个对象的结果。每个 StanfordNLP 对象都使用不同的字符串进行初始化,但每个对象都返回与第一个对象相同的结果。我是 Java 新手,所以我无法判断我的代码是否存在问题,或者运行它的 HPC 集群是否存在问题。
我没有从 StanfordNLP 类方法返回字符串列表,而是尝试使用 getter。我还尝试将第一个 StanfordNLP 对象设置为 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()
- 相反,初始化 StanfordCoreNLP 管道的成本很高,因此您应该将其移出
process()
方法并在类构造函数中执行。事情可能完全相反,构造函数初始化管道,process()
方法进行String
分析可能会更好。
推荐阅读
- c++ - “纯虚拟变量”或:如何强制派生类初始化静态成员变量?
- java - 如何在回收站视图android中选择多个项目?
- javascript - 我的 Mineflayer 机器人没有回复我的消息
- python - 将元组列表划分为多个列表的更简洁方法
- react-native - 在反应原生应用程序的顶部和底部添加颜色
- google-cloud-platform - GCP M1 和 M2 CPU 在批准配额请求后未更新
- javascript - 如何仅为 Android 设备隐藏网站内容?
- javascript - cordova-plugin-sign-in-with-apple 无限加载
- xmpp - XMPP MUC - 如果机器人发送消息没有通知
- postgresql - 有没有办法将 dd-mm-yyyy 格式的 varchar 列转换为 postgresql 中 yyyy-mm-dd 格式的日期列?