2017-06-05 230 views
-1

我想在Java中將句子拆分爲每行一個句子。在Java中將句子字符串拆分爲每行句子

輸入字符串: 「投資者考慮到美國總統大選的潛在影響,加強經濟狀況和利率上升,在本財年上半年迴歸市政債券市場。受到2016年市政債券發行創紀錄水平的推動。在此背景下,所有六隻基金均出現下跌,其中美國基金短期免稅債券基金的-0.92%至美國高收益市政債券基金的-3.77% (對於特定基金結果和信息,請參見第4頁至10。)」

輸出:

第1句話:投資者考慮到美國總統大選的潛在影響,加強經濟狀況和利率上升,在本財政年度上半年重返市政債券市場。

句子2:市場受到2016年市政債券發行創紀錄水平的進一步壓力。在此背景下,所有六隻基金均下跌,範圍從美國基金短期免稅債券基金的-0.92%至 - 3.77%爲美國高收入市政債券基金。

Sentence3:(基金具體的結果和信息,請參閱第4頁至10

我寫了一個Java代碼拆分句子時,(「句號」)發生時,新的生產線已後到來。美國

字符串=與string.replace( 「」 「\ n」)

+0

到目前爲止你做了什麼?你面臨什麼問題? –

+0

https://stackoverflow.com/a/4373687/102834 – FireAphis

+0

這個問題太廣泛了嗎?已經描述了一個問題,並試圖解決它。我想有些人有TL; DR綜合徵。 –

回答

1

你可以使用String::split用正則表達式來做到這一點,像這樣:

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

這使用先進的正則表達式技術稱爲向前看,並在後面保留匹配時的分隔符。

+0

不工作。拆分時。發生。由於投資者衡量U的潛在影響,該基金財政年度上半年的市場債券市場波動回升。 S。總統選舉,加強經濟條件和加息。 2016年市場債券發行量創歷史新高。 在此背景下,所有六隻基金均出現下跌,其中美國基金短期免稅債券基金的-0.92%至美國的-3.77%高收入市政債券基金.... –

+0

對於給定的測試案例,但我會編輯它並在測試期間添加一個空格 – CraigR8806

+0

@SurjitPatra現在試試吧 – CraigR8806

0

String#split()需要一個正則表達式。在正則表達式中,.表示除\n以外的任何其他值。逃生使用\點,因此產生的參數變得\\.

+0

這不會解決他的問題問題與「美國」匹配作爲一個句子盡頭 – CraigR8806

+0

他們匹配一個點後跟一個空格,這實際上會匹配句子,而不是「美國」,但第一個答案看起來好多了,所以我建議一個。 – ArsenArsen

+0

*「權衡美國總統大選的潛在影響」*「美國」之後沒有逗號, – CraigR8806

0

試試你的代碼中是這樣的:

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

上述代碼只是一個片段,可以幫助您瞭解如何實現目標或解決問題。

您也可以使用正則表達式來查找完整停止模式,但瞭解基本方法可能在以後更加有用。

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