簡體   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