gpt4 book ai didi

c# - 改进正则表达式以将大文本拆分为句子

转载 作者:行者123 更新时间:2023-12-02 09:47:31 30 4
gpt4 key购买 nike

Possible Duplicate:
What is a regular expression for parsing out individual sentences?

我想将大文本拆分成句子。我从答案 here 得到的正则表达式

string[] sentences = Regex.Split(mytext, @"(?<=[\.!\?])\s+");

所以我想到使用一种模式来进行分割,例如如果 . ? !遵循 space和一个capital 字母比做拆分。
大写字母表示句子的开头。

text = " Sentence one . Sentence e.g. two ? Sentence three.
sentence[1] = Sentence one
sentence[2] = Sentence e.g. two

对于有问题的情况,例如缩写,我打算进行替换

mytext.replace("e.g.","eg"); 

如何在正则表达式中实现这一点?

最佳答案

\p{Lt} 表示 Unicode 大写字母(包括重音符号等),因此

string[] sentences = Regex.Split(mytext, @"(?<=[.!?])\s+(?=\p{Lt})");

应该做你想做的事。

(请注意,我认为 .? 不需要在字符类中转义,因此我也删除了它们,但请检查这仍然是适用于这些角色。)

但是,请注意,这仍然会在例如先生。琼斯...

关于c# - 改进正则表达式以将大文本拆分为句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14254636/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com