Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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_Regex_Java.util.scanner - Fatal编程技术网

Java 在文本文件中搜索特定的文本行

Java 在文本文件中搜索特定的文本行,java,regex,java.util.scanner,Java,Regex,Java.util.scanner,我的同义词地图有问题。我希望能够在文本文件中搜索关键字或相关单词,然后输出找到的句子。因此,我的程序根据关键字或sunonym搜索问题的答案。我的程序的工作方式是在文本文件中搜索问题中的关键字,然后输出问题的答案,这是文本文件中问题之后的下一行。当我在一个问题中搜索主关键字时,程序会工作。但是当我试图用相关的单词提问时,程序无法识别输入。举个例子,如果我输入专业是什么?这个问题的答案在下一行,专业很难,但是如果我输入焦点,程序不识别相关单词focus,有人能帮我找到搜索相关单词的问题吗。这是我的

我的同义词地图有问题。我希望能够在文本文件中搜索关键字或相关单词,然后输出找到的句子。因此,我的程序根据关键字或sunonym搜索问题的答案。我的程序的工作方式是在文本文件中搜索问题中的关键字,然后输出问题的答案,这是文本文件中问题之后的下一行。当我在一个问题中搜索主关键字时,程序会工作。但是当我试图用相关的单词提问时,程序无法识别输入。举个例子,如果我输入专业是什么?这个问题的答案在下一行,专业很难,但是如果我输入焦点,程序不识别相关单词focus,有人能帮我找到搜索相关单词的问题吗。这是我的文本文件

 what is the textbook name?
 the textbook name is Java
 how is the major?
 the major is difficult
 how much did the shoes cost?
 the shoes cost two dollars
 how is the major when cramer took it?
 when cramer took it, it was okay
 how is the major when jar took it?
 jar said it was fine
 what is the color of my bag?
 the color of my bag is blue
这是我的密码

 public static class DicEntry {
    String key;
    String[] syns;
    Pattern pattern;

    public DicEntry(String key, String... syns) {
        this.key = key;
        this.syns = syns;
        pattern = Pattern.compile(".*(?:"
                + Stream.concat(Stream.of(key), Stream.of(syns))
                        .map(x -> "\\b" + Pattern.quote(x) + "\\b")
                        .collect(Collectors.joining("|")) + ").*");
    }
}



public static void parseFile(String s) throws IOException {

    List<DicEntry> synonymMap = populateSynonymMap(); // populate the map

    File file = new File("data.txt");
    Scanner scanner = new Scanner(file);
    Scanner forget = new Scanner(System.in);

    int flag_found = 0;

    while (scanner.hasNextLine()) {
        final String lineFromFile = scanner.nextLine();

        for (DicEntry entry : synonymMap) { // iterate over each word of the
                                            // sentence.

            if (entry.pattern.matcher(s).matches()) {

                if (lineFromFile.contains(entry.key)) {

                    //String bat = entry.key;
                    if(lineFromFile.contains(s))      {
                    String temp = scanner.nextLine();
                    System.out.println(temp);



                }
            }
        }
    }
    }


}


private static List<DicEntry> populateSynonymMap() {
    List<DicEntry> responses = new ArrayList<>();
    responses.add(new DicEntry("bag", "purse", "black"));
    responses.add(new DicEntry("shoe", "heels", "gas"));
    responses.add(new DicEntry("major", "discipline", "focus", "study"));

    return responses;
}

public static void getinput() throws IOException {

    Scanner scanner = new Scanner(System.in);
    String input = null;
    /* End Initialization */
    System.out.println("Welcome ");
    System.out.println("What would you like to know?");

    System.out.print("> ");
    input = scanner.nextLine().toLowerCase();
    parseFile(input);
}

public static void main(String args[]) throws ParseException, IOException {
    /* Initialization */
    getinput();

}

}

看来你过了以后

if (lineFromFile.contains(entry.key)) 
在parseFileString的方法中,您需要知道用户输入的输入是否包含entry.syn,并用键替换同义词

// This is case sensitive
boolean synonymFound = false;
for (String synonym : entry.syns) {
    if (s.contains(synonym)) {
        s = s.replace(synonym, entry.key)
        break;
    }
}
由于要在找到完全匹配或同义词匹配后停止搜索,因此需要使用return语句退出方法,或者使用标志退出while scanner.hasNextLine

结果:


请尝试从代码中删除任何不必要的部分,例如,对文本文件中找不到的问题进行提问和回答。我感谢您的帖子。但如果我问焦点是什么?答案应该是“专业是困难的”,而不是“专业”一词出现的每一个例子。懂另外,如果我问,当cramer接受它时,焦点是如何的,输出应该只有在cramer接受它时,才是okayYou,真相伴侣。非常感谢
if (lineFromFile.contains(s)) {
    String temp = scanner.nextLine();
    System.out.println(temp);

    flag_found = 1;

    System.out
            .println(" Would you like to update this information ? ");
    String yellow = forget.nextLine();
    if (yellow.equals("yes")) {
        // String black = scanner.nextLine();
        removedata(temp);
    } else if (yellow.equals("no")) {

        System.out.println("Have a good day");
        // break;
    }

    // Add return statment to end the search
    return;
}