简体   繁体   中英

MongoDB: How to realize an lookup dictionary for checking text

I would like to realize an dictionary to check for correct spelling of some text. In this dictionary there are 20.000 word. My application (which is a meteor application) will first load the text. Now I would split this text into words and check if each of them is in the dictionary.

But is this technically the best way? A text with 100 words, would have 100 DB calls, which feels not good. But also it doesn't make sense for me to load 20.000 word completly in an array to make a lookup...

let incorrect = [];
text.split(' ').forEach(word => {
    if (!Dictionary.findOne({ word: word })) {
        incorrect.push(word);
    }
})

if (incorrect.length)
    console.log('There is a spelling mistake');
else
    console.log('Everything seems to be correct');

Another way I was thinking of is to send the array with the splitted words in a query and geting all missing elements as an result (array). But I don't know if this can be done by mongoDB.

You would find all the words in the text which are in the database. So if the text contains 100 words, there should be 100 documents respectively, if not that means there is something wrong with the text:

const arr = text.split(' ');
const wordCount = arr.length;

const docCount = Dictionary.find({
  word: {
    $in: arr,
  },
}).count();

if (wordCount !== docCount) {
  console.log('There is a spelling mistake');
}

Update

If you need to get the misspelled words, you would simply use a diff function on the arr input and the result words found in db. I suppose you have underscore installed, I use _.difference to get the result:

const arr = text.split(' ');

const foundWord = Dictionary.find({
  word: {
    $in: arr,
  },
}).map(obj => obj.word);

const misspelledWords = _.difference(arr, foundWord);

console.log(misspelledWords);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM