【问题标题】:How do I find similar announcement by title and description?如何通过标题和描述找到类似的公告?
【发布时间】:2020-07-28 21:59:54
【问题描述】:

我需要将一个公告与一系列其他公告进行比较,并找出哪些是相似的。 如果两个公告的标题和描述中至少有一个相同的词,则它们被认为是相似的。 它们看起来像:

[
   {title: "Test", description: "hello", id: 22},
   {title: "Test two", description: "hi", id: 49},
   {title: "Test three", description: "hello there", id: 100},
   {title: "Test four", description: "oh", id: 129},
]

【问题讨论】:

    标签: javascript html arrays reactjs


    【解决方案1】:

    这应该可行:

    const findSimilarAnnouncements = (announcements, targetAnnouncement) => {
      const targetTitleWords = targetAnnouncement.title.split(' ');
      const targetDescriptionWords = targetAnnouncement.description.split(' ');
      
      return announcements.filter(({ title, description }) => {
        const titleWords = title.split(' ');
        const descriptionWords = description.split(' ');
    
        const isTitleSimilar = targetTitleWords
          .some(word => titleWords.includes(word));
        const isDescriptionSimilar = targetDescriptionWords
          .some(word => descriptionWords.includes(word));
    
        return isTitleSimilar && isDescriptionSimilar;
      });
    };
    

    【讨论】:

    • 它有效,但我需要在一个数组中收集类似的公告。你能帮忙吗?
    • 我不明白,它们都在一个数组中返回。如果没有找到类似的公告,则数组为空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多