如何从Java中的if语句中排除条件

时间:2013-12-18 00:05:41

标签: java netbeans-7

我正在尝试使用用户输入搜索文本文件的多行(字符串'question'),但我需要排除一些常见术语,因为它们会带来错误的结果以及正确的结果。

 try {
            readName file = new readName(file_name);
            String[] aryLines = file.OpenFile();//OPEN  KNOWLEDGE BASE

            //SEARCH EACH ENTRY IN KNOWLEDGE BASE
            for (int i = 0; i < aryLines.length; i = i + 1) {
                String delims = "[ ]+";//SPLIT ITEMS INTO TOKENS
                //CREATE ARRAY OF TOKENS 
                String[] tokens = aryLines[i].split(delims);

                //SEARCH THROUGH TOKENS
                for (int j = 0; j < tokens.length; j = j + 1){
                    //MATCH QUESTIONS AGAINST TOKENS AND EXCLUSIONS
                    if (question.matches("(.*)" + tokens[j] + "(.*)")) {
                        System.out.println(aryLines[i]);

                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }

我试过放入

 if (question.matches("(.*)" + tokens[j] + "(.*)") 
         && !question.matches(*excluded word*))

但在这种情况下,输入搜索问题时不会产生任何结果。当搜索问题中省略排除的术语时,两个版本都能正常工作。

我在这里和其他地方都在寻找过,但到目前为止没有什么对我有用。任何帮助非常感谢!

这是我的知识库样本

Dogs have tails 
Donkeys have no humps 

如果我搜索no tails,它会输出两行,但我想强制它从搜索中排除no,以便它只返回Dogs have tails

1 个答案:

答案 0 :(得分:1)

我认为这就是你所追求的目标:

//THIS IS AN EXAMPLE KNOWLEDGE BASE
String[] aryLines = {"Dogs have tails","Donkeys have no humps"};

//THIS IS THE QUESTION SUPPLIED BY POSTER
String question = "no tails";

//IT SEEMS THAT POSTER WANTS TO EXCLUDE CERTAIN WORDS FROM THE SEARCH
String exclude = "no";

//REMOVE ALL OCCURRENCES OF THE EXCLUDE STRING IN QUESTION
question = question.replaceAll(exclude, "");

//FOR EACH TOKEN (FROM KNOWLEDGE BASE)
for(String token : aryLines) {
    //MATCH QUESTION AGAINST TOKENS
    if (token.matches("(.*)" + question + "(.*)")) {
        System.out.println(token);
    }
}

在此示例中,我删除了问题中出现的所有排除字符串。然后我将令牌与正则表达式进行比较:.*<question>.*

由于已排除的字符串在比较之前已被删除,因此它们将不再影响匹配的结果,因为代码会将Dogs have tailsDonkeys have no humps.*tails.*进行比较。< / p>