Skip to content

Commit

Permalink
refactored word search to be more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Jun 22, 2017
1 parent f726bc1 commit 9f78f5f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
27 changes: 21 additions & 6 deletions ob/ob_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,38 @@ def generate_planets(star):
yield nth_planet_of_star(star, planet_index)
planet_index += 1


def find_syllable(set, pattern):
all = (ob.get_syllable(set, i) for i in xrange(0, 0x100))
if pattern == '*':
return all

if pattern.startswith('*'):
pattern = pattern[1:]
syllables = (ob.get_syllable(set, i) for i in xrange(0, 0x100))
syllables = ifilter(lambda x: x.endswith(pattern), syllables)
syllables = ifilter(lambda x: x.endswith(pattern), all)
return syllables

elif pattern.endswith('*'):
pattern = pattern[:-1]
syllables = (ob.get_syllable(set, i) for i in xrange(0, 0x100))
syllables = ifilter(lambda x: x.startswith(pattern), syllables)
syllables = ifilter(lambda x: x.startswith(pattern), all)
return syllables

elif ob.is_syllable(set, pattern):
return [pattern]

else:
raise Exception("Invalid argument: '%s'" % pattern)


def find_prefix_syllable(pattern):
return find_syllable(ob.prefix, pattern)
return find_syllable(ob.prefix, pattern)


def find_suffix_syllable(pattern):
return find_syllable(ob.suffix, pattern)
return find_syllable(ob.suffix, pattern)


def find_words(prefix_pattern, suffix_pattern):
prefix = list(find_prefix_syllable(prefix_pattern))
suffix = list(find_suffix_syllable(suffix_pattern))
return ((p + s) for p in prefix for s in suffix)
31 changes: 31 additions & 0 deletions ob/test_ob_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,34 @@ def test_find_suffix_syllable(self):
results = ob_util.find_suffix_syllable('*yn')
self.assertEquals(results.next(), 'byn')
self.assertEquals(results.next(), 'syn')

def test_find_words_by_prefix(self):
results = list(ob_util.find_words('doz', '*'))
self.assertEquals(len(results), 256)
self.assertEquals(results[0], 'dozzod')

results = list(ob_util.find_words('do*', '*'))
self.assertEquals(len(results), 9*256)

results = list(ob_util.find_words('d*', '*'))
self.assertEquals(len(results), 26*256)

results = list(ob_util.find_words('doz', 'n*'))
self.assertEquals(len(results), 25)

results = list(ob_util.find_words('doz', 'ne*'))
self.assertEquals(len(results), 11)

def test_find_words_by_suffix(self):
results = list(ob_util.find_words('*', 'zod'))
self.assertEquals(len(results), 256)
self.assertEquals(results[0], 'dozzod')

results = list(ob_util.find_words('*', 'fy*'))
self.assertEquals(len(results), 3*256)

results = list(ob_util.find_words('*', 'f*'))
self.assertEquals(len(results), 16*256)

results = list(ob_util.find_words('*', '*'))
self.assertEquals(len(results), 256*256)
21 changes: 6 additions & 15 deletions ob_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,9 @@ def find_planet(addr_range, galaxy):
return planets


def find_words(pattern):
if pattern.startswith('*'):
syllable = pattern[1:]
if ob.is_suffix_syllable(syllable):
for prefix in xrange(0, 0x100):
print(ob.get_prefix(prefix) + syllable)

elif pattern.endswith('*'):
syllable = pattern[:-1]
if ob.is_prefix_syllable(syllable):
for suffix in xrange(0, 0x100):
print(syllable + ob.get_suffix(suffix))

def find_words(prefix, suffix):
for word in ob_util.find_words(prefix, suffix):
print(word)

if __name__ == '__main__':
parser = argparse.ArgumentParser()
Expand All @@ -92,7 +82,8 @@ def find_words(pattern):
name_from_address.add_argument('address', help='address to lookup name of')

word_parser = subparsers.add_parser('word', help='find words based on glob pattern')
word_parser.add_argument('pattern', help='pattern such as *zod or bin*')
word_parser.add_argument('--prefix', help='pattern for prefix such as *od or z*', default='*')
word_parser.add_argument('--suffix', help='pattern for suffix such as bi* or *n', default='*')

planet_parser = subparsers.add_parser('planet', help='find planet by partial name')
planet_parser.add_argument('-g', '--galaxy', default='0x0', help='galaxy to search in')
Expand All @@ -117,7 +108,7 @@ def find_words(pattern):
print(ob.to_ship_name(address))

elif args.command == 'word':
find_words(args.pattern)
find_words(args.prefix, args.suffix)

elif args.command == 'planet':
galaxy = int(args.galaxy, 16)
Expand Down

0 comments on commit 9f78f5f

Please sign in to comment.