text mostFrequentWord分析仪挑战
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text mostFrequentWord分析仪挑战相关的知识,希望对你有一定的参考价值。
"Regular expressions (regex or regexp) are extremely useful in extracting information from any text by searching for one or more matches of a specific search pattern (i.e. a specific sequence of ASCII or unicode characters)."
So, the regular expression in this code works as a function called getTokens that takes one argument called rawString.
The getTokens function executes logic that basically manipulates a string and filters it in order to return an array of strings that reflect the specific information extracted by the getTokens function.
First, getTokens takes the string and converts the characters in the string to lowercase by applying the toLowerCase() string method.
Next, getTokens applies the split() method to rawString. This splits a string object into an array of strings by separating the string into substrings. This is where the regular expression pattern is written - it is the split method's argument:
split(/[ ,!.";:-]+/)
"Just like strings get written between quotes, regular expression patterns get written between slashes (/). This means that slashes inside the expression have to be escaped."
"The [ and ] characters have a special meaning inside a regular expression. They can enclose a set of characters, and they mean 'any of these characters'. Most non-alphanumeric characters have some special meaning inside a regular expression, so it is a good idea to always escape them with a backslash1 when you use them to refer to the actual characters."
The .filter(Boolean) array method is then applied to the array that was returned from having used the split method on the rawString. This means any falsy values will be removed from the array. Falsy values are false, 0, empty strings, null, undefined, and NaN.
Then, the sort method is applied to the array.
"The sort() method sorts the elements of an array in place (no copy of the array is made) and returns the array."
So, the getTokens function will work to remove flasy values and the specific characters in the regular expression from the text that we ultimately run the mostFrequentWord function on.
Next, we write the mostFrequentWord function that takes the text in the string we're going to run it on as its argument.
Our variables establish that the text that gets passed to the function will have had the getTokens function run on it and they also create an object of arrays that we're going to run a for loop on.
"The length property of an object which is an instance of type Array sets or returns the number of elements in that array."
So, this for loop will count the number of elements in the area and loop through each of them up until the total number of elements.
The if, else statement establishes that if i, an element in the array, appears more than once it will be counted incrementally. If it only appears once then it will be assigned the value of 1.
以上是关于text mostFrequentWord分析仪挑战的主要内容,如果未能解决你的问题,请参考以下文章