文本词频统计

  1. /**
  2. *
  3. * @param {string} text 文本内容
  4. * @param {string | array} targetWord 需要统计的词,可为单个词的字符串,也可为多个词的列表
  5. */
  6. function wordCount (text, targetWord) {
  7. let wordOnlyText = ''
  8. // // 去除文本内所有符号数字
  9. // Array.prototype.forEach.call(text, (str, i) => {
  10. // const asciicode = str.charCodeAt()
  11. // if ((asciicode <= 90 && asciicode >= 65) || (asciicode >= 97 && asciicode <= 122) || asciicode === 32) {
  12. // wordOnlyText += str
  13. // }
  14. // })
  15. // // 将文本转为小写
  16. // const lowerCaseText = wordOnlyText.toLowerCase()
  17. // // 将文本切分为数组
  18. // const words = lowerCaseText.split(' ')
  19. // 更为简单的文本切割方法
  20. const words = text.toLowerCase().match(/\w+/g)
  21. // 词频统计函数
  22. function wordFrequencyAccount (targetWord) {
  23. let count = 0
  24. words.forEach((word, i) => {
  25. if (word === targetWord) {
  26. count++
  27. }
  28. })
  29. return count
  30. }
  31. // 传入的待统计为单词还是单词组
  32. const countType = typeof targetWord
  33. // 最终返回的数据
  34. let result = {}
  35. if (countType === 'string') {
  36. const _count = wordFrequencyAccount(targetWord)
  37. result[targetWord] = _count
  38. } else if (countType === 'object') {
  39. targetWord.forEach((target, i) => {
  40. const _count = wordFrequencyAccount(target)
  41. result[target] = _count
  42. })
  43. }
  44. return result
  45. }
  46. const testText = `
  47. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  48. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  49. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  50. `
  51. console.log(wordCount(testText, 'this'))
  52. // { this: 2 }
  53. console.log(wordCount(testText, ['this', 'and', 'software']))
  54. // { this: 2, and: 4, software: 8 }

数据插入元素

假设我们需要在数组 [ 1, 2, 4, 5 ] 中的第三个位置,即下标为 2 的位置上添加元素 3。这需要用到 array.splice(start, deleteCount, element1[, …[, elementN]]) 方法。你可以注意到该方法第二个参数是 deleteCount,因为这个方法也可以用来删除数组中某一个位置开始的若干个元素,而当我们将这个参数设置为 0 的时候,该方法第三个以及后面的参数便会插入到下标为 start 的位置,后面的元素自动往后推导。

  1. const array = [ 1, 2, 6, 7 ]
  2. array.splice(2, 0, 3)
  3. console.log(array) //=> [1, 2, 3, 6, 7]
  4. array.splice(3, 0, 4, 5)
  5. console.log(array) //=> [1, 2, 3, 4, 5, 6, 7]...