2012-09-18 73 views
1

像這樣一開始我有一串字符串在我的數據庫:紅寶石文本分析:尋找下一個句子

下班開車回家。狗在沙發上跳到了他的主人 。他舔了舔他的臉。

字符串從句子中間開始。我想找一個方法來切斷最初的不完整句子,然後從「沙發上的狗跳到他的主人在門口,他舔乾淨他的臉。」

我該怎麼做?

回答

1

問題是如何定義不完整的句子。我們可以假定所有以upcased character開頭的句子都是完整的句子。如果是這樣的代碼可能看起來像這樣

str = 'driving home from work. The dog leaped of the sofa to great his master at the door. He licked his face clean.' 
sentences = str.split('.') 
sentences.shift if sentences[0][0].downcase == sentences[0][0] 
sentences.join('.').strip << '.' 

有點棘手,但工程。

0

也許這樣的事情?

str = "driving home from work. The dog leaped of the sofa to great his master at the door. He licked his face clean." 
str.first == str.first.upcase ? str : str.split(".")[1..-1].join(".").lstrip << "." 

假設它以大寫字母開頭表示句子的開頭,否則它是不可能的。其他情況下要考慮,如果它以數字開頭呢?例如:1只狗跑了。狗...是1狗...一句話?

1

最簡單的答案:

str = 'driving home from work. The dog leaped of the sofa to great his master at the door. He licked his face clean.' 
str.sub!(/^[^A-Z].+?\./, '').strip! 
+0

這將刪除第一句話不管......並留下空白後面。 – iouri

+0

現在已修復。只有當文本不以大寫字母開頭到下一個點時纔會刪除。同時'strip!'將刪除所有初始和隨後的空格。 – Hauleth

+0

我喜歡它,簡單易記。並且它處理第一句以數字,符號等開頭的情況。 – iouri