如何检查字符串中的单词对?

时间:2019-03-09 08:13:39

标签: javascript count word

下面的代码可以完美地在字符串(大规模.txt文件)中创建单词出现的CSV列表,就像这样:

Name;Total
THE;23562
OF;15954
AND;15318
IN;12159
TO;11879
A;11145
I;6135
WAS;6045
etc...

我现在想要的是两个单词对,如果足够容易的话甚至三个单词对。所以像

Name;Total
OF THE;25
FROM THE;20
BY WHICH;13
OF WHICH;5
etc...

如何修改现有代码以检查配对而不是单个单词?

//chrisjopa.com/2016/04/21/counting-word-frequencies-with-javascript/

var fs = require('fs');
var file = 'INPUT.txt';

//Create Headers for the CSV File
const createCsvWriter = require('csv-writer').createObjectCsvWriter;  
const csvWriter = createCsvWriter({  

//Define Pathname to your choice
  path: 'Data1.csv',
  header: [
    {id: 'name', title: 'Name'},
    {id: 'total', title: 'Total'},
  ]
});

// read file from current directory
fs.readFile(file, 'utf8', function (err, data) {

  if (err) throw err;

  var wordsArray = splitByWords(data);
  var wordsMap = createWordMap(wordsArray);
  var finalWordsArray = sortByCount(wordsMap);

//Write CSV Output File
  csvWriter  
  .writeRecords(finalWordsArray)
  .then(()=> console.log('DONE'));

});


function splitByWords (text) {

  // Removes all special characters, then white spaces, 
  //then converts to all capital letters, then splits the words
  var noPunctuation = text.replace(/[\.,-\/#!$%\^&\*;:{}�=\-_'`’~"()@\+\?><\[\]\+]/g, '');
  var noExtraSpaces = noPunctuation.replace(/\s{2,}/g," ");
  var allUpperCase  = noExtraSpaces.toUpperCase();
  var wordsArray    = allUpperCase.split(/\s+/);
  return wordsArray;
}

//This is the part in the code that I feel is the place to check for word 
//pairs, but I'm not sure how I'm supposed to write it.
function createWordMap (wordsArray, ) {

  // create map for word counts
  var wordsMap = {};

  wordsArray.forEach(function (key) {
    if (wordsMap.hasOwnProperty(key)) {
      wordsMap[key]++;
    } else {
      wordsMap[key] = 1;
    }
  });

  return wordsMap;

}


function sortByCount (wordsMap) {

  // sort by count in descending order
  var finalWordsArray = [];
  finalWordsArray = Object.keys(wordsMap).map(function(key) {
    return {

      name: key,
      total: wordsMap[key]

    };
  });

  finalWordsArray.sort(function(a, b) {
    return b.total - a.total;
  });

  return finalWordsArray;

}

1 个答案:

答案 0 :(得分:0)

wordsArray,创建另一个将每对单词组合在一起的数组。例如,来自{p的wordsArray

['Foo', 'Bar', 'Baz', 'Buzz']

创建:

['Foo Bar', 'Bar Baz', 'Baz Buzz']

然后,您可以使用已经完全计数每对出现次数的完全相同的功能-只需使用createWordMap对其进行调用(然后调用sortByCount )。例如:

const wordsArray = ['Foo', 'Bar', 'Baz', 'Buzz', 'Foo', 'Bar'];
const wordPairsArray = [];
for (let i = 1; i < wordsArray.length; i++) {
  wordPairsArray.push(wordsArray[i - 1] + ' ' + wordsArray[i]);
}
const wordPairMap = createWordMap(wordPairsArray);
const wordPairCount = sortByCount(wordPairMap);
console.log(wordPairCount);


// the following is your original code:
function createWordMap(wordsArray, ) {
  // create map for word counts
  var wordsMap = {};
  wordsArray.forEach(function(key) {
    if (wordsMap.hasOwnProperty(key)) {
      wordsMap[key]++;
    } else {
      wordsMap[key] = 1;
    }
  });
  return wordsMap;
}
function sortByCount(wordsMap) {
  // sort by count in descending order
  var finalWordsArray = [];
  finalWordsArray = Object.keys(wordsMap).map(function(key) {
    return {
      name: key,
      total: wordsMap[key]

    };
  });
  finalWordsArray.sort(function(a, b) {
    return b.total - a.total;
  });
  return finalWordsArray;
}

要将其扩展到不仅仅是对,只需更改循环以将动态数量的元素连接在一起即可。

function combineWords(words, wordsInItem) {
  const items = [];
  for (let i = wordsInItem - 1; i < words.length; i++) {
    const start = i - (wordsInItem - 1);
    const end = i + 1;
    items.push(words.slice(start, end).join(' '));
  }
  return items;
}
function getCount(words, wordsInItem) {
  const combinedWords = combineWords(words, wordsInItem);
  const map = createWordMap(combinedWords);
  const count = sortByCount(map);
  console.log(count);
}

getCount(['Foo', 'Bar', 'Baz', 'Buzz', 'Foo', 'Bar'], 2);
getCount(['Foo', 'Bar', 'Baz', 'Buzz', 'Foo', 'Bar', 'Baz'], 3);





// the following is your original code:
function createWordMap(wordsArray, ) {
  // create map for word counts
  var wordsMap = {};
  wordsArray.forEach(function(key) {
    if (wordsMap.hasOwnProperty(key)) {
      wordsMap[key]++;
    } else {
      wordsMap[key] = 1;
    }
  });
  return wordsMap;
}
function sortByCount(wordsMap) {
  // sort by count in descending order
  var finalWordsArray = [];
  finalWordsArray = Object.keys(wordsMap).map(function(key) {
    return {
      name: key,
      total: wordsMap[key]

    };
  });
  finalWordsArray.sort(function(a, b) {
    return b.total - a.total;
  });
  return finalWordsArray;
}