如何将文本解析成句子

我试图将一个段落分解成句子。 这是我到目前为止的代码:

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. 拆分的每个句子都以空格开头。 如何删除冗余空间?

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

要获得更深入的见解和java库,请查看此链接http://nlp.stanford.edu/software/lex-parser.shtml,http://nlp.stanford.edu:8080 / parser / index.jsp和类似ruby语言的问题如何将一段文本解析成句子? (相当于Ruby)

例如:文字 –

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

标记后变为:

/ DT协议/ NNS的/ DT结果/ NN是/ VBZ至/ / JJ,/,因为/ IN / DT当前/ JJ税/ NN级别/ NNS签署/ VBN进/ IN法/ NN / IN President / NNP George / NNP W./NNP Bush / NNP expire / VBP on / RP Dec./NNP 31 / CD ./。 除非/ IN国会/ NNP行动/ VBZ,/,税/ NN率/ NNS / IN / / RB所有/ RB美国人/ NNPS谁/ WP支付/ VBP收入/ NN税/ NNS将/ MD上升/ VB上/ 1月/ NNP 1 / CD ./。 / DT可能/ MD影响/ VB经济/ JJ增长/ NN和/ CC甚至/ RB假期/ NN销售/ NNS ./。 解析

检查它如何区分句号(。)和12月31日之后的句号…

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

 "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..." 

您可以尝试使用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)); } 

修剪它……

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

您可以在此处使用此开源库提供的Class SentenceSplitter

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

首先修剪()你的字符串…并使用此链接

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

你也可以使用StringBuffer Class …只需使用这个链接我希望它能帮到你