java - 在文本文件中搜索特定文本行

标签 java regex java.util.scanner

我的同义词映射有问题。我希望能够在文本文件中搜索关键字或文本文件中的相关单词,然后输出找到的句子。所以我的程序根据关键字或同义词搜索问题的答案。我的程序的工作方式是在文本文件中搜索问题中的关键字,然后输出问题的答案,即文本文件中问题之后的下一行。当我搜索问题中的主要关键字时,程序可以正常工作。但是,当我尝试用相关单词提出问题时,程序无法识别输入。例如,如果我输入“专业怎么样?”该问题的答案在下一行,即“专业很难”,但如果我输入“焦点如何”,程序无法识别相关词焦点有人可以帮我找到搜索相关词的问题吗?这是我的文本文件

 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)) 

在您的 parseFile(String s) 方法中,您想知道用户输入的内容是否包含任何 entry.syns 并将同义词替换为键

// 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())

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;
}

结果:

enter image description here

enter image description here

关于java - 在文本文件中搜索特定文本行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30129182/

相关文章:

java - 我在尝试创建文件时遇到 FileNotFound 异常

java - 自签名证书 - 无法从客户端访问相关链接

java - 手动字符串查找和替换

python - 在导入带有额外逗号的 pandas 的 csv 文件时,如何使用正则表达式作为分隔符?

java - 扫描仪无法正常工作

java - Java程序终止而不是遍历选项

Java 正则表达式 : replace any B between A and Z

java - JWT 在其网站上很容易被解码

对整数数组进行排序时出现 java.lang.ArrayIndexOutOfBoundsException

c# 使用正则表达式更改文件链接路径