Files
freeCodeCamp/curriculum/challenges/chinese/22-rosetta-code/rosetta-code-challenges/word-frequency.md
2024-01-24 19:52:36 +01:00

3.7 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5e94a54cc7b022105bf0fd2c 词频 1 393913 word-frequency

--description--

Given a text string and an integer n, return the n most common words in the file (and the number of their occurrences) in decreasing frequency.

--instructions--

编写一个函数来计算每个单词的出现次数,并返回 n 个最常用的单词以及它们出现频率递减的次数。

该函数应返回一个二维数组,其中包含以下形式的每个元素:[word, freq]word 应该是单词的小写版本,freq 是表示计数的数字。

如果未提供字符串,该函数应返回一个空数组。

该函数应该不区分大小写例如字符串“Hello”和“hello”应该被视为相同。

您可以将具有特殊字符(例如下划线、破折号、撇号、逗号等)的单词视为不同的单词。

例如给定字符串“Hello hello goodbye”你的函数应该返回[['hello', 2], ['goodbye', 1]]

--hints--

wordFrequency 应该是一个函数。

assert(typeof wordFrequency == 'function');

wordFrequency 应该返回一个数组。

assert(Array.isArray(wordFrequency('test')));

wordFrequency("Hello hello world", 2) 应该返回[['hello', 2], ['world', 1]]

assert.deepEqual(wordFrequency(example_1, 2), example_1_solution);

wordFrequency("The quick brown fox jumped over the lazy dog", 1) 应该返回[['the', 2]]

assert.deepEqual(wordFrequency(example_2, 1), example_2_solution);

wordFrequency("Opensource opensource open-source open source", 1) 应该返回[['opensource', 2]]

assert.deepEqual(wordFrequency(example_3, 1), example_3_solution);

wordFrequency("Apple App apply aPP aPPlE", 3) 应该返回 [['app', 2], ['apple', 2], ['apply', 1]][['apple', 2], ['app', 2], ['apply', 1]]

const arr = JSON.stringify(wordFrequency(example_4, 3));
assert(arr === example_4_solution_a || arr === example_4_solution_b);

wordFrequency("c d a d c a b d d c", 4) 应该返回 [['d', 4], ['c', 3], ['a', 2], ['b', 1]]

assert.deepEqual(wordFrequency(example_5, 4), example_5_solution);

wordFrequency("", 5) 应该返回 []

assert.deepEqual(wordFrequency(example_6, 5), example_6_solution);

--seed--

--before-user-code--

var example_1 = 'Hello hello world';
var example_1_solution = [['hello', 2], ['world', 1]];
var example_2 = 'The quick brown fox jumped over the lazy dog';
var example_2_solution = [['the', 2]];
var example_3 = 'Opensource opensource open-source open source';
var example_3_solution = [['opensource', 2]];
var example_4 = 'Apple App apply aPP aPPlE';
var example_4_solution_a = "[[\"app\",2],[\"apple\",2],[\"apply\",1]]";
var example_4_solution_b = "[[\"apple\",2],[\"app\",2],[\"apply\",1]]";
var example_5 = 'c d a d c a b d d c';
var example_5_solution = [['d', 4], ['c', 3], ['a', 2], ['b', 1]];
var example_6 = '';
var example_6_solution = [];

--seed-contents--

function wordFrequency(txt, n) {

}

--solutions--

function wordFrequency(txt, n) {
  var words = txt.split(/\s+/);
  var wordCount = {};
  words.forEach(word => {
    if (word == '') {
      return;
    }
    const lowerWord = word.toLowerCase();
    wordCount[lowerWord] =
      lowerWord in wordCount ? wordCount[lowerWord] + 1 : 1;
  });

  var wordsArray = [];
  for (let [word, count] of Object.entries(wordCount)) {
    wordsArray.push([word, count]);
  }

  wordsArray.sort((a, b) => {
    if (a[1] !== b[1]) {
      return b[1] - a[1];
    } else if (a[0] !== b[0]) {
      return a[0] < b[0] ? -1 : 1;
    }
    return 0;
  });
  return wordsArray.slice(0, n);
}