0
0
Fork 0
EnglishPal/app/test_bug511_Bosh.py

57 lines
2.0 KiB
Python
Executable File

###########################################################################
# Copyright 2019 (C) Hui Lan <hui.lan@cantab.net>
# Written permission must be obtained from the author for commercial uses.
###########################################################################
import unittest
from wordfreqCMD import remove_punctuation, freq, sort_in_descending_order
from WordFreq import WordFreq
class TestWordFrequency(unittest.TestCase):
def test_word_frequency_normal_case(self):
text = "BANANA; Banana, apple ORANGE Banana banana."
wf = WordFreq(text)
result = wf.get_freq()
expected = [('banana', 4), ('orange', 1), ('apple', 1)]
self.assertEqual(result, expected)
def test_word_frequency_with_long_word(self):
text = "apple banana " + "a" * 31 + " orange banana apple"
wf = WordFreq(text, max_word_length=30)
result = wf.get_freq()
expected = [('banana', 2), ('apple', 2), ('orange', 1)]
self.assertEqual(result, expected)
def test_word_frequency_all_long_words(self):
text = "a" * 31 + " " + "b" * 32 + " " + "c" * 33
wf = WordFreq(text, max_word_length=30)
result = wf.get_freq()
expected = []
self.assertEqual(result, expected)
def test_word_frequency_with_punctuation(self):
text = "Hello, world! Hello... hello; 'hello' --world--"
wf = WordFreq(text)
result = wf.get_freq()
expected = [('hello', 4), ('world', 2)]
self.assertEqual(result, expected)
def test_word_frequency_empty_string(self):
text = ""
wf = WordFreq(text)
result = wf.get_freq()
expected = []
self.assertEqual(result, expected)
def test_word_frequency_with_max_length_parameter(self):
text = "apple banana apple"
wf = WordFreq(text, max_word_length=5)
result = wf.get_freq()
expected = [('apple', 2)]
self.assertEqual(result, expected)
if __name__ == '__main__':
unittest.main()