如何将文本解析成句子
我正在尝试将一个段落分成句子。这是到目前为止我的代码:
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]);
}
}
}
发现了两个问题:
- 每当遇到句点(“.”)符号时,代码就会分裂,即使它实际上是一个句子。我该如何防止这种情况?
- 拆分的每个句子都以空格开头。如何删除多余的空间?
I'm trying to break up a paragraph into sentences. Here is my code so far:
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]);
}
}
}
Two problems were found:
- The code splits anytime it comes to a period (".") symbol, even when it's actually one sentence. How do I prevent this?
- Each sentence that is split starts with a space. How do I delete the redundant space?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你提到的问题是一个NLP(自然语言处理)问题。编写一个粗略的规则引擎很好,但它可能无法扩展以支持完整的英文文本。
要获得更深入的了解和 Java 库,请查看此链接 http://nlp.stanford .edu/software/lex-parser.shtml , http://nlp .stanford.edu:8080/parser/index.jsp 和
ruby
语言的类似问题 如何将一段文本解析成句子? (最好是用 Ruby 语言)例如:
正文——
标记后变为:
检查它如何区分句号 (.) 和 Dec. 31 之后的句点 ...
The problem you mentioned is a NLP (Natural Language Processing) problem. It is fine to write a crude rule engine but it might not scale up to support full english text.
To have a deeper insight and a java library check out this link http://nlp.stanford.edu/software/lex-parser.shtml , http://nlp.stanford.edu:8080/parser/index.jsp and similar question for
ruby
language How do you parse a paragraph of text into sentences? (perferrably in Ruby)for example :
The text -
after tagging becomes :
Check how it has distinguished the full stop (.) and the period after Dec. 31 ...
您可以尝试使用
java.text.BreakIterator
类来解析句子。例如:You can try to use the
java.text.BreakIterator
class for parsing sentences. For example:第一个问题是一个很难正确解决的问题,因为您必须实现句子检测。我建议你不要这样做,只需在标点符号后用两个空行分隔句子即可。例如:
第二个可以使用 String.trim()。
例子:
The first one is a pretty hard problem to do properly, since you'd have to implement sentence detection. I suggest you don't do that, and just separate sentences with two blank lines after a punctuation mark. For example:
The second one can be solved using String.trim().
Example:
修剪它...
Trim it...
考虑到当前的输入格式,很难分割成句子。除了句号之外,您还必须施加一些规则附加规则来识别句子的结尾。例如,这条规则可以是“一个句子应该以句点(.)和两个空格结尾”。 (这就是 UNIX 工具
grep
识别句子的方式。Given the current input format, it will be difficult to split into sentences. You have to impose some rule additional rule to identify the end of a sentence, in addition to the period. For instance, this rule could be "a sentence should end with a period(.) and two spaces". (This is how the UNIX tool
grep
identifies sentences.您可以在此处使用此开源库提供的类
SentenceSplitter
< /a>.You can use the Class
SentenceSplitter
provided by this open source library here.首先 Trim() Your String... 并使用此链接
http://www. java-examples.com/java-string-split-example &http://www.rgagnon.com/javadetails/java-0438.html
你也可以使用 StringBuffer 类...只需使用此链接我希望它能帮助你
first Trim() Your String... and use this link
http://www.java-examples.com/java-string-split-example &http://www.rgagnon.com/javadetails/java-0438.html
and you can also use StringBuffer Class... just use this link i hope it will help you