Код: Выделить всё
public List countWords(String filename) throws IOException {
List lines = readFile(filename);
for(String line : lines) {
line = removePunctuation(line);
List words = splitLineIntoWords(line);
for(String word : words) {
// lowercase the word to count all capitalization as same word
word = word.toLowerCase().trim();
if(word.isBlank()) {
continue;
}
// TODO implement
// Does this word exist in the hash table?
// If not, create a new entry with the word as the key
// and the data is the integer 1 (to represent one instance of the word)
// If the value does exist, update the node's data to add 1 to the count
// because another instance of the word has been found
Link wordLink = wordMap.find(word);
if (wordLink != null) {
// If the word exists, update the value to add 1 to the count
wordMap.insert(word, wordMap.hashFunc(word) + 1);
} else {
// If the word doesn't exist, add the word as a new key with a value of 1
wordMap.insert(word, 1);
}
}
}
@Test
void testCountWords() бросает IOException {
Listwords = wordCount.countWords("a_christmas_carol.txt");
Код: Выделить всё
Assertions.assertEquals(4428, words.size(), "a_christmas_carol.txt file should contain 4428 unique words");
Link tiny = wordCount.wordMap.find("tiny");
Assertions.assertNotNull(tiny, "The word 'tiny' should exist");
Assertions.assertEquals(26, tiny.getData(), "The word 'tiny' should occur 26 times");
Link humbug = wordCount.wordMap.find("humbug");
Assertions.assertNotNull(humbug, "The word 'humbug' should exist");
Assertions.assertEquals(33, humbug.getData(), "The word 'humbug' should occur 33 times");
}
Ожидаемое: 4428
Фактическое: 32304
Фактические результаты: число wordCount слишком велико, оно достигает 32304, хотя при добавлении метода uniqueword значение wordCount все еще слишком велико и не близко к ожидаемому числу.
Подробнее здесь: https://stackoverflow.com/questions/747 ... word-count
Мобильная версия