【问题标题】:Split a Sentences String into sentence per line in Java [closed]在Java中将句子字符串拆分为每行的句子[关闭]
【发布时间】:2017-06-05 10:40:09
【问题描述】:

我想在 Java 中将句子分成每行一个句子。

输入字符串: “由于投资者权衡美国总统大选、经济状况加强和利率上升的潜在影响,市政债券市场在本财年上半年再次出现波动。市场受到市政债券创纪录水平的进一步压力2016 年发行。在此背景下,所有六只基金均出现下跌,从 American Funds 短期免税债券基金的 –0.92% 到美国高收入市政债券基金的 –3.77%。(见第 4 页至第 10 页资助具体的结果和信息。)”

输出:

句子 1:由于投资者权衡了美国总统大选、经济状况转强和利率上升的潜在影响,市政债券市场在本财年上半年再次出现波动。

句子 2:2016 年市政债券发行量创历史新高,进一步打压市场。在此背景下,所有六只基金均录得下跌,从 American Funds 短期免税债券基金的 –0.92% 到 –3.77美国高收入市政债券基金的百分比。

句子 3:(有关基金的具体结果和信息,请参见第 4 至 10 页。

我编写了一个 java 代码来在出现 .('Full stop') 时拆分句子,在 U.S. 之后出现新行

string = string.replace(".", ".\n")

【问题讨论】:

  • 到目前为止你做了什么?您面临哪些问题?
  • 这个问题有什么过于宽泛的地方?已经描述了一个问题,并试图解决它。我猜有些人患有 TL;DR 综合征。

标签: java string


【解决方案1】:

您可以使用String::split 和正则表达式来完成此操作:

String[] sentences = paragraph.split("(?<=[^ ]\\.) (?=[^a-z])");
int count = 0;
for(String str:sentences)
    System.out.println("Sentence " + (++count) + ":" + str);

这使用称为前瞻和后视的高级正则表达式技术在匹配时保留分隔符。

【讨论】:

  • 不工作。时分裂。由于投资者权衡了 U .年代。总统选举、经济状况改善和利率上升。市场受到 2016 年创纪录的市政债券发行的进一步压力。在此背景下,所有六只基金均出现下跌,从 American Funds 短期免税债券基金的 –0.92% 到 American High-收入市政债券基金......
  • 对于给定它有效的测试用例,但我将对其进行编辑并在句点之前添加一个空格
  • @SurjitPatra 立即尝试
【解决方案2】:

String#split() 采用正则表达式。在正则表达式中,. 表示除\n 之外的任何内容。使用\ 转义点,因此生成的参数变为\\.

【讨论】:

  • 这不能解决他关于“美国”的问题虽然匹配作为句子的结尾
  • 他们匹配一个点后跟一个空格,这实际上会匹配句子,而不是U.S.,,但第一个答案看起来要好得多,所以我推荐那个。
  • “权衡美国总统大选的潜在影响”“U.S.”后面没有逗号
  • 啊哈,没看到这个。感谢您指出这一点。
【解决方案3】:

在你的代码中尝试这样的事情:

List<String> eachLine = new ArrayList<String>();
String initialString = new String("Volatility returned to the municipal bond market during the first half of the funds’ fiscal year as investors weighed the potential impact of the U.S. presidential election, strengthening economic conditions and rising interest rates. The market was further pressured by a record level of municipal bond issuance in 2016. Against this backdrop, all six funds registered declines, ranging from –0.92% for American Funds Short-Term Tax-Exempt Bond Fund to –3.77% for American High-Income Municipal Bond Fund. (See pages 4 through 10 for fund specific results and information.)");

int stopIndex = initialString.indexOf( '. ' );//I am searching for the first occurance of '. ' in the string. 
//Note full stop followed blank space, which would denote either end of a sentence or words like U.K. or U.S. etc.

boolean UpperCase = checkForUpperCase(stopIndex+1);//write a function to check whether the alphabet/character following '. ' is in uppercase or not
//checking for Uppercase because a senetence starts with Uppercase
if(UpperCase){
   eachLine.add(initialString.substring(0,stopIndex));//add the sentence to List<String> to be processed later
   initialString = initialString.substring(stopIndex+1);//storing the rest of the sentence in the same string to be processed again
}
//keep parsing till you parse the whole string

您可以从这里大致了解如何检查大写:Java Program to test if a character is uppercase/lowercase/number/vowel

上述代码只是一个 sn-p,可让您了解如何实现目标或解决问题。

您也可以使用正则表达式来查找句号模式,但了解基本方法可能会在以后更有用。

Java 中的正则表达式:https://www.tutorialspoint.com/java/java_regular_expressions.htm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2014-04-23
    • 2020-06-14
    • 1970-01-01
    相关资源
    最近更新 更多