Java 如何将文本解析成句子

Java 如何将文本解析成句子,java,text-parsing,Java,Text Parsing,我正试着把一段话分成几个句子。以下是我目前的代码: 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

我正试着把一段话分成几个句子。以下是我目前的代码:

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]);  
     }  
 }
}
import java.util.*;
公共类StringSplit{
公共静态void main(字符串args[])引发异常{
String testString=“谈判的结果至关重要,因为乔治·W·布什总统签署成为法律的现行税率将于12月31日到期。除非国会采取行动,否则几乎所有缴纳所得税的美国人的税率将于1月1日上调。这可能会影响经济增长,甚至影响假日销售。”;
String[]句=testString.split(“[\.\\!\\?]”);

对于(int i=0;iit…

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

"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 one = "   and now...    ";
String two = one.trim();
System.out.println(two);          // output: "and now..."

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

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

要深入了解java库,请查看此链接,以及有关
ruby
语言的类似问题

例如: 文本-

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

在标记变为:

/DT中/的/DT结果/NN 谈判/NNS is/VBZ vital/JJ,/, 因为/在/DT当前/JJ税/NN中 级别/NNS已签名/VBN进入/进入法律/NN 由/由主席/无国籍乔治/无国籍西/无国籍 布什/NNP到期/VBP月日/RP月日/NNP 31/CD./。除非/在国会/NNP acts/VBZ,/,税/NN费率/NNS on/IN 几乎/RB全部/RB美国人/NNPS who/WP pay/VBP收入/NN税收/NNS 将/MD上升/VB于1月/日/NNP 1/CD ./。那/DT可能/MD影响/VB 经济/JJ增长/NN和/CC偶数/RB 假日/NN销售/NNS./.Parse

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

第一次修剪()您的字符串…并使用此链接

&http://www.rgagnon.com/javadetails/java-0438.html


您也可以使用StringBuffer类…只需使用此链接,我希望它将帮助您

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

"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."
BreakIterator border=BreakIterator.getSentenceInstance(Locale.US);
border.setText(文本);
int start=border.first();
//迭代,用给定边界之间的所有字符串创建句子
for(int end=border.next();end!=BreakIterator.DONE;start=end,end=border.next()){
System.out.println(text.substring(start,end));
}

您可以使用这个开源库提供的类
语句拆分器


你的第一个解决方案的问题是,在过去十年左右的时间里,在句子之间插入两个空格变成了只插入一个空格。如果用这种较新的文体写作,你的解决方案就行不通了(
BreakIterator
是一个好主意,但它会遇到许多类似的问题。请参阅此问题:此URL上没有可下载的内容。它返回“您没有访问此服务器上的/page/download\u view/的权限。”