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.
|
in an article and counts them.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
# toDo: dict ändern!
|
from collections import defaultdict
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from nltk.stem.porter import PorterStemmer
|
from nltk.stem.porter import PorterStemmer
|
||||||
|
@ -45,8 +44,8 @@ class FilterKeywords:
|
||||||
# remove duplicates
|
# remove duplicates
|
||||||
keywords = set(keyword_list)
|
keywords = set(keyword_list)
|
||||||
|
|
||||||
# counts keywords in article
|
# counts keywords in article (default value: 0)
|
||||||
dict_keywords = {}
|
dict_keywords = defaultdict(int)
|
||||||
|
|
||||||
# search for matchings in dictionary of input article
|
# search for matchings in dictionary of input article
|
||||||
for key in dict_input.keys():
|
for key in dict_input.keys():
|
||||||
|
@ -54,10 +53,13 @@ class FilterKeywords:
|
||||||
for kword in keywords:
|
for kword in keywords:
|
||||||
if re.match(kword, key):
|
if re.match(kword, key):
|
||||||
# if match, increase value of matching 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:
|
if str(kword) in dict_keywords:
|
||||||
dict_keywords[str(kword)] += dict_input[key]
|
dict_keywords[str(kword)] += dict_input[key]
|
||||||
else:
|
else:
|
||||||
dict_keywords[str(kword)] = dict_input[key]
|
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