簡體   English   中英

如何檢查單詞在哪一行

[英]How to check in which line of text the word is

我正在做一個練習,我需要計算一個單詞出現在文本上的次數,並且我還需要打印單詞出現在哪一行。

文本示例:

Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed 做 eiusmod tempor incididunt ut labore et dolore magna aliqua。 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat。 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur。 Exceptioneur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est labourum。

這是我查找和計算每個單詞的方法:

public void findWords() {
    try {
        File myObj = new File("path\\text.txt");
        Scanner myReader = new Scanner(myObj);
        while (myReader.hasNextLine()) {
            String text = myReader.nextLine();
            final String lowerText = text.toLowerCase();
            final String[] split = lowerText.split("\\W+");
            System.out.println("Output: ");
            for (String s : split) {
                if (s == null) {
                    continue;
                }
                int count = 0;
                for (int i = 0; i < split.length; i++) {
                    final boolean sameWorld = s.equals(split[i]);
                    if (sameWorld) {
                        count = count + 1;
                        split[i] = null;
                    }
                }
                System.out.println(s + " " + count);
            }
        }
        myReader.close();
    } catch (FileNotFoundException e) {
        System.out.println(e);
    }
}

當前的 output 是這樣的:

Output:
lorem 1
ipsum 1
dolor 1
sit 1
amet 1
consectetur 1
adipiscing 1
....

我希望它顯示單詞在哪一行:

Output:
lorem 1 - line 1
ipsum 1 - line 1
...

為了更清楚,單詞“ut”在 2 行中出現了 3 次,output 應該如下所示:

 ut 3 - line 1 2

是否可以創建一個 object 來處理這個詞。 它可以有一個字符串來表示單詞的文本,一個整數數組表示它出現的行,然后一個整數表示它在文本中出現的頻率。 至於跟蹤行,您可以使用 while 循環內的計數器變量來跟蹤您所在的行嗎?

如果你走那條路你可能會更好地使用 hashmap 和樹集來存儲單詞對象,然后根據某種順序將它們打印出來。

好吧,如果您被允許使用地圖,這就是如何完成的。 在我看來,非常熟悉和熟悉如何以及何時使用 List、HashMap(又名字典)和 HashSet(通常結合使用)是編程中最基本的技能之一。

public class SO { public static void main(String[] args) 拋出異常 { Map<String, Integer> wordToCount = new HashMap<>(); Map<String, Set> wordToLines = new HashMap<>();

    File myObj = new File("path\\text.txt");
    Scanner myReader = new Scanner(myObj);
    int lineNumber = 0;
    while (myReader.hasNextLine()) {
        lineNumber++;
        String text = myReader.nextLine();
        if (text.isBlank()) {
            continue;
        }
        final String lowerText = text.toLowerCase();
        final String[] split = lowerText.split("\\W+");
        for (String word : split) {
            Integer count = wordToCount.get(word);
            if (count == null) {
                count = 0;
            }
            wordToCount.put(word, count + 1);

            Set<Integer> lines = wordToLines.get(word);
            if (lines == null) {
                lines = new TreeSet<>();
                wordToLines.put(word, lines);
            }
            lines.add(lineNumber);
        }
    }
    myReader.close();

    for (String word : wordToCount.keySet()) {
        Set<Integer> lines = wordToLines.get(word);
        List<String> linesStrings = lines.stream().map(String::valueOf).toList();
        String linesStr = String.join(", ", linesStrings);
        int wordCount = wordToCount.get(word);
        System.out.printf("%-20s %-3d %s\n", word, wordCount, linesStr);
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM