Flesch-Kincaid计算器Jekyll插件所属类别及展示方案咨询
关于开发Jekyll阅读难度插件的问题
我打算开发我的第一个Jekyll插件,目标是利用Flesch-Kincaid阅读难度算法,给页面中每个<p>标签或带有[data-read-level]标记的元素添加数据属性。
Flesch阅读难度得分公式:
Flesch Reading Ease Score = 206.835 − 1.015 × ( Total Words / Total Sentences ) − 84.6 × ( Total Syllables / Total Words )
我有两个疑问:
- 这个插件属于Jekyll插件的哪一类别?我个人认为是generator类。
- 有没有比添加数据属性更好的结果展示方式?
我已经写了一个初步可用的JavaScript版本,目前仅支持英文,还需要进一步完善:
// Flesch Reading Ease Score = 206.835 − 1.015 × ( Total Words / Total Sentences ) − 84.6 × ( Total Syllables / Total Words ) let fkExamine = function() { window.addEventListener('DOMContentLoaded', init); const fkConstA = 206.835; const fkConstB = 1.015; const fkConstC = 84.6; function init() { console.log('-- fkExamine.js initiated'); let parseArray = [].slice.call(document.querySelectorAll('p, [data-read-level]')); parseArray.map(function(parseItem) { let txtContent = parseItem.textContent; let totalWords = getTotalWords(txtContent); let totalSentences = getTotalSentences(txtContent); let totalSyllables = getTotalSyllables(txtContent); let fkScore = fkConstA - fkConstB * (totalWords / totalSentences) - fkConstC * (totalSyllables / totalWords); parseItem.setAttribute('fk-score', fkScore); }); } function getTotalWords(input) { const words = input.split(' '); return words.filter(word => word !== '').length; } function getTotalSentences(input) { return input.match(/[\w|\)][.?!](\s|$)/g).length; } function getTotalSyllables(input) { let syllables = 0; let words = input.split(' '); words = words.filter(word => word !== ''); for(let i = 0; i < words.length; i++) { words[i].toLowerCase(); words[i].replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, ''); words[i].replace(/^y/, ''); syllables += words[i].match(/[aeiouy]{1,2}/g).length; } return syllables; } }();
内容的提问来源于stack exchange,提问作者Moxxi
相关产品推荐
相关产品推荐

