dict -> defaultdict
This commit is contained in:
parent
36588ce72f
commit
cd3a90101f
|
@ -6,8 +6,7 @@ FilterKeywords searches for merger specific keywords
|
|||
in an article and counts them.
|
||||
'''
|
||||
|
||||
# toDo: dict ändern!
|
||||
|
||||
from collections import defaultdict
|
||||
import re
|
||||
|
||||
from nltk.stem.porter import PorterStemmer
|
||||
|
@ -45,8 +44,8 @@ class FilterKeywords:
|
|||
# remove duplicates
|
||||
keywords = set(keyword_list)
|
||||
|
||||
# counts keywords in article
|
||||
dict_keywords = {}
|
||||
# counts keywords in article (default value: 0)
|
||||
dict_keywords = defaultdict(int)
|
||||
|
||||
# search for matchings in dictionary of input article
|
||||
for key in dict_input.keys():
|
||||
|
@ -54,10 +53,13 @@ class FilterKeywords:
|
|||
for kword in keywords:
|
||||
if re.match(kword, key):
|
||||
# if match, increase value of matching key
|
||||
## DORIS: Hier könntest du ein defaultdict verwenden, https://www.accelebrate.com/blog/using-defaultdict-python/
|
||||
if str(kword) in dict_keywords:
|
||||
dict_keywords[str(kword)] += dict_input[key]
|
||||
else:
|
||||
dict_keywords[str(kword)] = dict_input[key]
|
||||
|
||||
return dict_keywords
|
||||
return dict_keywords
|
||||
|
||||
if __name__ == '__main__':
|
||||
dict_test={'example':2, 'combined':5, 'sells':3}
|
||||
print(FilterKeywords.search_keywords(dict_test))
|
Loading…
Reference in New Issue