【问题标题】:How do sort my pattern match results as complete match, half a match and so on [duplicate]如何将我的模式匹配结果排序为完全匹配、半匹配等[重复]
【发布时间】:2012-10-27 18:31:15
【问题描述】:

可能重复:
Regular expression not matching subwords in phrase

我的程序显示匹配结果,但我想将结果排序为完全匹配(100%)、半匹配等。 我的文本文件包含以下行:

  1. 红车

  2. 红色

  3. 汽车

所以如果我搜索:“红色汽车”。我得到以下结果

  1. 红车

  2. 红色

  3. 汽车

所以我想做的是将找到的结果排序如下:

  1. “红车”100%匹配

  2. “红色”40% 匹配

  3. “汽车”40% 匹配

感谢任何帮助。

感谢任何帮助。我的代码如下:

public static void main(String[] args) {
  // TODO code application logic here
  String strLine;
  try{
    // Open the file that is the first 
    // command line parameter   
    FileInputStream fstream = new FileInputStream("C:\\textfile.txt"");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    Scanner input  = new Scanner (System.in);         
    System.out.print("Enter Your Search:  ");   // String key="red or yellow";
    String key = input.nextLine();

    while ((strLine = br.readLine()) != null) {     
      Pattern p = Pattern.compile(key); // regex pattern to search for
      Matcher m = p.matcher(strLine);  // src of text to search
      boolean b = false;
      while(b = m.find()) {                       
        System.out.println( " " + m.group()); // returns index and match
        // Print the content on the console
      }
    }
    //Close the input stream
    in.close();              
  }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
  }
}  

【问题讨论】:

  • 你能再解释一下逻辑吗?如果您有一行包含“Red or yellow Red Yellow”并且您搜索“Red or yellow”,我希望它在字符串的开头匹配一次。还是您的意思是您正在搜索“红色”或“黄色”,在这种情况下,我希望它匹配 4 次,每种颜色匹配两次 - 但这仍然不是 100%(字符串“或”不匹配)。
  • 抱歉文本文件包含三行。第 1 行是“红色或黄色” 第 2 行是“红色”,第三行是“黄色”。第一行是 100% 匹配,所以我想对它们进行排序
  • 我很困惑是“或”要匹配的字符串,还是逻辑运算符?如果是要匹配的字符串,则您的第一行匹配 100%,但其他两行根本不匹配。如果是逻辑运算符,那么我不明白为什么第一行是 100%。对不起,如果我错过了明显的
  • 你是对的,这是合乎逻辑的。我的例子完全错误,这就是造成混乱的原因。这是真正的问题。文本文件包含:第 1 行是:“红色汽车”。第 2 行是:"red" 第 3 行是"car",所以 red car 将 100% 匹配,而 red 和 car 各 40% 匹配。任何帮助表示赞赏

标签: java sorting


【解决方案1】:

假设您正在搜索“Red”或“Yellow”,and or 是您需要的唯一逻辑运算符(没有“and”或“xor”),并且您不想在其中使用任何通配符或正则表达式您搜索的内容,然后我将简单地循环遍历,尝试将每个字符串依次匹配该行。在伪代码中,类似于:

foreach (thisLine: allLinesInTheFile) {
    numOfCharsMatching = 0
    foreach (thisString: allSearchStrings) {
         if (thisLine.contains(thisString) {
               numOfCharsMatching = numOfCharsMatching + thisString.length
         }
    }
    score = ( numOfCharsMatching / thisLine.length ) * 100
}

如果您不希望空格计入分数,则需要从 thisString.length 中删除它们(并且不允许在搜索词中使用它们)

另一个问题是如果匹配可以重叠,numOfCharsMatching 将不正确(即,如果在“棕色行”中搜索“行”或“棕色”,它会说有 11 个字符匹配,比字符串的长度长. 您可以使用 BitSet 来跟踪匹配中涉及的字符,例如:

foreach (thisLine: allLinesInTheFile) {
    whichCharsMatch = new BitSet()
    foreach (thisString: allSearchStrings) {
         if (thisLine.contains(thisString) {
               whichCharsMatch.set(startPositionOfMatch, endPositionOfMatch, true)
         }
    }
    score = ( numOfCharsMatching / thisLine.length ) * 100
}

看看 BitSet javadoc,尤其是 set 和 cardinality 方法

【讨论】:

  • 我的意思是伪代码,我没有检查任何方法名称,而且 foreach 来自完全不同的语言!
猜你喜欢
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 2014-10-01
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
相关资源
最近更新 更多