Skip to content

Instantly share code, notes, and snippets.

@cjavad
Created November 4, 2020 09:58
Show Gist options
  • Save cjavad/ac5c2ab1fca449ed37ad0dc3235d4285 to your computer and use it in GitHub Desktop.
Save cjavad/ac5c2ab1fca449ed37ad0dc3235d4285 to your computer and use it in GitHub Desktop.
Yes, i was bored.
# Character index based search.
# Each line is seperated into words
# each word is then categories into indexes
# INPUT_PATH = r"PATH"
text = """What the fuck did you just fucking say about me, you little bitch?
I'll have you know I graduated top of my class in the Navy Seals,
and I've been involved in numerous secret raids on Al-Quaeda,
and I have over 300 confirmed kills.
I am trained in gorilla warfare and I'm the top sniper in the entire US armed forces.
You are nothing to me but just another target.
I will wipe you the fuck out with precision the likes of which has never been seen before on this Earth,
mark my fucking words. You think you can get away with saying that shit to me over the Internet?
Think again, fucker.
As we speak I am contacting my secret network of spies across the USA
and your IP is being traced right now so you better prepare for the storm, maggot.
The storm that wipes out the pathetic little thing you call your life.
You're fucking dead, kid. I can be anywhere, anytime, and I can kill you in over seven hundred ways,
and that's just with my bare hands.
Not only am I extensively trained in unarmed combat,
but I have access to the entire arsenal of the United States Marine Corps
and I will use it to its full extent to wipe your miserable ass off the face of the continent,
you little shit. If only you could have known what unholy retribution your little "clever" comment
was about to bring down upon you, maybe you would have held your fucking tongue.
But you couldn't, you didn't, and now you're paying the price, you goddamn idiot.
I will shit fury all over you and you will drown in it.
You're fucking dead, kiddo."""
class TextIndex:
words = dict()
max_len = int()
word_dict = dict()
word_index = dict()
def __init__(self, input_text, seperator = " "):
words = input_text.split(seperator)
for word in words:
self.word_dict[len(self.word_dict)] = word
self.words[len(self.words)] = word
self.max_len = len(max(words, key=len)) - 1
self.word_index = self.create_index(self.word_index, 0)
def create_index(self, previous_index, depth):
for i, wi in enumerate(self.word_dict.copy().keys()):
word = self.word_dict[wi]
if not len(word) > depth:
del self.word_dict[wi]
continue
char = word[depth].lower()
if not char in previous_index.keys():
previous_index[char] = { "wi": [] }
previous_index[char]["wi"].append(wi)
for ci in previous_index.keys():
if len(ci) == 1:
previous_index[ci] = self.create_index(previous_index[ci], depth + 1)
return previous_index
def search_index_at(self, character, depth, override_index=False):
word_index_at = override_index or self.word_index.copy()
while depth > 0:
new_word_dict = {}
for char in word_index_at.copy().keys():
if not len(char) == 1: continue
if not character in word_index_at[char].keys():
del word_index_at[char]
else:
new_word_dict.update(word_index_at[char])
word_index_at = new_word_dict
depth -= 1
return word_index_at
def search_word_at(self, character, depth):
valid_index = set()
word_dict = self.search_index_at(character, depth)
if not character in word_dict:
return []
for wi in word_dict[character]["wi"]:
valid_index.add(wi)
return [self.words[i] for i in valid_index]
def search_term(self, term):
valid_index = set()
current_index = self.word_index.copy()
for depth, character in enumerate(term):
current_index = self.search_index_at(character, len(term) - 1 - depth, current_index)
for wi in current_index["wi"]:
valid_index.add(wi)
return [self.words[i] for i in valid_index]
def haha_python(self, term):
for word in self.words.keys():
if term in self.words[word]:
yield self.words[word]
# text = open(INPUT_PATH, "r").read()
text = text.replace("\n", " ").replace(".", "").replace(",", "")
index = TextIndex(text)
print(index.search_word_at("a", 1))
print(index.search_term("kiddo"));
for i in index.haha_python("sh"):
print(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment