【问题标题】:How to parse text into sentences如何将文本解析为句子
【发布时间】:2011-05-21 09:23:49
【问题描述】:

我正在尝试将一个段落分成句子。到目前为止,这是我的代码:

import java.util.*;

public class StringSplit {
 public static void main(String args[]) throws Exception{
     String testString = "The outcome of the negotiations is vital, because the current tax levels signed into law by President George W. Bush expire on Dec. 31. Unless Congress acts, tax rates on virtually all Americans who pay income taxes will rise on Jan. 1. That could affect economic growth and even holiday sales.";
     String[] sentences = testString.split("[\\.\\!\\?]");
     for (int i=0;i<sentences.length;i++){  
         System.out.println(i);
      System.out.println(sentences[i]);  
     }  
 }
}

发现两个问题:

  1. 只要遇到句点(“.”)符号,代码就会拆分,即使它实际上是一个句子。如何防止这种情况发生?
  2. 分割的每个句子都以空格开头。如何删除多余的空间?

【问题讨论】:

    标签: java text-parsing


    【解决方案1】:

    Trim它...

    【讨论】:

      【解决方案2】:

      第一个问题很难正确解决,因为您必须实现句子检测。我建议你不要这样做,只是在标点符号后用两个空行分隔句子。例如:

      "The outcome of the negotiations is vital, because the current tax levels signed into law by President George W. Bush expire on Dec. 31.  Unless Congress acts, tax rates on virtually all Americans who pay income taxes will rise on Jan. 1.  That could affect economic growth and even holiday sales."
      

      第二个可以使用String.trim()解决。

      例子:

      String one = "   and now...    ";
      String two = one.trim();
      System.out.println(two);          // output: "and now..."
      

      【讨论】:

      • 第一个解决方案的问题是,在过去十年左右的时间里,已经从在句子之间插入两个空格转变为只插入一个。对于使用这种较新风格的写作,您的解决方案将不起作用。 :(
      【解决方案3】:

      鉴于当前的输入格式,将很难拆分成句子。除了句号之外,您还必须施加一些规则附加规则来识别句子的结尾。例如,此规则可以是“一个句子应该以句点 (.) 和两个空格结尾”。 (这就是 UNIX 工具 grep 识别句子的方式。

      【讨论】:

        【解决方案4】:

        您提到的问题是 NLP(自然语言处理)问题。编写一个粗略的规则引擎很好,但它可能无法扩展以支持完整的英文文本。

        要获得更深入的了解和 Java 库,请查看此链接 http://nlp.stanford.edu/software/lex-parser.shtmlhttp://nlp.stanford.edu:8080/parser/index.jspruby 语言 How do you parse a paragraph of text into sentences? (perferrably in Ruby) 的类似问题

        例如: 正文 -

        谈判的结果是 至关重要,因为当前的税收水平 乔治 W 总统签署成为法律。 布什将于 12 月 31 日到期。除非 国会法案,税率几乎 所有缴纳所得税的美国人 将在 1 月 1 日上升。这可能会影响 经济增长甚至假期 销售。

        标记后变为:

        /IN/DT的/DT结果/NN 谈判/NNS是/VBZ重要/JJ,/, 因为/IN/DT电流/JJ税/NN 级别/NNS签名/VBN成/IN法/NN 作者/IN 总裁/NNP George/NNP W./NNP 布什/NNP 到期/VBP on/RP Dec./NNP 31/CD./.除非/IN 国会/NNP 行为/VBZ ,/, 税/NN 税率/NNS on/IN 几乎/RB所有/RB美国人/NNPS 谁/WP 支付/VBP 收入/NN 税/NNS 将/MD升/VB on/IN Jan./NNP 1/CD ./.那/DT能/MD影响/VB 经济/JJ增长/NN和/CC偶/RB 假日/NN 销售/NNS ./.解析

        检查它如何区分句号 (.) 和 12 月 31 日之后的期间...

        【讨论】:

          【解决方案5】:

          首先 Trim() 你的字符串...然后使用这个链接

          http://www.java-examples.com/java-string-split-example&http://www.rgagnon.com/javadetails/java-0438.html

          您也可以使用 StringBuffer 类...只需使用此链接,希望对您有所帮助

          【讨论】:

            【解决方案6】:

            您可以尝试使用java.text.BreakIterator 类来解析句子。例如:

            BreakIterator border = BreakIterator.getSentenceInstance(Locale.US);
            border.setText(text);
            int start = border.first();
            //iterate, creating sentences out of all the Strings between the given boundaries
            for (int end = border.next(); end != BreakIterator.DONE; start = end, end = border.next()) {
                System.out.println(text.substring(start,end));
            }
            

            【讨论】:

            【解决方案7】:

            你可以使用这个开源库here提供的Class SentenceSplitter

            SentenceSplitter sp = new SentenceSplitter("filename");
            String str = null;
            while((str = sp.next().toString()) != null)
            {
                //Your code here.
            }
            

            【讨论】:

            • 在此 URL 没有可下载的内容。它返回“您无权访问此服务器上的 /page/download_view/。”
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2023-03-15
            • 1970-01-01
            • 2010-10-26
            • 1970-01-01
            • 1970-01-01
            • 2017-02-10
            • 1970-01-01
            相关资源
            最近更新 更多