Java和Stanford Parser

时间:2018-03-21 11:36:44

标签: java stanford-nlp

我使用StanfordCoreNLP jar文件库将英文段落分成句子但我可以将拆分句子检索为CoreMap对象,但是我想将CoreMap类型的拆分句子转换为String类型,无论如何都要实现这个任务。代码中的粗体文本显示了使用CoreMap的区域,我希望检索到的句子将其转换为String

代码段:

    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);

2 个答案:

答案 0 :(得分:0)

不确定CoreMap的确切类型,但Map#values()可能是您需要的List。要将其转换为单个String,您可以使用Java8 Streams API:

list.stream ().map (i -> i.toString ()).collect (Collectors.joining (","));

答案 1 :(得分:-1)

whatever.toString() 因为toString适用于每个java Object,因为每个对象都继承自java.lang.Object