pmp 成本估算准确高
by Pritish Vaidya
通过Pritish Vaidya
准确估算JavaScript中篇文章的阅读时间 (Accurate estimation of read time for Medium articles in JavaScript)
介绍 (Introduction)
Read Time Estimate is the estimation of the time taken by the reader to read an article. It has been a part of Medium’s core features since it launched in 2013.
阅读时间估算是读者阅读文章所花费时间的估算。 自2013年推出以来,它一直是Medium核心功能的一部分。
As explained in the New Yorker:
如《纽约客》中所述 :
The more we know about something — including precisely how much time it will consume — the greater the chance we will commit to it.
我们对某事了解得越多-确切地包括它将消耗多少时间-我们致力于该事的机会就越大。
Knowing in advance how long an article will take to read helps with better time management by allowing us to plan further ahead.
提前知道一篇文章将花费多长时间,可以使我们提前计划,从而有助于更好地管理时间。
为什么要使用新脚本? (Why should I use a new script?)
Yes, there are many open source libraries available on npm but they contain several flaws.
是的, npm上有很多开源库 但它们包含一些缺陷。
Before that, let’s take a look at these two articles on Medium.
在此之前,让我们看一下有关Medium的这两篇文章。
Read Time — Medium Support
读取时间-中度支持
Read Time and You
阅读时间和你
The above two articles have the following key features
上面的两篇文章具有以下主要功能
- Average Read Time (English) — 265 Words per min 平均阅读时间(英语)—每分钟265个单词
- Average Read Time (Chinese, Japanese and Korean) — 500 Characters/min 平均阅读时间(中文,日文和韩文)-500个字符/分钟
- Image Read Time — 12 seconds for the first image, 11 for the second, and minus an additional second for each subsequent image. Other images counted at 3 seconds. 图像读取时间-第一张图像12秒钟,第二张图像11秒钟,每个后续图像再减去一秒钟。 其他图像计数为3秒。
Most of the libraries don’t account for the above features completely. They use HTML strings as is without omitting its tag names which increases the deviation of estimation from the original value.
大多数库并未完全说明上述功能。 他们按原样使用HTML字符串,而不会省略其标签名 ,这会增加估计值与原始值的偏差。
码 (Code)
The code can be split into three parts:
该代码可以分为三部分:
- Constants 常数
- Utility 效用
- Main 主要
常数 (Constants)
The constants can be used as defaults to the main function. The image tag has its own use which will be defined later.
常量可以用作主函数的默认值。 image标签有其自己的用途,稍后将进行定义。
实用功能 (Utility Functions)
Strip WhiteSpace
带空格
It is a simple utility function to remove all leading and trailing whitespace from the string provided.
这是一个简单的实用程序函数,可从提供的字符串中删除所有前导和尾随空格。
2. Image Read Time
2.图像读取时间
It parses the string, looks for any HTML image tags based on the defaults provided in the constants and returns the count.
它解析该字符串,根据常量中提供的默认值查找任何HTML图像标签,然后返回计数。
If the image count is greater than 10, we calculate the image read time of the first 10 images in decreasing arithmetic progression starting from 12 sec / customReadTime
provided by the user using the simple formula n * (a+b) / 2
and 3 sec for the remaining images.
如果图像数量大于10,我们将使用简单的公式n * (a+b) / 2
和3秒,从12秒/用户提供的customReadTime
开始,以递减的算术级数计算前10张图像的图像读取时间。剩下的图像。
3. Strip Tags
3.剥离标签
Next, we check for any HTML tags (both) in the string and remove it to extract only the words from it.
接下来,我们检查字符串中是否有HTML标记(均为HTML标记),并将其删除以仅提取其中的单词。
4. Words Read Time
4.单词阅读时间
This utility function calculates the words count and Chinese / Korean and Japanese characters using the different Unicode character range.
该实用程序功能使用不同的Unicode字符范围来计算单词数以及中文/韩文和日文字符。
The time is calculated by dividing it with the constants defined above.
通过将时间除以上面定义的常数来计算时间。
5. Humanize Time
5.人性化的时间
Based on the distance of time in words, we can calculate and return the humanized duration of the time taken to read.
根据时间(以字为单位)的距离 ,我们可以计算并返回读取时间的人性化持续时间。
主要 (Main)
The main function only consolidates all the utility methods in the correct order.
main函数仅以正确的顺序合并所有实用程序方法。
这个脚本的准确性如何? (How accurate is this script?)
Taking the tests on the HTML string (from the Chrome inspector) before this article section.
在本文开始之前 ,请对HTML字符串(来自Chrome检查器) 进行测试。
The tests and the Pages clearly give the correct estimate about the total words from the parsed HTML and the number of images.
测试和页面清楚地给出了来自解析HTML的单词总数和图像数量的正确估计。
链接 (Links)
I’ve consolidated the complete code on GitHub. It is also available as an npm package read-time-estimate.
我已经在GitHub上整合了完整的代码。 它也可以作为npm包read-time-estimate提供 。
More of the cool stuff can be found on my StackOverflow and GitHub profiles.
在我的StackOverflow和GitHub个人资料中可以找到更多有趣的东西。
Follow me on LinkedIn, Medium, Twitter for further update new articles.
在LinkedIn , Medium和Twitter上关注我,以获取更多更新的新文章。
One clap, two claps, three claps, forty?
一拍,两拍,三拍,四十?
Originally published at blog.pritishvaidya.com on January 30, 2019.
最初于2019年1月30日发布在blog.pritishvaidya.com上。
翻译自: https://www.freecodecamp.org/news/how-to-more-accurately-estimate-read-time-for-medium-articles-in-javascript-fb563ff0282a/
pmp 成本估算准确高