-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken_freq.py
68 lines (52 loc) · 1.82 KB
/
token_freq.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
# Young Jin Jung
# Sun Mar 14 17:02:28 EST 2021
from collections import Counter
class TokenFrequency:
"""
Token Frequency Class
It can be initialized with pre-made list of tokens.
Or individual tokens can be fed.
Attributes
----------
token_list : list
list of given tokens
counter : Counter (dict)
sorted dictionary that consists of {'token_name' : frequency}
Methods
-------
add_token(token):
append a token into the token_list and returns updated counter
clear_list():
clear token_list
get_token_list():
returns token_list
get_frequency():
returns Counter (dict) with current token_list
def get_most_freq_token():
returns the most frequently appeared token name (can be None)
def get_most_freq_token_not_none():
returns the most frequently appeared token name.
if none, returns the next most frequent non-None token name.
"""
# can initialize the class with a list of tokens
def __init__(self, tokenList=[]):
self.token_list = tokenList
self.counter = Counter(self.token_list)
# add single token to the token list
def add_token(self, token) -> Counter:
self.token_list.append(token)
self.counter = Counter(self.token_list)
return self.counter
def clear_list(self):
self.token_list = []
def get_token_list(self):
return self.token_list
def get_frequency(self):
return self.counter
def get_most_freq_token(self):
return self.counter.most_common(1)[0][0]
def get_most_freq_token_not_none(self):
if self.counter.most_common(1)[0][0] != None:
return self.counter.most_common(1)[0][0]
else:
return self.counter.most_common(2)[1][0]