我使用StanfordCoreNLP jar文件库将英文段落拆分成句子,但我可以作为CoreMap对象检索拆分的句子,但我想将CoreMap类型的拆分语句转换为String,是否存在实现此任务的方法。代码中的粗体文本显示了使用CoreMap的区域,我希望检索到的句子将其转换为字符串。
代码片段:
props.setProperty("annotators","tokenize,ssplit");
//put that in a pipeline
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
//a data structure for the annotation
Annotation document = new Annotation(text);
// run the pipeline on that data structure
pipeline.annotate(document);
// access the annotations which has worked on a sentence
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
PrintStream printStream = new PrintStream(new FileOutputStream("/home/sakshi/Desktop/Admin_System/translate.en"));
PrintStream console = System.out; // To store the reference to default output stream to use it to restore the default std output stream
System.setOut(printStream);// To change the default output stream
**for (CoreMap sentence : sentences) {
System.out.println(sentence);**
}
System.setOut(console);
response.setContentType("text/plain");
response.getWriter().write(text);发布于 2018-03-21 11:47:10
whatever.toString(),因为toString适用于每个java对象,因为每个对象都是从java.lang.Object继承的。
发布于 2018-03-21 11:43:18
不确定CoreMap的确切类型,但可能Map#values()是您需要的列表。要将其转换为单个字符串,可以使用Java8 Streams API:
list.stream ().map (i -> i.toString ()).collect (Collectors.joining (","));https://stackoverflow.com/questions/49405325
复制相似问题