Skip to content
Snippets Groups Projects
Commit 42158bbe authored by Morten Hannemose's avatar Morten Hannemose
Browse files

Update week6

parent 139a1414
No related branches found
No related tags found
No related merge requests found
Showing
with 10 additions and 14 deletions
"""Exercise 6.3-6.4.""" """Exercise 6.3-6.4."""
def word_histogram(lines : list) -> list: def word_histogram(lines : list) -> dict:
"""Return the word count histogram from the input lines. """Return the word count histogram from the input lines.
:param lines: The lines that are analyzed for word count. :param lines: The lines that are analyzed for word count.
...@@ -20,19 +20,15 @@ def extract_keyword(lines : str, ignore_list : list) -> dict: ...@@ -20,19 +20,15 @@ def extract_keyword(lines : str, ignore_list : list) -> dict:
if __name__ == "__main__": if __name__ == "__main__":
# here you can try out your functions # here you can try out your functions
print(word_histogram('I think therefore I am.')) lines = ['This is the first sentence of text for you', 'This is the second sentence of text', 'This is for you']
print("word_histogram")
print(word_histogram(lines))
# Ignore list of common words # Ignore list of common words
ignore_list = [ ignore_list = ['the', 'be', 'to', 'of', 'and', 'a', 'in', 'is', 'have', 'I']
'a', 'an', 'the', 'above', 'across', 'against', 'along', 'among', 'around',
'at', 'before', 'behind', 'below', 'beneath', 'beside', 'between', 'by',
'down', 'from', 'in', 'into', 'near', 'of', 'off', 'on', 'to', 'toward',
'under', 'upon', 'with', 'within','function', 'for', 'and', 'nor', 'but', 'or', 'yet', 'so']
# Example usage:
lines = [ "Write the function word_histogram, which takes as argument a list containing lines of a text.", "The function should ... ... ... ... ... make a histogram of words that occur in the text."]
keywords_result = extract_keyword(lines, ignore_list)
# Print the 5 most occurring keywords # Print the 5 most occurring keywords
print(keywords_result) print("extract_keywords")
print(extract_keyword(lines, ignore_list))
No preview for this file type
No preview for this file type
...@@ -22,7 +22,7 @@ class Week06WordHistogram(UTestCase): ...@@ -22,7 +22,7 @@ class Week06WordHistogram(UTestCase):
def test_WordHistogram(self): def test_WordHistogram(self):
from cp.ex06.word_histogram import word_histogram from cp.ex06.word_histogram import word_histogram
self.assertEqual(word_histogram(["Write the function word_histogram."," which takes as argument a list containing lines of a text."]),{'write': 1, 'the': 1, 'function': 1, 'wordhistogram': 1, 'which': 1, 'takes': 1, 'as': 1, 'argument': 1, 'a': 2, 'list': 1, 'containing': 1, 'lines': 1, 'of': 1, 'text': 1}) self.assertEqual(word_histogram(["Write the function word_histogram."," which takes as argument a list containing lines of a text."]),{'write': 1, 'the': 1, 'function': 1, 'wordhistogram': 1, 'which': 1, 'takes': 1, 'as': 1, 'argument': 1, 'a': 2, 'list': 1, 'containing': 1, 'lines': 1, 'of': 1, 'text': 1})
self.assertEqual(word_histogram(["The function should make a histogram of words that occur in the text.","Punctuation, spaces, numbers, and capitalization should be ignored.",]),{'the': 2, 'function': 1, 'should': 2, 'make': 1, 'a': 1, 'histogram': 1, 'of': 1, 'words': 1, 'that': 1, 'occur': 1, 'in': 1, 'text': 1, 'punctuation': 1, 'spaces': 1, 'numbers': 1, 'and': 1, 'capitalization': 1, 'be': 1, 'ignored': 1}) self.assertEqual(word_histogram(["The function should make a histogram of words that occur in the text.","Punctuation, spaces, and capitalization should be ignored.",]),{'the': 2, 'function': 1, 'should': 2, 'make': 1, 'a': 1, 'histogram': 1, 'of': 1, 'words': 1, 'that': 1, 'occur': 1, 'in': 1, 'text': 1, 'punctuation': 1, 'spaces': 1, 'and': 1, 'capitalization': 1, 'be': 1, 'ignored': 1})
self.assertEqual(word_histogram(["The function should return a dictionary, e.g. {'write': 2, 'the': 12, 'function': 7, …}",]),{'the': 2, 'function': 2, 'should': 1, 'return': 1, 'a': 1, 'dictionary': 1, 'eg': 1, 'write': 1}) self.assertEqual(word_histogram(["The function should return a dictionary, e.g. {'write': 2, 'the': 12, 'function': 7, …}",]),{'the': 2, 'function': 2, 'should': 1, 'return': 1, 'a': 1, 'dictionary': 1, 'eg': 1, 'write': 1})
class Week06ExtractKeywords(UTestCase): class Week06ExtractKeywords(UTestCase):
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment