waqarahmed/read-time

获取文章的预估阅读时间。类似于 medium.com 的 "x 分钟阅读"。

v1.1 2021-08-04 23:02 UTC

This package is auto-updated.

Last update: 2024-09-09 01:06:34 UTC


README

计算文章的阅读时间。

输出字符串

例如:x 分钟阅读5 分钟阅读

特性

  • 支持多语言翻译。
  • 静态方法以获取英文中的简单缩写输出字符串。
  • 静态方法以获取所需阅读给定文本的分钟数的整数。
  • 支持多语言和从右到左的语言。
  • 支持数组输出
  • 支持 JSON 输出

安装

使用 composer 安装

composer require waqarahmed/read-time

使用方法

静态方法

有两个静态方法 minRead(string $text)time(string $text)

minRead()

使用此方法生成简单的 x 分钟阅读 消息。它返回一个四舍五入的分钟数和一个 min read 消息。

$text = str_repeat('ad bc ', 251); //502 words in $text
echo ReadTime::minRead($text);

输出

2 分钟阅读

time()

time() 方法返回一个包含所需 分钟 数组的对象。

$text = str_repeat('ad bc ', 251); //502 words in $text
ReadTime::time($text);

输出

['minutes' => 2, 'seconds' => 12]

类方法

创建类的实例以使用

  • 翻译
  • 从右到左的语言支持
  • JSON 输出
  • 数组输出

构造函数()

构造函数接受并设置以下参数

public function __construct(
  string $text, 
  array $translation = null, 
  bool $abbreviate = true, 
  bool $rtl = false, 
  string $language = null,
  int $wordsPerMinute = 228
  )

类默认值

  • $wordsPerMinute 默认值为 200
  • $rtl 语言方向为从右到左,默认为 false
  • $translation 默认为 null,类默认输出英文
  • $abbreviate 默认缩写单词 'minute/minutes' 为 'min',默认为 true

getTime()

初始化新的类对象后,调用 getTime() 方法以获取结果。例如:4 分钟阅读1 分钟阅读 或缩写为 4 min read

setTextLanguge()

不同语言的阅读时间差异很大(S. Klosinski,K. Dietz)。类方法 setTextLanguage() 采用了从该研究得出的 17 种语言的预估阅读时间。

参考:"标准化阅读性能评估:新的国际阅读速度文本 IReST"

语言(ISO 编码)每分钟单词数

阿拉伯语(ar)138,中文(zh)158,荷兰语(nl)202,英语(en)228,芬兰语(fi)161,法语(fr)195,德语(de)179,希伯来语(he)187,意大利语(it)188,日语(jp)193,波兰语(pl)166,葡萄牙语(pt)181,俄语(ru)184,斯洛文尼亚语(sl)180,西班牙语(es)218,瑞典语(sv)199,土耳其语(tr)166。

默认语言为英语。通过传递两个字母(ISO 639-1)语言代码到 setTextLanguag() 方法设置不同的语言。

例如:将土耳其语设置为输入语言。

$text = str_repeat('ad bc ', 251); //502 words in $text
$result = new ReadTime($this->generateText(), ['minute' => 'dakika', 'minutes' => 'dakika', 'read' => 'okuman'], false, false, 'tr');
echo $result->getTime();

翻译

将翻译数组传递到类中,以设置单词 minuteminutesminread 的翻译。传递的数组必须是一个关联数组,包含任意数量的翻译字符串。

$translation 的默认属性

$translation = [
        'min'     => 'min',
        'minute'  => 'minute',
        'minutes' => 'minutes',
        'read'    => 'read',
    ];

示例翻译输入

$text = str_repeat('ad bc ', 251); //502 words in $text
$result = new ReadTime($this->generateText(), ['minute' => 'minuto', 'minutes' => 'minutos', 'read' => 'leer'], false);
echo $result->getTime();

西班牙语翻译输出:2 minutos leer

从右到左的语言翻译

$rtl 属性设置为 true 并传递从右到左书写的语言的 $translation

$text = str_repeat('ad bc ', 251);
$result = new ReadTime($this->generateText(), ['minute' => 'دقیقه', 'minutes' => 'دقایق', 'read' => 'خواندن'], false, true);
echo $result->getTime();

波斯语翻译输出:'خواندن دقایق 2'

getJSON()

获取计算出的阅读时间和类属性 JSON 输出的方法。

具有默认属性的类实例输出

$text = str_repeat('hello world ', 251);
$result = new ReadTime($text);
echo $result->getJSON();

输出

{
    "minutes": 2,
    "time": {
        "minutes": 2,
        "seconds": 12
    },
    "wordCount": 502,
    "translation": {
        "min": "min",
        "minute": "minute",
        "minutes": "minutes",
        "read": "read"
    },
    "abbreviate": true,
    "wordsPerMinute": 228
}

getArray()

获取计算读取时间和实例属性数组的函数。具有默认属性的类实例

$text = str_repeat('hello world ', 251);
$result = new ReadTime($text);
echo $result->getArray();

输出

array(6) {
  ["minutes"]=>
  int(2)
  ["time"]=>
  array(2) {
    ["minutes"]=>
    int(2)
    ["seconds"]=>
    int(12)
  }
  ["wordCount"]=>
  int(502)
  ["translation"]=>
  array(4) {
    ["min"]=>
    string(3) "min"
    ["minute"]=>
    string(6) "minute"
    ["minutes"]=>
    string(7) "minutes"
    ["read"]=>
    string(4) "read"
  }
  ["abbreviate"]=>
  bool(true)
  ["wordsPerMinute"]=>
  int(228)
}