我在大学作业中使用斯坦福 NLP 工具。该解析器在每个点(句点)结束句子,但我还需要在每一行中结束,即在每个字符中结束 '\n' 。通过命令行,您可以使用选项“-sentences”,但到目前为止还没有类似的代码命令。
LexicalizedParser 中的选项 setOptionFlags 也不起作用
最佳答案
这里有一些示例代码来详细说明 Gabor 的回答:
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import java.io.*;
import java.util.*;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.semgraph.*;
import edu.stanford.nlp.trees.TreeCoreAnnotations.*;
import edu.stanford.nlp.ling.CoreAnnotations.*;
import edu.stanford.nlp.util.*;
public class ParserExample {
public static void main (String[] args) throws IOException {
String text = new String(Files.readAllBytes(Paths.get(args[0])), StandardCharsets.UTF_8);
Annotation document = new Annotation(text);
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse");
props.setProperty("ssplit.newlineIsSentenceBreak", "always");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
pipeline.annotate(document);
}
}
args[0] 应该是你的文件的路径,每行一句话
您需要从此链接下载 Stanford CoreNLP 3.5.2,并将下载的 jar 文件放入您的类路径:http://nlp.stanford.edu/software/corenlp.shtml
您可以使用 props.setProperty() 为解析器设置其他选项
如果你有一个每行一个句子的文件,你可以使用
props.setProperty("ssplit.eolonly", "true");
如果您只想按换行符拆分。
关于java - 斯坦福 CoreNLP : input with one sentence per line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33604939/