Mar*_*ers 12
您需要一个自然语言解析库.
例如,您可以使用SharpNLP,它是OpenNLP项目的C#端口.
SharpNLP是用C#编写的自然语言处理工具的集合.目前它提供以下NLP工具:
- 句子分割器
- 等等...
文章统计解析英语句子有一些关于如何在SharpNLP中安装和使用句子检测器的细节.下面将重复该文章中的示例代码作为预告片,但请阅读文档以获得有关可用功能及其使用方法的更完整说明.
using OpenNLP.Tools.SentenceDetect;
// ...
EnglishMaximumEntropySentenceDetector sentenceDetector =
new EnglishMaximumEntropySentenceDetector(mModelPath + "EnglishSD.nbin");
string[] sentences = sentenceDetector.SentenceDetect(input);
Run Code Online (Sandbox Code Playgroud)
如果你可以假设一个关于你的句子的简单规则,例如它们都在一个句号中结束,并且在一个句子的结尾处出现一个句号,那么你可以改为只计算文本中句点的数量.但请注意,英文文本通常不适合此模式,因为:
如果您已经安装了Word,则可以使用Word interop来获取句子计数以及其他统计信息.除了英语之外,这还有可能与其他语言一起使用.
object oMissing = System.Reflection.Missing.Value;
var oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
var oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Content.Text = inputTextBox.Text;
//get just sentence count
sentenceCountLabel.Text = oDoc.Sentences.Count.ToString();
//get all statistics
foreach (Microsoft.Office.Interop.Word.ReadabilityStatistic stat in oDoc.ReadabilityStatistics)
{
Console.WriteLine("{0}: {1}", stat.Name, stat.Value);
}
object oFalse = false;
oDoc.Close(ref oFalse, ref oMissing, ref oMissing);
Run Code Online (Sandbox Code Playgroud)
这将输出:
Words: 283
Characters: 1271
Paragraphs: 3
Sentences: 6
Sentences per Paragraph: 2
Words per Sentence: 47.1
Characters per Word: 4.3
Passive Sentences: 0
Flesch Reading Ease: 55.2
Flesch-Kincaid Grade Level: 12.5
Run Code Online (Sandbox Code Playgroud)
这可能不是最有效的,但它只需要几行代码,可能根据您的需要而适用.