Количество слов в хэш-таблицеJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Количество слов в хэш-таблице

Сообщение Anonymous »

В следующем коде находится wordCount

Код: Выделить всё

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);
}
}
}
на основе тестового файла под файлом «a_Christmas_carol.txt» файл должен содержать 4428 уникальных слов.
@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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»