【问题标题】:algorithm to extract simple sentences from complex(mixed) sentences?从复杂(混合)句子中提取简单句子的算法?
【发布时间】:2012-04-04 22:58:15
【问题描述】:

有没有一种算法可以用来从段落中提取简单的句子?

我的最终目标是稍后对生成的简单句子运行另一个算法来确定作者的情绪。

我从 Chae-Deug Park 等来源对此进行了研究,但没有人讨论准备简单的句子作为训练数据。

提前致谢

【问题讨论】:

  • “简单句”到底是什么意思?与段落相比,只是一个句子——在这种情况下,您的问题是关于句子边界检测。或者一个只包含一个主要谓词的句子(而不是一个包含从句等的复杂句子)?还是完全不同的东西?
  • 嗨 jogojapan,是的,这是正确的,我的意思是一个句子与一个段落相比......
  • 一个简单的句子你没有正确定义你的意思,所以任何人都很难回答你的问题。也许您想使用斯坦福解析器之类的工具来获取每个句子的解析树,并删除所有不属于“NP VP”类型的句子,即由名词短语后跟动词短语的句子(例如'[约翰] [坐在长凳上]'、'[玛丽和吉尔] [吃了他们的三明治]'等等)
  • 简单句是英语语法中定义明确的概念。我不明白为什么需要在 SO 问题中定义它,尤其是标记为nlp 的问题。对于不参与 NLP 的读者,我想@JohnRambo 可以提供定义的链接(例如grammar.about.com/od/rs/g/simpsenterm.htm

标签: nlp extraction text-mining text-extraction information-extraction


【解决方案1】:

看看Apache OpenNLP,它有一个句子检测器模块。该文档包含如何从命令行和 API 使用它的示例。

【讨论】:

    【解决方案2】:

    我刚刚使用过 openNLP。

    public static List<String> breakIntoSentencesOpenNlp(String paragraph) throws FileNotFoundException, IOException,
            InvalidFormatException {
    
        InputStream is = new FileInputStream("resources/models/en-sent.bin");
        SentenceModel model = new SentenceModel(is);
        SentenceDetectorME sdetector = new SentenceDetectorME(model);
    
        String[] sentDetect = sdetector.sentDetect(paragraph);
        is.close();
        return Arrays.asList(sentDetect);
    }
    

    例子

        //Failed at Hi.
        paragraph = "Hi. How are you? This is Mike.";
        SentenceDetector.breakIntoSentencesOpenNlp(paragraph).forEach(sentence -> System.out.println(sentence));
    
        //Failed at Door.Noone
        paragraph = "Close the Door.Noone is out there";
        SentenceDetector.breakIntoSentencesOpenNlp(paragraph).forEach(sentence -> System.out.println(sentence));//not able to break on noone
    
        paragraph = "Really!! I cant believe. Mr. Wilson can come any moment to receive mrs. watson.";
        SentenceDetector.breakIntoSentencesOpenNlp(paragraph).forEach(sentence -> System.out.println(sentence));
    
        //Failed at dr.
        paragraph = "Radhika, Mohan, and Shaik went to meet dr. Kashyap to raise fund for poor patients.";
        SentenceDetector.breakIntoSentencesOpenNlp(paragraph).forEach(sentence -> System.out.println(sentence));//breaking on dr.
    
        paragraph = "This is how I tried to split a paragraph into a sentence. But, there is a problem. My paragraph includes dates like Jan.13, 2014 , words like U.S. and numbers like 2.2. They all got splitted by the above code.";
        SentenceDetector.breakIntoSentencesOpenNlp(paragraph).forEach(sentence -> System.out.println(sentence));//breaking on dr.
    
        paragraph = "www.thinkzarahatke.com is the second site I developed. You can send mail to admin@thinkzarahatke.com";
        SentenceDetector.breakIntoSentencesOpenNlp(paragraph).forEach(sentence -> System.out.println(sentence));
    

    只有在出现人为错误时才会失败。例如。 “博士。”缩写应大写D,2个句子之间至少要有1个空格。

    您也可以通过以下方式使用RE实现它;

    public static List<String> breakIntoSentencesCustomRESplitter(String paragraph){
        List<String> sentences = new ArrayList<String>();
        Pattern re = Pattern.compile("[^.!?\\s][^.!?]*(?:[.!?](?!['\"]?\\s|$)[^.!?]*)*[.!?]?['\"]?(?=\\s|$)", Pattern.MULTILINE | Pattern.COMMENTS);
        Matcher reMatcher = re.matcher(paragraph);
        while (reMatcher.find()) {
            sentences.add(reMatcher.group());
        }
        return sentences;
    
    }
    

    例子

        paragraph = "Hi. How are you? This is Mike.";
        SentenceDetector.breakIntoSentencesCustomRESplitter(paragraph).forEach(sentence -> System.out.println(sentence));
    
        //Failed at Door.Noone
        paragraph = "Close the Door.Noone is out there";
        SentenceDetector.breakIntoSentencesCustomRESplitter(paragraph).forEach(sentence -> System.out.println(sentence));
    
        //Failed at Mr., mrs.
        paragraph = "Really!! I cant believe. Mr. Wilson can come any moment to receive mrs. watson.";
        SentenceDetector.breakIntoSentencesCustomRESplitter(paragraph).forEach(sentence -> System.out.println(sentence));
    
        //Failed at dr.
        paragraph = "Radhika, Mohan, and Shaik went to meet dr. Kashyap to raise fund for poor patients.";
        SentenceDetector.breakIntoSentencesCustomRESplitter(paragraph).forEach(sentence -> System.out.println(sentence));
    
        //Failed at U.S.
        paragraph = "This is how I tried to split a paragraph into a sentence. But, there is a problem. My paragraph includes dates like Jan.13, 2014 , words like U.S. and numbers like 2.2. They all got splitted by the above code.";
        SentenceDetector.breakIntoSentencesCustomRESplitter(paragraph).forEach(sentence -> System.out.println(sentence));
    
        paragraph = "www.thinkzarahatke.com is the second site I developed. You can send mail to admin@thinkzarahatke.com";
        SentenceDetector.breakIntoSentencesCustomRESplitter(paragraph).forEach(sentence -> System.out.println(sentence));
    

    但是错误是有竞争力的。另一种方法是使用 BreakIterator;

    public static List<String> breakIntoSentencesBreakIterator(String paragraph){
        List<String> sentences = new ArrayList<String>();
        BreakIterator sentenceIterator =
                BreakIterator.getSentenceInstance(Locale.ENGLISH);
        BreakIterator sentenceInstance = sentenceIterator.getSentenceInstance();
        sentenceInstance.setText(paragraph);
    
        int end = sentenceInstance.last();
         for (int start = sentenceInstance.previous();
              start != BreakIterator.DONE;
              end = start, start = sentenceInstance.previous()) {
             sentences.add(paragraph.substring(start,end));
         }
    
         return sentences;
    }
    

    例子:

        paragraph = "Hi. How are you? This is Mike.";
        SentenceDetector.breakIntoSentencesBreakIterator(paragraph).forEach(sentence -> System.out.println(sentence));
    
        //Failed at Door.Noone
        paragraph = "Close the Door.Noone is out there";
        SentenceDetector.breakIntoSentencesBreakIterator(paragraph).forEach(sentence -> System.out.println(sentence));
    
        //Failed at Mr.
        paragraph = "Really!! I cant believe. Mr. Wilson can come any moment to receive mrs. watson.";
        SentenceDetector.breakIntoSentencesBreakIterator(paragraph).forEach(sentence -> System.out.println(sentence));
    
        //Failed at dr.
        paragraph = "Radhika, Mohan, and Shaik went to meet dr. Kashyap to raise fund for poor patients.";
        SentenceDetector.breakIntoSentencesBreakIterator(paragraph).forEach(sentence -> System.out.println(sentence));
    
    
        paragraph = "This is how I tried to split a paragraph into a sentence. But, there is a problem. My paragraph includes dates like Jan.13, 2014 , words like U.S. and numbers like 2.2. They all got splitted by the above code.";
        SentenceDetector.breakIntoSentencesBreakIterator(paragraph).forEach(sentence -> System.out.println(sentence));
    
        paragraph = "www.thinkzarahatke.com is the second site I developed. You can send mail to admin@thinkzarahatke.com";
        SentenceDetector.breakIntoSentencesBreakIterator(paragraph).forEach(sentence -> System.out.println(sentence));
    

    基准测试

    • 自定义 RE:7 毫秒
    • BreakIterator:143 毫秒
    • openNlp:255 毫秒

    【讨论】:

      猜你喜欢
      • 2014-06-05
      • 2023-03-11
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      相关资源
      最近更新 更多