Created
December 8, 2015 13:19
-
-
Save bmwant/6527f44f632a2c57a2e4 to your computer and use it in GitHub Desktop.
Find least distance between two words in text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from collections import defaultdict | |
words = ('the', 'quisck', 'brown', 'fox', 'quick') | |
class WordDistanceFinder(object): | |
def __init__(self, words): | |
self.words = words | |
self.min_dist = len(words) | |
self._words_map = defaultdict(list) | |
self._build_words_map() | |
def _build_words_map(self): | |
"""Create a list of ascending indexes for each unique word""" | |
for index, word in enumerate(self.words): | |
self._words_map[word].append(index) | |
def distance(self, word_one, word_two): | |
pos_one = self._words_map[word_one] | |
pos_two = self._words_map[word_two] | |
for index_one in pos_one: | |
for index_two in pos_two: | |
new_dist = abs(index_one - index_two) | |
if new_dist < self.min_dist: | |
self.min_dist = new_dist | |
# Return not index distances, but words between them | |
return new_dist-1 | |
k = WordDistanceFinder(words) | |
print(k.distance('quick', 'the')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment