Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将模式匹配结果排序为完全匹配、半匹配等_Java_Sorting - Fatal编程技术网

Java 如何将模式匹配结果排序为完全匹配、半匹配等

Java 如何将模式匹配结果排序为完全匹配、半匹配等,java,sorting,Java,Sorting,可能重复: 我的程序显示匹配结果,但我希望将结果排序为完全匹配(100%)、半匹配等。 我的文本文件包含以下行: 红色汽车 红色的 汽车 所以如果我搜索“红色汽车”。我得到以下结果 红色汽车 红色的 汽车 因此,我要做的是对找到的结果进行排序,如下所示: public static void main(String[] args) { // TODO code application logic here String strLine; try{ // Open the f

可能重复:

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

  • 红色汽车

  • 红色的

  • 汽车

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

  • 红色汽车

  • 红色的

  • 汽车

  • 因此,我要做的是对找到的结果进行排序,如下所示:

    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());
      }
    }  
    
  • “红色汽车”100%匹配

  • “红色”40%匹配

  • “汽车”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”或“Yellow”,或者是您需要的唯一逻辑运算符(no'and'或'xor'),并且您不想在搜索的内容中使用任何通配符或正则表达式,那么我将简单地循环,试着根据行依次匹配每个字符串。在伪代码中,类似于:

    foreach (thisLine: allLinesInTheFile) {
        numOfCharsMatching = 0
        foreach (thisString: allSearchStrings) {
             if (thisLine.contains(thisString) {
                   numOfCharsMatching = numOfCharsMatching + thisString.length
             }
        }
        score = ( numOfCharsMatching / thisLine.length ) * 100
    }
    
    foreach (thisLine: allLinesInTheFile) {
        whichCharsMatch = new BitSet()
        foreach (thisString: allSearchStrings) {
             if (thisLine.contains(thisString) {
                   whichCharsMatch.set(startPositionOfMatch, endPositionOfMatch, true)
             }
        }
        score = ( numOfCharsMatching / thisLine.length ) * 100
    }
    
    如果不想让空格计入分数,则需要将其从thisString.length中删除(并且不允许在搜索词中使用)

    另一个问题是,如果匹配可能重叠,numOfCharsMatching将不正确(即,如果在“brown row”中搜索“row”或“brown”,则会显示有11个字符匹配,长度超过字符串长度。您可以使用位集跟踪匹配中涉及的字符,例如:

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

    请看一下位集javadoc,特别是与set和cardinality方法相关的:您能进一步解释一下逻辑吗?如果您有一行包含“Red或yellow Red yellow”并搜索“Red或yellow”,我希望它在字符串开头匹配一次。或者您的意思是搜索“Red”或“yellow”,在这种情况下,我希望它匹配4次,每种颜色两次-但这仍然不是100%(字符串“或”不匹配)。很抱歉,文本文件包含三行。第1行是“红色或黄色”,第2行是“红色”,第3行是“黄色”。第一行是100%匹配,所以我想对它们进行排序。我感到困惑的是“或”要匹配的字符串,还是逻辑运算符?如果是要匹配的字符串,您的第一行匹配100%,但其他两行完全不匹配。如果是逻辑运算符,则我不理解为什么第一行是100%。抱歉,如果我缺少显而易见的内容,您是对的,这是逻辑的。我的exmaple完全错误,这就是问题所在造成混乱。这是真正的问题。文本文件包含:第1行是:“红色汽车”。第2行是:“红色”第3行是“汽车”,因此红色汽车将是100%匹配,而红色和汽车分别是40%匹配。任何帮助都受到PSEDOO代码的赞赏,我的意思是,我没有检查任何方法名称,foreach来自完全不同的语言!