繁体   English   中英

按每个单词查找文本文件的行号

[英]Find the line number of a text file by each word

我想通过每个单词找到文本文件的行号,但是,我在下面编写的方法只给出了第一个数字,而我需要一个行号列表。

例如,如果“a”出现在第 1,3,5 行中,它应该有一个 [1,3,5] 的列表。 这个列表结果然后将被传递到另一个方法进行进一步处理。 但是,我的结果只显示 [1] 为“a”。

有人可以帮我解决这个问题吗? 谢谢!

    public SomeObject<Word> buildIndex(String fileName, Comparator<Word> comparator) {
        SomeObject<Word> someObject = new SomeObject<>(comparator);

        Comparator<Word> comp = checkComparator(someObject.comparator());
        int num = 0;
        if (fileName != null) {
            File file = new File(fileName);
            try (Scanner scanner = new Scanner(file, "latin1")) {
                while (scanner.hasNextLine()) {
                    String lines;
                    if (comparator instanceof IgnoreCase) {
                        lines = scanner.nextLine().toLowerCase();
                    } else {
                        lines = scanner.nextLine();
                    }
                    if (lines != null) {
                        String[] lineFromText = lines.split("\n");

                        List<Integer> list = new ArrayList<>();
                        for (int i = 0; i < lineFromText.length; i++) {
                            String[] wordsFromText = lineFromText[i].split("\\W");
                            num++;

                            for (String s : wordsFromText) {

                                if (s != null && lineFromText[i].contains(s)) {
                                    list.add(num);
                                }

                                if (s != null && !s.trim().isEmpty() && s.matches("^[a-zA-Z]*$")) {
                                    doInsert(s, comp, someObject, list);
                                }
                            }


                        }

                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return someObject;
    }

像这样的事情对你有用吗?

  1. 它一次读取一行。
  2. 通过在spaces拆分来查找单词。
  3. 然后将单词和行号放在map ,其中键是单词,值是行号列表。
      int lineCount = 1;
      String fileName = "SomeFileName";
      Map<String, List<Integer>> index = new HashMap<>();
      Scanner scanner = new Scanner("fileName");

      while (scanner.hasNextLine()) {
         //get single line from file
         String line = scanner.nextLine().toLowerCase();
         //split into words
         for (String word : line.split("\\s+")) {
             // add to lineNumber to map if List already there.
             // otherwise add new List and then add lineNumber  
             index.compute(word,
                   (wd, list) -> list == null ? new ArrayList<>()
                        : list).add(lineCount);
         }
         // bump lineCount for next line
         lineCount++;
      }

打印出来。

      index.forEach((k, v) -> System.out.println(k + " --> " + v));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM