-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkufurnet.py
More file actions
73 lines (57 loc) · 2.58 KB
/
Copy pathkufurnet.py
File metadata and controls
73 lines (57 loc) · 2.58 KB
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
import re
from configurator import Configurator
class KufurNet:
def __init__(self):
self.cfg = Configurator()
self.black_starts = self.cfg.get_start_words()
self.black_in = self.cfg.get_in_words()
self.black_exact = self.cfg.get_exact_words()
self.stop_words = self.cfg.get_stop_words()
def ml_anlayze(self, text: str):
# TODO : This section will be defined by APO.
pass
def filter_analyze(self, text: str):
words = self.get_words(text)
if len(words):
black_list = []
for word in words:
if word in self.black_exact:
black_list.append(word)
for black in self.black_in:
if black in word:
black_list.append(word)
for black in self.black_starts:
if word.startswith(black):
black_list.append(word)
return len(black_list) > 0, round((len(black_list) / len(words)), 2), black_list
return {
"status": True,
"result": {
"is_black": False,
"black_score": 0,
"black_list": []
}
}
def get_words(self, text):
text = text.replace(",", " ")
text = text.replace("'", " ")
text = text.replace('.', " ")
text = re.sub(r'[^\w\s]', '', text.lower()) # remove puncts.
text = text.replace(" ", " ")
text = text.replace(" ", " ")
words = text.split(" ")
words = [word for word in words if len(word) > 1]
return [word for word in words if word not in self.stop_words]
def validate_input(self, input_message):
if not isinstance(input_message, dict):
raise TypeError(f"Input message should be dict but you gave {type(input_message)} type.")
if "text" not in input_message.keys():
raise KeyError(f"Input message does not contain key variable as 'text'.")
if len(input_message.keys()) != 1:
raise ValueError(f"Length of item should be 1, but you gave {len(input_message.keys())}.")
if len(input_message["text"]) <= 2:
raise ValueError(
f"Please check min character constraint in the message. Lower limit is 2 char. Your message has {len(input_message['text'])} char.")
if len(input_message["text"]) >= 200:
raise ValueError(
f"Please check max character constraint in the message. Upper limit is 200 char. Your message has {len(input_message['text'])} char.")