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 成本估算準確高