如何比较String Array并计算类似的单词

时间:2015-11-26 22:29:50

标签: java arrays

我一直在尝试获取此代码,但我仍然无法接受。这段代码是我能做的最接近的。我错过了什么?我试图在没有Hash的情况下执行此代码。

    // Read all the words from the dictionary (text.txt) into an array
    BufferedReader br = new BufferedReader(new FileReader("text.txt"));
    int bufferLength = 1000000;
    char[] buffer = new char[bufferLength];
    int charsRead = br.read(buffer, 0, bufferLength);
    br.close();
    String text = new String(buffer);
    text = text.trim();
    text = text.toLowerCase();
    String[] words = text.split("\n");

    System.out.println("Total number of words in text: " + words.length);

    //Find unique words:
    String[] uniqueText = words;
    int[] uniqueTextCount = new int[uniqueText.length];

    for (int i = 0; i < words.length; i++) {
        for (int j = 0; j < uniqueText.length; j++) {
            if (words[i].equals(uniqueText[j])) {
                uniqueTextCount[j]++;
            } else {
                uniqueText[i] = words[i];
            }
        }
        System.out.println(uniqueText[i] + " for " + uniqueTextCount[i]);
    }
}

2 个答案:

答案 0 :(得分:1)

从原始代码我假设:

  • text.txt每行包含一个单词。
  • 你想要计算每个单词出现的次数(而不是&#34;类似的单词&#34;当你在标题中写的时候)。

也许首先是BufferedReader允许line-by-line reading

for (String line; (line = br.nextLine()) != null; ) {
  // Process each line, which in this case is a word.
}

更希望逐行处理而不是读取整个文件,因为你的程序需要使用更少的内存(尽可能多的文件大小)。

现在,如果我们考虑需求,所需的输出是从不同的单词到其计数的映射。这应该在上面的for - 循环之前。

// A HashMap would also work, but you have specified that you do not want
// to use hashing.
Map<String, Integer> distinctWordCounts = new TreeMap<>();

当初始化时,在循环的每次迭代中(即,对于我们遇到的每一行),我们可以执行以下操作:

if (distinctWordCounts.hasKey(line)) {
  // We have seen this word. Increment the count we've seen it.
  distinctWordCounts.put(line, distinctWordCounts.get(line) + 1);
} else {
  // We have never seen this word. Set the count seen to 1.
  distinctWordCounts.put(line, 1);
}

上面的代码会产生比看起来最优的更多开销,因为if案例涉及三次遍历,我们可以通过一次遍历。但这可能是另一天的故事,除非你有理由关注非渐近的速度提升。

在一天结束时,我们可以遍历distinctWordCounts以获取字数

for (Entry<String, Integer> entry : distinctWordCounts.entrySet()) {
  System.out.println(entry.getKey() + " occurs " + entry.getValue() + "times.");
}

答案 1 :(得分:1)

听起来你只是想计算每个单词的不同出现次数?如果是这样的话,你可以这样做:

String[] array = {"a", "a", "b", "c", "c", "c", "d", "e", "f", "f"};
Map<String, Long> map = new HashMap<>();

Stream.of(array)
      .distinct()
      .forEach(s -> map.put(s, 
          Stream.of(array)
                .filter(s::equals)
                .count()));

如果您只想要独特的单词:

String[] unique = Stream.of(array)
                        .distinct()
                        .toArray(String[]::new);