检查数据库“单词”集合中的所有单词,并检查文本中的任何单词是否与任何单词匹配

时间:2018-07-13 12:53:44

标签: javascript mongodb

我在MongoDB数据库中有一个名为word的集合,该集合存储所有单词。它们是通过后端查询提取出来的,并被推送到前端。

这已在前端完成:

this.annotationSub = this.annotationService
      .getWordUpdateListener()
      .subscribe((thewords: ComplexWord[]) => {
        this.thewords = thewords;
        this.thewords.map(word => {
          if (word.word === this.setWord) {
            this.wordIWant = word.word;
        }
         console.log(word);
      });

顶部的console.log(word);赋予这些字段=

{word: "Lorem", annotation: "Explain Lorem"},
{word: "Aenean", annotation: "Explain Aenean"},
{word: "Hello", annotation: "Explaining Hello"}

这将检索所有文本:

this.postsService.getPosts();
    this.postsSub = this.postsService
      .getPostUpdateListener()
      .subscribe((posts: Post[]) => {
        this.posts = posts;
        this.posts.map(post => {
          if (post.id === this.id) {
            this.postIWant = post.fileText;
          }
        });
      });

this.postIWant,我收到了帖子中的所有文字。

现在我该如何检查单词是否与this.postIWant中的文本匹配?

非常感谢

1 个答案:

答案 0 :(得分:0)

这是最好的解决方案。

将文本传递给函数:

function complexWordIdentification(text) {
  const complexWords = ['Hello', 'World', 'Complex Phrase'];
  const results = [];
  let match, regexp, result;

  for (let i = 0; i < complexWords.length; i++) {
    // the complex word we are checking in this iteration
    const complexWord = complexWords[i];
    regexp = new RegExp(complexWord, 'g');

    while ((match = regexp.exec(text)) !== null) {

      result = {
        begin: (regexp.lastIndex - complexWords[i].length),
        end: regexp.lastIndex,
        text: complexWord
      };
      results.push(result);
    }
  }
  return results;
}