-
Notifications
You must be signed in to change notification settings - Fork 0
/
ABClf.py
182 lines (155 loc) · 3.64 KB
/
ABClf.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import pickle
import logging
import pandas as pd
from parse import compile
from transliterate import translit
from typing import Set, List, Dict
chars_to_remove = {
'!',
'"',
'#',
'%',
'&',
'(',
')',
'*',
'+',
',',
'-',
'.',
'/',
':',
';',
'<',
'=',
'>',
'?',
'[',
']',
'_',
'`',
'«',
'°',
'²',
'³',
'µ',
'·',
'»',
'½',
'‑',
'–',
'‘',
'’',
'“',
'”',
'„',
'•',
'…',
'‰',
'″',
'₂',
'₃',
'€',
'™',
'→',
'−',
'∕',
'😀',
'😉',
'🙁',
'🙂'
}
def is_alpha(token: str) -> bool:
"""Checks if the input string is strictly lowercase without numerals.
Args:
token (str): Input text.
Returns:
bool: Result of checking.
"""
import re
pattern = "^[a-zšđčćž]+$"
compiled_pattern = re.compile(pattern)
return bool(compiled_pattern.match(token))
def preprocess(s: str) -> str:
"""Removes unusual characters and lowercases the string.
Args:
s (str): input string.
Returns:
str: output string.
"""
for c in chars_to_remove:
s = s.replace(c, "")
s = s.casefold()
return s
def count_variants(s: str, lex: dict):
"""Counts the variant specific words in the preprocessed input string based on the lexicon lex.
Returns tuple (counts, per_token_breakdown).
Counts look like this:
{"A":3, "B":0}.
per_token is a dictionary with all the words detected, their counts and their variant:
{"word1":
{"count":3, "variant":"A"}
}
Args:
s (str): Input string.
lex (dict): Lexicon.
Returns:
results (tuple): (counts, per_token).
"""
counts = dict()
per_token = dict()
for word in preprocess(s).split():
if not is_alpha(word):
continue
variant = lex.get(word, None)
if not variant:
continue
logging.debug(f"Found word {word}, presumed variant: {variant}.")
counts[variant] = counts.get(variant, 0) + 1
if word in per_token.keys():
per_token[word]["count"] += 1
else:
per_token[word] = {"variant": variant, "count": 1}
return counts, per_token
def counts_to_category(counts: dict) -> str:
"""Discretizes counts like {"A": 2, "B":0} to
categories A, B, MIX, UNK.
Args:
counts (dict): result of count_variants function.
Returns:
str: category.
"""
A = counts.get("A", 0)
B = counts.get("B",0)
if A > 2*B:
return "A"
elif B > 2*A:
return "B"
elif A == B == 0:
return "UNK"
else:
return "MIX"
def load_lexicon(balanced=False)-> dict:
"""Loads 'lexicon.pickle'.
Args:
balanced (bool, optional): Whether or not to use balanced lexicon (equal number of A and B keys).
Defaults to False.
Returns:
dict: lexicon for variety identification.
"""
with open(f"lexicon{'_balanced' if balanced else ''}.pickle", "rb") as f:
lex = pickle.load(f)
return lex
def get_variant(text: str, lex=None) -> str:
"""Quick way to classify text.
Loads the lexicon, preprocesses the string. Returns the predicted
category {'A', 'B', 'UNK', 'MIX'}.
Args:
text (str): input string.
Returns:
str: category {'A', 'B', 'UNK', 'MIX'}
"""
if not lex:
lex = load_lexicon()
variant_detector_count = count_variants(text, lex)[0]
return counts_to_category(variant_detector_count)