【问题标题】:Smart string matching algorithm智能字符串匹配算法
【发布时间】:2021-11-06 23:21:42
【问题描述】:

我正在寻找一种可用于比较两个句子并提供匹配分数的算法。例如,

输入

句子输入 1:“一只敏捷的棕狐跳过懒狗”

句子输入 2:“Alpha bravo dog quick beta gamma fox over the lazy dug”

输出

输出:“一只快速棕色狐狸跳过 懒惰狗"

分数:9 分中的 5 分(以正确顺序找到的单词)

有人可以指点我正确的方向吗?

【问题讨论】:

标签: string algorithm matching


【解决方案1】:

你能试试这个基于c++的算法来解决上面的问题吗?

int MatchingScore(string s1, string s2) {
      int count = 0;
      unordered_map<string, int> my_map;
      stringstream ss1(s1);
      stringstream ss2(s2);
      vector<string> tokens1;
      vector<string> tokens2;
      string temp_str;

      // tokenise strings s1, s2 based on space char
      while(getline(ss1, temp_str, ' ')) {
         tokens1.push_back(temp_str);
      }
      while(getline(ss2, temp_str, ' ')) {
         tokens2.push_back(temp_str);
      }

      // push words of string1 to hash_map
      for(auto s: tokens1) {
          my_map[s]++;
      }
      // while iterating through string2 check if word already present in hash_map
      for(auto s: tokens2) {
          if(my_map.find(s) != my_map.end()) {
              count++;
          }
      }
      return count;
  }

【讨论】:

    【解决方案2】:

    你可以试试这个java代码:

        String sentence = "A quick brown fox jumps over the lazy dog";
        String searchPhrase = "Alpha bravo dog quick beta gamma fox over the lazy dug";
    
        String[] searchwords = searchPhrase.trim().split(" ");
        int score = 0;
    
        for (int i = 0; i<searchwords.length ;i++) {
            if (sentence.toLowerCase().indexOf(searchwords[i].toLowerCase()) != -1) {
                score +=1;
            } else {
                score +=0;
            }
        }
    
        System.out.println("Score: " + score + " out of " + searchwords.length);
    

    【讨论】:

      【解决方案3】:

      一个可行的解决方案,经过变体测试。需要用正则表达式等进行优化

      输出:

      Score: 5 out of 9
      A *quick* brown *fox* jumps *over* *the* *lazy* dog
      

      Java 代码是用 Android Studio junit 编写的

      String sentence = "A quick brown fox jumps over the lazy dog";
          String searchPhrase = "Alpha bravo dog quick beta gamma fox over the lazy dug";
      
          String[] searchWords = sentence.split(" ");
          int score = 0;
      
          String outputString = sentence;
      
          for (String searchWord : searchWords) {
              String spacedPhrase = " " + searchPhrase + " ";
              String spacedWord = " " + searchWord.toLowerCase() + " ";
              if (spacedPhrase.toLowerCase().contains(spacedWord)) {
                  score += 1;
                  searchPhrase = searchPhrase.substring(spacedPhrase.indexOf(spacedWord) + 1);
                  outputString = (" " + outputString + " ").replaceFirst(" "+ searchWord + " ",
                          " *" + searchWord + "* ");
              }
          }
      
          System.out.println("Score: " + score + " out of " + sentence.split(" ").length);
          System.out.println(outputString.trim());
          assertEquals("A *quick* brown *fox* jumps *over* *the* *lazy* dog", outputString.trim());
      

      【讨论】:

        猜你喜欢
        • 2011-01-01
        • 1970-01-01
        • 2015-06-18
        • 1970-01-01
        • 2013-12-24
        • 2016-07-19
        • 2012-07-24
        • 2010-09-08
        • 2013-07-02
        相关资源
        最近更新 更多