我想用正则表达式(使用Ruby)将一些文本拆分成句子.它不需要准确,因此可以忽略诸如"华盛顿特区"之类的情况.
但是我要求如果引用句子(通过单引号或双引号),则应该忽略它.
说我有以下文字:
一句话."哇." 爱丽丝说.塞内斯三.
它应分为三句话:
一句话.
"哇." 爱丽丝说.
句子三.
目前我有content.scan(/[^\.!\?\n]*[\.!\?\n]/),但我的报价有问题.
更新:
目前的答案可能会遇到一些性能问题.请尝试以下方法:
'Alice stood besides the table. She looked towards the rabbit, "Wait! Stop!", said Alice'.scan(regexp)
Run Code Online (Sandbox Code Playgroud)
如果有人能弄清楚如何避免它会很好.谢谢!
这个怎么样:
result = subject.scan(
/(?: # Either match...
"[^"]*" # a quoted sentence
| # or
[^".!?]* # anything except quotes or punctuation.
)++ # Repeat as needed; avoid backtracking
[.!?\s]* # Then match optional punctuation characters and/or whitespace./x)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
281 次 |
| 最近记录: |