-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsult.py
185 lines (158 loc) · 6.01 KB
/
insult.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
182
183
184
185
import os
import sqlite3
import uuid
from pathlib import Path
from random import choice
from typing import List, Union
amplifier_path = "word_lists/amplifiers.txt"
edder_path = "word_lists/edder.txt"
disgusting_path = "word_lists/disgusting.txt"
fucking_path = "word_lists/fucking.txt"
insult_path = "word_lists/insult.txt"
db = "insults.db"
class ListExhaustedException(Exception):
def __init__(self, word_type):
self.message = f"All possible {word_type} words have been used - request new id, or remove unique flag."
def __str__(self):
return self.message
class Insult:
def __init__(
self,
id: Union[str, None] = None,
subject: Union[str, None] = None,
unique: bool = False,
alliteration: bool = False,
nolog: bool = False,
):
self.id = id
self.subject = subject
self.unique = unique
self.alliteration = alliteration
self.nolog = nolog
self.con = self.get_db_con()
self.amplifier_list = self.read_words(amplifier_path)
self.edder_list = self.read_words(edder_path)
self.disgusting_list = self.read_words(disgusting_path)
self.fucking_list = self.read_words(fucking_path)
self.insult_list = self.read_words(insult_path)
self.found_amplifiers = []
def get_db_con(self):
if not os.path.isfile(db):
con = sqlite3.connect(db)
cur = con.cursor()
cur.execute(
"""
CREATE TABLE insults (
insult TEXT NOT NULL
);
"""
)
cur.execute(
"""
CREATE TABLE uuid_insult (
uuid TEXT NOT NULL,
insult_id INTEGER NOT NULL,
FOREIGN KEY(insult_id) REFERENCES insults(id)
)
"""
)
return con
return sqlite3.connect(db)
def read_words(self, file_path: str):
with open(file_path, "r") as f:
lines = f.readlines()
return [line.rstrip() for line in lines]
def find_amplifiers(self, word: str) -> List:
for amplifier in self.amplifier_list:
if amplifier in word:
self.found_amplifiers.append(amplifier)
def remove_found_amplifiers(self, word_list: List) -> List:
word_list_copy = word_list.copy()
for word in word_list_copy:
for amplifier in self.found_amplifiers:
if amplifier in word:
try:
word_list.remove(word)
except:
continue
return word_list
def get_all_logged_insults(self):
cur = self.con.cursor()
cur.execute("SELECT * FROM uuid_insult WHERE uuid = ?", (self.id,))
insult_ids = [row[1] for row in list(cur.fetchall())]
all_insults = []
for insult_id in insult_ids:
cur.execute("SELECT * FROM insults WHERE rowid = ?", (insult_id,))
all_insults.append(cur.fetchone())
return [insult[0] for insult in all_insults]
def remove_logged_words(self, word_list):
logged_insults = self.get_all_logged_insults()
logged_insults = " ".join(logged_insults)
word_list_copy = word_list.copy()
for word in word_list_copy:
if word.split(";")[0] in logged_insults:
word_list.remove(word)
return word_list
def log_insult(self, insult):
cur = self.con.cursor()
cur.execute("INSERT INTO insults VALUES(?)", (insult,))
insult_id = cur.lastrowid
self.con.commit()
cur.execute("INSERT INTO uuid_insult VALUES(?,?)", (self.id, insult_id))
self.con.commit()
def find_word_starting_with_letter(self, word_list: List, letter: str):
words_starting_with_letter = []
for word in word_list:
if word.startswith(letter):
words_starting_with_letter.append(word)
if words_starting_with_letter:
return choice(words_starting_with_letter)
else:
return choice(word_list)
def get_word(
self, word_list: List, word_type: str, letter: Union[str, None]
) -> Union[str, None]:
if self.unique:
word_list = self.remove_logged_words(word_list)
word_list = self.remove_found_amplifiers(word_list)
if not word_list:
raise ListExhaustedException(word_type)
if letter:
word = self.find_word_starting_with_letter(word_list, letter)
else:
word = choice(word_list)
self.find_amplifiers(word)
return word
def get_insult(self) -> (Union[None, str], str, str):
if self.nolog and self.unique:
return ("unique and nolog can not be used together", "")
if not self.id:
self.id = str(uuid.uuid1())
letter = None
edder = self.get_word(self.edder_list, "edder type", letter)
if self.alliteration:
letter = edder[0]
disgusting = self.get_word(self.disgusting_list, "disgusting", letter)
letter = None
fucking = self.get_word(self.fucking_list, "fucking", letter)
if self.alliteration:
letter = fucking[0]
insult_with_gender = self.get_word(self.insult_list, "insult", letter)
insult_with_gender = insult_with_gender.split(";")
insult = insult_with_gender[0]
gender = insult_with_gender[1]
if not self.subject:
article = 'din'
if gender == 'et':
article = 'dit'
full_insult = f"du er {edder} {disgusting}, {article} {fucking} {insult}"
else:
article = 'den'
if gender == 'et':
article = 'det'
full_insult = (
f"{self.subject} er {edder} {disgusting}, {article} {fucking} {insult}"
)
if not self.nolog:
self.log_insult(full_insult)
return (None, self.id, full_insult)