-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter_occurences.py
69 lines (46 loc) · 2.19 KB
/
counter_occurences.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import collections
from itertools import chain
def counting_occurrences_list(nouns_list):
"""Given the list that contains all the nouns parsed, this function
counts the number of occurences of each noun int the list
Args:
list that contains the parsed nouns
Returns:
counted_nouns (Counter): A mapping between the noun and it's number of occurences
"""
counted_nouns = collections.Counter(nouns_list)
return counted_nouns
def count_in_file(filename_origin):
"""Given the ".txt" file that contains all the nouns parsed, this function
counts the number of occurences of each noun of the file
Args:
filename_origin (str): file that contains the parsed nouns.
In this case, "corpora_BNC.txt" or "corpora_reviews.txt"
Returns:
counted_nouns (Counter): A mapping between the noun and it's number of occurences
"""
#reads the file and count the number of occurences of each noun:
with open(filename_origin, 'r', encoding="utf-8") as f:
#counted_nouns is a kind of mapping between the strings of the file (noun)
#and it's occurence; example: {'film': 86286, ...,'movie': 80667}
counted_nouns = collections.Counter(chain.from_iterable(map(str.split, f)))
return (counted_nouns)
def counting_occurences_file(filename_origin, counting_file):
"""Given the ".txt" file that contains all the nouns parsed, this function
counts the number of occurences of each noun of the file and prints
in the counting_file the mapping done between each noun and it's occurrence
Args:
filename_origin (str): file that contains the parsed nouns.
In this case, "corpora_BNC.txt" or "corpora_reviews.txt"
counting_file (str): The ".txt" file that is going to contain
the mapping between the noun and it's occurrences
Returns:
None
"""
counted_nouns = count_in_file(filename_origin)
with open(counting_file, 'w', encoding="utf-8") as f:
print(counted_nouns, file=f)
#tests:
if __name__ == '__main__':
counting_occurences_file("corpora_BNC.txt", "BNC_counter.txt")
counting_occurences_file("corpora_reviews.txt", "counter_review.txt")