-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
90 lines (66 loc) · 2.75 KB
/
utils.py
File metadata and controls
90 lines (66 loc) · 2.75 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import itertools
import json
import random
import re
import string
class Util:
# Class is created for the files to load only once.
def __init__(self, constantJSON='data/constants.json'):
with open(constantJSON, 'r') as file:
constants = json.load(file)
#self.keywords = constants["triggers"]
self.subreddits = constants["subreddits"]
with open('data/songs.json', 'r') as file:
songs = json.load(file)
self.all_lyrics = self._get_all_lyrics_list(songs)
self.spellchecker = SpellChecker(self.all_lyrics)
def _get_all_lyrics_list(self, songs):
song_list = [song for song in songs.values()]
massive_list = [lyric for lyric in song_list]
un_nested_list = list(itertools.chain.from_iterable(massive_list))
return un_nested_list
def get_next_lyric(self, text):
""" Checks if the trigger words to call the bot are present in the string """
is_return_lyric = False
text = str(text).replace("'","")
for lyric in self.all_lyrics:
# If this is the lyric to return.
if is_return_lyric:
return lyric
# Checks if comment is a lyric.
lyric = str(lyric).replace("'","")
try:
if re.search(text.lower(), lyric.lower(), re.IGNORECASE):
is_return_lyric = True
except re.error:
pass
# Returns nothing if not.
return None
"""def get_random_quote(self):
Returns random quote from quotes file
return random.choice(self.quotes).upper()"""
def get_subs(self):
"Returns a list of all the subs where the bot should be active."
return self.subreddits
class SpellChecker():
"""Find and fix simple spelling errors.
based on Peter Norvig
http://norvig.com/spell-correct.html
"""
def __init__(self, names):
self.model = set(names)
def __known(self, words):
for w in words:
if w in self.model:
return w
return None
def __edits(self, word):
splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = (a + b[1:] for a, b in splits if b)
transposes = (a + b[1] + b[0] + b[2:] for a, b in splits if len(b) > 1)
replaces = (a + c + b[1:] for a, b in splits for c in string.ascii_lowercase if b)
inserts = (a + c + b for a, b in splits for c in string.ascii_lowercase)
return itertools.chain(deletes, transposes, replaces, inserts)
def correct(self, word):
"""returns input word or fixed version if found"""
return self.__known([word]) or self.__known(self.__edits(word)) or word