使用JavaScript Glob函数
我有这个脚本(下面),我想在页面上使用。这是一个简单的客户端页面,其中包含预定义的文本块,我希望通过这些函数传递这些文本块。然而,由于这是一个函数(glob),我知道它会立即执行。我不知道如何将文本传递到各种函数中。我该怎么办使用JavaScript Glob函数,javascript,Javascript,我有这个脚本(下面),我想在页面上使用。这是一个简单的客户端页面,其中包含预定义的文本块,我希望通过这些函数传递这些文本块。然而,由于这是一个函数(glob),我知道它会立即执行。我不知道如何将文本传递到各种函数中。我该怎么办 <script type="text/javascript"> // TextStatistics.js // Christopher Giffard (2012) // 1:1 API Fork of TextStatisti
<script type="text/javascript">
// TextStatistics.js
// Christopher Giffard (2012)
// 1:1 API Fork of TextStatistics.php by Dave Child (Thanks mate!)
// https://github.com/DaveChild/Text-Statistics
(function(glob) {
function cleanText(text) {
// all these tags should be preceeded by a full stop.
var fullStopTags = ['li', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'dd'];
fullStopTags.forEach(function(tag) {
text = text.replace("</" + tag + ">",".");
})
text = text
.replace(/<[^>]+>/g, "") // Strip tags
.replace(/[,:;()\-]/, " ") // Replace commans, hyphens etc (count them as spaces)
.replace(/[\.!?]/, ".") // Unify terminators
.replace(/^\s+/,"") // Strip leading whitespace
.replace(/[ ]*(\n|\r\n|\r)[ ]*/," ") // Replace new lines with spaces
.replace(/([\.])[\. ]+/,".") // Check for duplicated terminators
.replace(/[ ]*([\.])/,". ") // Pad sentence terminators
.replace(/\s+/," ") // Remove multiple spaces
.replace(/\s+$/,""); // Strip trailing whitespace
text += "."; // Add final terminator, just in case it's missing.
return text;
}
var TextStatistics = function TextStatistics(text) {
this.text = text ? cleanText(text) : this.text;
};
TextStatistics.prototype.fleschKincaidReadingEase = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round((206.835 - (1.015 * this.averageWordsPerSentence(text)) - (84.6 * this.averageSyllablesPerWord(text)))*10)/10;
};
TextStatistics.prototype.fleschKincaidGradeLevel = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round(((0.39 * this.averageWordsPerSentence(text)) + (11.8 * this.averageSyllablesPerWord(text)) - 15.59)*10)/10;
};
TextStatistics.prototype.gunningFogScore = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round(((this.averageWordsPerSentence(text) + this.percentageWordsWithThreeSyllables(text, false)) * 0.4)*10)/10;
};
TextStatistics.prototype.colemanLiauIndex = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round(((5.89 * (this.letterCount(text) / this.wordCount(text))) - (0.3 * (this.sentenceCount(text) / this.wordCount(text))) - 15.8 ) *10)/10;
};
TextStatistics.prototype.smogIndex = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round(1.043 * Math.sqrt((this.wordsWithThreeSyllables(text) * (30 / this.sentenceCount(text))) + 3.1291)*10)/10;
};
TextStatistics.prototype.automatedReadabilityIndex = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round(((4.71 * (this.letterCount(text) / this.wordCount(text))) + (0.5 * (this.wordCount(text) / this.sentenceCount(text))) - 21.43)*10)/10;
};
TextStatistics.prototype.textLength = function(text) {
text = text ? cleanText(text) : this.text;
return text.length;
};
TextStatistics.prototype.letterCount = function(text) {
text = text ? cleanText(text) : this.text;
text = text.replace(/[^a-z]+/ig,"");
return text.length;
};
TextStatistics.prototype.sentenceCount = function(text) {
text = text ? cleanText(text) : this.text;
// Will be tripped up by "Mr." or "U.K.". Not a major concern at this point.
return text.replace(/[^\.!?]/g, '').length || 1;
};
TextStatistics.prototype.wordCount = function(text) {
text = text ? cleanText(text) : this.text;
return text.split(/[^a-z0-9]+/i).length || 1;
};
TextStatistics.prototype.averageWordsPerSentence = function(text) {
text = text ? cleanText(text) : this.text;
return this.wordCount(text) / this.sentenceCount(text);
};
TextStatistics.prototype.averageSyllablesPerWord = function(text) {
text = text ? cleanText(text) : this.text;
var syllableCount = 0, wordCount = this.wordCount(text), self = this;
text.split(/\s+/).forEach(function(word) {
syllableCount += self.syllableCount(word);
});
// Prevent NaN...
return (syllableCount||1) / (wordCount||1);
};
TextStatistics.prototype.wordsWithThreeSyllables = function(text, countProperNouns) {
text = text ? cleanText(text) : this.text;
var longWordCount = 0, self = this;
countProperNouns = countProperNouns === false ? false : true;
text.split(/\s+/).forEach(function(word) {
// We don't count proper nouns or capitalised words if the countProperNouns attribute is set.
// Defaults to true.
if (!word.match(/^[A-Z]/) || countProperNouns) {
if (self.syllableCount(word) > 2) longWordCount ++;
}
});
return longWordCount;
};
TextStatistics.prototype.percentageWordsWithThreeSyllables = function(text, countProperNouns) {
text = text ? cleanText(text) : this.text;
return (this.wordsWithThreeSyllables(text,countProperNouns) / this.wordCount(text)) * 100;
};
TextStatistics.prototype.syllableCount = function(word) {
var syllableCount = 0,
prefixSuffixCount = 0,
wordPartCount = 0;
// Prepare word - make lower case and remove non-word characters
word = word.toLowerCase().replace(/[^a-z]/g,"");
// Specific common exceptions that don't follow the rule set below are handled individually
// Array of problem words (with word as key, syllable count as value)
var problemWords = {
"simile": 3,
"forever": 3,
"shoreline": 2
};
// Return if we've hit one of those...
if (problemWords[word]) return problemWords[word];
// These syllables would be counted as two but should be one
var subSyllables = [
/cial/,
/tia/,
/cius/,
/cious/,
/giu/,
/ion/,
/iou/,
/sia$/,
/[^aeiuoyt]{2,}ed$/,
/.ely$/,
/[cg]h?e[rsd]?$/,
/rved?$/,
/[aeiouy][dt]es?$/,
/[aeiouy][^aeiouydt]e[rsd]?$/,
/^[dr]e[aeiou][^aeiou]+$/, // Sorts out deal, deign etc
/[aeiouy]rse$/ // Purse, hearse
];
// These syllables would be counted as one but should be two
var addSyllables = [
/ia/,
/riet/,
/dien/,
/iu/,
/io/,
/ii/,
/[aeiouym]bl$/,
/[aeiou]{3}/,
/^mc/,
/ism$/,
/([^aeiouy])\1l$/,
/[^l]lien/,
/^coa[dglx]./,
/[^gq]ua[^auieo]/,
/dnt$/,
/uity$/,
/ie(r|st)$/
];
// Single syllable prefixes and suffixes
var prefixSuffix = [
/^un/,
/^fore/,
/ly$/,
/less$/,
/ful$/,
/ers?$/,
/ings?$/
];
// Remove prefixes and suffixes and count how many were taken
prefixSuffix.forEach(function(regex) {
if (word.match(regex)) {
word = word.replace(regex,"");
prefixSuffixCount ++;
}
});
wordPartCount = word
.split(/[^aeiouy]+/ig)
.filter(function(wordPart) {
return !!wordPart.replace(/\s+/ig,"").length
})
.length;
// Get preliminary syllable count...
syllableCount = wordPartCount + prefixSuffixCount;
// Some syllables do not follow normal rules - check for them
subSyllables.forEach(function(syllable) {
if (word.match(syllable)) syllableCount --;
});
addSyllables.forEach(function(syllable) {
if (word.match(syllable)) syllableCount ++;
});
return syllableCount || 1;
};
function textStatistics(text) {
return new TextStatistics(text);
}
(typeof module != "undefined" && module.exports) ? (module.exports = textStatistics) : (typeof define != "undefined" ? (define("textstatistics", [], function() { return textStatistics; })) : (glob.textstatistics = textStatistics));
})(this);
//TextStatistics.js
//克里斯托弗·吉法德(2012)
//Dave Child的TextStatistics.php的1:1 API分支(谢谢,伙计!)
// https://github.com/DaveChild/Text-Statistics
(功能(全球){
函数cleanText(文本){
//所有这些标签前面都应该有一个句号。
var fullStopTags=['li','p','h1','h2','h3','h4','h5','h6','dd'];
fullStopTags.forEach(函数(标记){
text=text.replace(“,”);
})
文本=文本
.replace(/]+>/g,“”//带标签
.replace(//[,:;()\-]/,“”)//替换逗号、连字符等(将它们计为空格)
.替换(/[\.!?]/,“)//统一终端
.replace(/^\s+/,“”)//去掉前导空格
.replace(//[]*(\n |\r\n |\r)[]*/,“”)//用空格替换新行
.replace(/([\.])[\.]+/,“)//检查是否有重复的终止符
.replace(//[]*([\.]])/,“)///Pad语句终止符
.replace(//\s+/,“”)//删除多个空格
.replace(//\s+$/,“”);//去掉尾随的空格
text+=“;//添加最终终止符,以防丢失。
返回文本;
}
var TextStatistics=函数TextStatistics(text){
this.text=text?cleanText(text):this.text;
};
TextStatistics.prototype.FleschkinCaiDaredingEase=函数(文本){
text=text?cleanText(text):this.text;
返回Math.round((206.835-(1.015*this.averageWordsPerContent(text))-(84.6*this.averageCellelesPerWord(text)))*10;
};
TextStatistics.prototype.fleschKincaidGradeLevel=函数(文本){
text=text?cleanText(text):this.text;
返回Math.round((0.39*this.averageWordsPerContent(text))+(11.8*this.averageCellelesPerWord(text))-15.59)*10)/10;
};
TextStatistics.prototype.gunningFogScore=函数(文本){
text=text?cleanText(text):this.text;
返回Math.round(((this.averageWordsPersence(text)+this.percentageWords,带三个音节(text,false))*0.4)*10)/10;
};
TextStatistics.prototype.colemanLiauIndex=函数(文本){
text=text?cleanText(text):this.text;
返回Math.round(((5.89*(this.letterCount(text)/this.wordCount(text))-(0.3*(this.sentenceCount(text)/this.wordCount(text)))-15.8)*10)/10;
};
TextStatistics.prototype.smogIndex=函数(文本){
text=text?cleanText(text):this.text;
返回Math.round(1.043*Math.sqrt((这个单词有三个音节(文本)*(30/这个句子计数(文本))+3.1291)*10)/10;
};
TextStatistics.prototype.automatedReadabilityIndex=函数(文本){
text=text?cleanText(text):this.text;
返回Math.round((4.71*(this.letterCount(text)/this.wordCount(text))+(0.5*(this.wordCount(text)/this.sentenceCount(text))-21.43)*10)/10;
};
TextStatistics.prototype.textLength=函数(文本){
text=text?cleanText(text):this.text;
返回text.length;
};
TextStatistics.prototype.leteCount=函数(文本){
text=text?cleanText(text):this.text;
text=text。替换(/[^a-z]+//ig,”);
返回text.length;
};
TextStatistics.prototype.sentenceCount=函数(文本){
text=text?cleanText(text):this.text;
//会被“Mr.”或“U.K.”绊倒。这不是主要问题。
返回文本。替换(/[^\.!?]/g',)。长度| | 1;
};
TextStatistics.prototype.wordCount=函数(文本){
text=text?cleanText(text):this.text;
返回文本。拆分(/[^a-z0-9]+/i)。长度| | 1;
};
TextStatistics.prototype.AverageWordsPerContent=函数(文本){
text=text?cleanText(text):this.text;
返回this.wordCount(text)/this.sentenceCount(text);
};
TextStatistics.prototype.AverageSydellesPerWord=函数(文本){
text=text?cleanText(text):this.text;
var-syllecount=0,wordCount=this.wordCount(text),self=this;
text.split(/\s+/).forEach(函数(word){
音节计数+=自身。音节计数(单词);
});
//防止南。。。
返回(音节计数1)/(字数计数1);
};
TextStatistics.prototype.WordsWithThree音节=函数(文本,countProperNouns){
text=text?cleanText(text):this.text;
var longWordCount=0,self=this;
countProperNouns=countProperNouns===false?false:true;
text.split(/\s+/).forEach(函数(word){
//如果设置了countProperNouns属性,则不计算专有名词或大写单词。
//默认为true。
如果(!word.match(/^[A-Z]/)| | countProperNouns){
if(self.syllecount(word)>2)longWordCount++;
}
});
返回longWordCount;
};
TextStatistics.prototype.PercentageWords三个音节=函数(文本,CountProperNuns){
text=text?cleanText(text):this.text;
var stat = new textstatistics('Your text here');
console.log(stat.fleschKincaidReadingEase());
console.log(stat.fleschKincaidGradeLevel());
// or
console.log(stat.sentenceCount('This. is. a. long. sentence.'));
<div id="my-div"></div>
var stat = new textstatistics('Your text here');
var div = document.getElementById('my-div');
div.innerHTML = div.innerHTML + stat.sentenceCount('This. is. a. long. sentence.');