org_heigl/textstatistics

从给定文本获取统计数据

0.2.1 2018-02-16 16:58 UTC

README

计算文本统计数据,包括音节、Flesch-Reading-Ease(英语和德语)等。

Build Status Scrutinizer Code Quality Code Climate StyleCI Coverage Status

Latest Stable Version Total Downloads License composer.lock

原因

另一个实现 davechild/textstatistics 很遗憾仅实现了英语文本的统计。这对于例如德语带重音符号的文本来说不起作用。因此,我决定使用我为 hyphenator 所做的工作再次实现一些算法。

这就是为什么例如音节计算会有所不同。

安装

最好使用 composer 安装 TextStatistics。

$ composer require org_heigl/textstatistics

用法

不同的计算器都实现了共同的 CalculatorInterface,因此都提供了一个期望包含要计算的文本的 Text 对象的 calculate 方法。

目前可用的这些统计数据

  • 平均句子长度
  • 平均每词音节数
  • 字符计数(包括空白字符)
  • 字符计数(不包括空白字符)
  • Flesch-Kincaid 评分等级
  • 英语文本的 Flesch-Reading-Ease
  • 德语文本的 Flesch-Reading-Ease
  • Flesch-Reading-Ease 学校评分测量
  • 句子数
  • 句子中最大音节数
  • 句子中最大词数
  • 音节数
  • Wiener Sachtext-Formel 1, 2, 3 和 4
  • 词数
  • 单词中最大音节数
  • 至少 N 个字符的单词数
  • 至少 N 个字符的单词百分比
  • 至少 N 个音节的单词数
  • 至少 N 个音节的单词百分比
  • 仅包含 N 个音节的单词数
  • 仅包含 N 个音节的单词百分比

每个统计数据都有工厂方法,因此获取某个统计数据需要以下代码行

$text = new \Org_Heigl\TextStatistics\Text($theText);
$wordCount =\Org_Heigl\TextStatistics\Service\WordCounterFactory::getCalculator()->calculate($text);
$fleschReadingEase = /Org_Heigl\TextStatistics\Service\FleschReadingEaseCalculatorFactory::getCalculator()->calculate($text);

您还可以向 TextStatisticsGenerator 添加多个计算器,并一次性检索多个统计数据,如下所示

$text = new \Org_Heigl\TextStatistics\Text($theText);

$statGenerator = new \Org_Heigl\TextStatistics\TextSTatisticsGenerator();
$statGenerator->add('wordCount', \Org_Heigl\TextStatistics\Service\WordCounterFactory::getCalculator());
$statGenerator->add('flesch', \Org_Heigl\TextStatistics\Service\FleschReadingEaseCalculatorFactory::getCalculator());

print_R($statGenerator->calculate($text));

// array(
//    'wordCount' => xx,
//    'flesch' => yy,
// )