如何修改JavaScript代码统计文本中的双词/三词词组频次?
Hey there! Let's adjust your existing code to count two-word (or three-word) phrases instead of single words. The core idea is to generate "ngrams" (sequences of n words) from your processed word list, then reuse most of your existing counting logic since it works great for tallying occurrences.
Step 1: Add a Function to Generate N-Grams
First, we'll create a helper function that takes your array of single words and turns it into an array of word pairs (or triples). This function lets you choose how many words per phrase (set n to 2 for bigrams, 3 for trigrams):
function createNgrams(wordsArray, n) { const ngrams = []; // Loop through the words, stopping early to avoid going out of bounds for (let i = 0; i <= wordsArray.length - n; i++) { // Join n consecutive words into a single phrase const phrase = wordsArray.slice(i, i + n).join(' '); ngrams.push(phrase); } return ngrams; }
Step 2: Update the Main Logic to Use N-Grams
In your fs.readFile callback, we'll swap out the single-word array for our n-gram array. Everything else (counting, sorting, writing the CSV) stays almost the same—since your existing createWordMap and sortByCount functions work with any string keys:
fs.readFile(file, 'utf8', function (err, data) { if (err) throw err; // First get the cleaned single-word array (same as before) var singleWordsArray = splitByWords(data); // Generate 2-word phrases (change 2 to 3 for triples!) var phraseArray = createNgrams(singleWordsArray, 2); // Use your existing counting and sorting logic var wordsMap = createWordMap(phraseArray); var finalWordsArray = sortByCount(wordsMap); //Write CSV Output File csvWriter .writeRecords(finalWordsArray) .then(()=> console.log('DONE')); });
Step 3: (Optional) Make It Flexible for Any N
If you want to easily switch between single words, pairs, and triples, you can add a variable at the top to control the phrase length:
// Add this near your file path variable const PHRASE_LENGTH = 2; // Change to 1 for single words, 3 for triples
Then update the callback to use this variable:
var phraseArray = createNgrams(singleWordsArray, PHRASE_LENGTH);
Why This Works
Your original splitByWords function still does all the heavy lifting of cleaning the text (removing punctuation, normalizing to uppercase, trimming spaces)—this ensures phrases like "Of the" and "OF THE" are counted as the same entry. The createNgrams function just groups consecutive words into phrases, and your existing counting/sorting logic handles the rest seamlessly.
内容的提问来源于stack exchange,提问作者Seabottom

