2012-09-18 37 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