gpt4 book ai didi

java - 使用 MapReduce 作业调用斯坦福CoreNLP API

转载 作者:太空宇宙 更新时间:2023-11-04 14:45:17 25 4
gpt4 key购买 nike

我正在尝试使用MapReduce处理大量文档,其想法是将文件拆分为mapper中的文档,并在reducer阶段应用stanford coreNLP注释器。

我有一个相当简单的(标准)管道“tokenize,ssplit,pos,lemma,ner”,并且reducer只是调用一个函数,该函数将这些注释器应用于reducer传递的值并返回注释(如List字符串),但是生成的输出是垃圾。

我观察到,如果我从映射器内调用注释函数,作业会返回预期的输出,但这会击败整个并行性。当我忽略在 reducer 中获得的值并仅将注释器应用于虚拟字符串时,作业也会返回预期的输出。

这可能表明该过程中存在一些线程安全问题,但我无法弄清楚我的注释函数是同步的并且管道是私有(private)最终的。

有人可以提供一些关于如何解决此问题的指示吗?

-昂舒

编辑:

这就是我的 reducer 的样子,希望这能增加清晰度

public static class Reduce extends MapReduceBase implements Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException {
while (values.hasNext()) {
output.collect(key, new Text(se.getExtracts(values.next().toString()).toString()));
}
}
}

这是获取摘录的代码:

final StanfordCoreNLP pipeline; 
public instantiatePipeline(){
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, ner");

}


synchronized List<String> getExtracts(String l){
Annotation document = new Annotation(l);

ArrayList<String> ret = new ArrayList<String>();

pipeline.annotate(document);

List<CoreMap> sentences = document.get(SentencesAnnotation.class);
int sid = 0;
for(CoreMap sentence:sentences){
sid++;
for(CoreLabel token: sentence.get(TokensAnnotation.class)){
String word = token.get(TextAnnotation.class);
String pos = token.get(PartOfSpeechAnnotation.class);
String ner = token.get(NamedEntityTagAnnotation.class);
String lemma = token.get(LemmaAnnotation.class);

Timex timex = token.get(TimeAnnotations.TimexAnnotation.class);

String ex = word+","+pos+","+ner+","+lemma;
if(timex!=null){
ex = ex+","+timex.tid();
}
else{
ex = ex+",";
}
ex = ex+","+sid;
ret.add(ex);
}
}

最佳答案

我解决了这个问题,实际上问题出在我正在读取的文件中的文本编码(我猜将其转换为文本会导致进一步的损坏),这导致了标记化和溢出垃圾的问题。我正在清理输入字符串并应用严格的 UTF-8 编码,现在一切正常。

关于java - 使用 MapReduce 作业调用斯坦福CoreNLP API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24467990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com