将输入存储到文件

时间:2015-05-07 01:03:27

标签: java regex

我在尝试搜索文本文件以查找用户输入的确切输入时遇到问题。我想不仅通过直接用户输入输出句子,而且我希望程序识别一些可以发出所需文本信号的单词。我搜索了关键字part down pack and working但我只能根据关键字搜索文本。我想根据关键字和整个输入的句子进行搜索。例如,如果关键字是电子邮件,并且用户输入的是什么是火星电子邮件?并且文本文件包含“mars电子邮件是mars3433@aol.com,john电子邮件是匿名的”我想输出火星电子邮件是...而不是两个句子。我完全陷入困境,试图找出这个问题,任何人都可以帮助我吗?

   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 removedata(String s) throws IOException {

    File f = new File("data.txt");
    File f1 = new File("data2.txt");
    BufferedReader input = new BufferedReader(new InputStreamReader(
            System.in));
    BufferedReader br = new BufferedReader(new FileReader(f));
    PrintWriter pr = new PrintWriter(f1);
    String line;

    while ((line = br.readLine()) != null) {
        if (line.contains(s)) {

            System.out.println("Enter new Text :");
            String newText = input.readLine();
            line = newText;
            System.out.println("Thank you, Have a good Day!");

        }

        pr.println(line);
    }
    br.close();
    pr.close();
    input.close();
    Files.move(f1.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING);

}

public static void parseFile(String s) throws IOException {
    File file = new File("data.txt");
    Scanner forget = new Scanner(System.in);
    Scanner scanner = new Scanner(file);
    int flag_found = 0;
    while (scanner.hasNextLine()) {
        final String lineFromFile = scanner.nextLine();
        if (lineFromFile.contains(s)) {

            // a match!
            System.out.println(lineFromFile);
            flag_found = 1;

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

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

        }

    }
    if (flag_found == 0) {// input is not found in the txt file so
                            // flag_found remains 0
        writer();
    }

}

public static void writer() {
    Scanner Keyboard = new Scanner(System.in);
    Scanner input = new Scanner(System.in);

    File file = new File("data.txt");
    try (BufferedWriter wr = new BufferedWriter(new FileWriter(
            file.getAbsoluteFile(), true))) { // Creates a writer object
                                                // called wr
                                                // file.getabsolutefile
                                                // takes the filename and
                                                // keeps on storing the old
        System.out.println("I Do not know, Perhaps you want to teach me?"
                + "..."); // data
        while ((Keyboard.hasNext())) {

            String lines = Keyboard.nextLine();
            System.out.print(" is this correct ? ");
            String go = input.nextLine();

            if (go.equals("no")) {
                System.out.println("enter line again");
                lines = Keyboard.nextLine();
                System.out.print(" is this correct ? ");
                go = input.nextLine();
            }

            else if (go.equals("yes")) {
                wr.write(lines);
                // wr.write("\n");

                wr.newLine();

                wr.close();
            }

            System.out.println("Thankk you");
            break;
        }
    } catch (IOException e) {
        System.out.println(" cannot write to file " + file.toString());
    }
}

private static List<DicEntry> populateSynonymMap() {
    List<DicEntry> responses = new ArrayList<>();
    responses.add(new DicEntry("student", "pupil", "scholar"));
    responses.add(new DicEntry("office", "post", "room"));
    responses.add(new DicEntry("topics", "semester talk"));

    return responses;
}

public static void getinput() throws IOException {

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

    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();
    String[] inputs = input.split(" ");

    int flag_found = 0;

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

        if (entry.pattern.matcher(input).matches()) {
            // System.out.println(entry.key);
            parseFile(entry.key);
            flag_found = 1;// Input is found

        }

    }
    if (flag_found == 0) {// input is not found in the txt file so
                            // flag_found remains 0
        writer();
    }
}

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

}

  }

所以我的方法就像这样工作,解析文件方法在文本文件中搜索句子中的关键字。我的writer()写入文件,如果找不到输入,我的删除数据()将删除该行,并在用户请求时使用新字符串更新它。获取输入只是从扫描仪获取信息的一种方法。

1 个答案:

答案 0 :(得分:0)

在我看来,另外一个障碍是事实,有些词可以在不相关的句子中重复。我的解决方案对我来说似乎很长,但它确实有效。但是当我测试它时,我没有使用你的dicEntry。对所有同义词进行硬编码是不可能的,因此您应该重新考虑这种方法。 我添加了一个类,jast作为int重复变量的数据持有者(见下文)和特定的句子:

public class Pair {
    int repetitions;
    String sentence;

    public Pair(int rep, String string){
        repetitions = rep;
        sentence = string;
    }

    public int getRepetitions() {
        return repetitions;
    }

    public String getSentence() {
        return sentence;
    }
}

然后我写了一个方法,循环输入句子和文件内容,寻找文件中的句子,其中大多数输入单词被重复。我很确定,这不是最有效的方式,但我不知道另一个:P。

public static String getMostAppropriate(String[] input) throws IOException{
    File file = new File("data.txt");
    Scanner scanner = new Scanner(file);
    ArrayList<Pair> pairs  = new ArrayList<>();
    int repetitions = 0;
    while (scanner.hasNextLine()) {
        String newLine = scanner.nextLine();
        String[] line = newLine.split(","); // this regex depends on your file format style,
        String oneSentence = "";
        for(String sentence : line){  // for sentence in file lines
            for(String string : sentence.split(" ")){  // for words in these sentences
                for(String word : input){  // for words from input
                    if(word.equals(string)){
                        repetitions += 1;
                        oneSentence = sentence;
                    }
                }
            }
            pairs.add(new Pair(repetitions,oneSentence));
            repetitions = 0;
        }
    }
    return mostCommon(pairs);
}

参数String[] inputs形成getInput方法。在return语句中,我调用了另一个新方法,该方法查找重复次数最多的句子:

public static String mostCommon(ArrayList<Pair> pairs){
    Pair max = new Pair(0,"");
    String result = "";
    for(Pair pair : pairs){
        if(pair.getRepetitions() > max.getRepetitions()){
            result = pair.getSentence();
            max = pair;
        }else if(pair.getRepetitions()==max.getRepetitions()){
            result += "; " + pair.getSentence();
        }
    }
    return result;
}

如果某些句子具有相同的重复次数,则它将两个(或更多)连接成一个句子(句子;句子;等)。

如果您有兴趣,请执行我留给您的代码。

正如我所说,我没有使用你的dicEntry,你仍然可以将它添加为额外的循环,但是使用我的方法查找整个字典不会太有效。

另外,如果我是你,我会将你的一些方法划分为较小的方法,我的意思是:将文件读入一个,在另一个中请求额外的输入。因为这种方式更容易实现更改。你不需要关注整个方法,只需要将它们传递给对方。

我希望你能在我的帖子中找到有用的东西。