From 9f78f5fe98f60126c5af25caf5b8ada3d6eacab0 Mon Sep 17 00:00:00 2001 From: assaf Date: Wed, 21 Jun 2017 19:25:29 -0700 Subject: [PATCH] refactored word search to be more generic --- ob/ob_util.py | 27 +++++++++++++++++++++------ ob/test_ob_util.py | 31 +++++++++++++++++++++++++++++++ ob_cli.py | 21 ++++++--------------- 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/ob/ob_util.py b/ob/ob_util.py index c6813f3..fe92fbb 100644 --- a/ob/ob_util.py +++ b/ob/ob_util.py @@ -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) diff --git a/ob/test_ob_util.py b/ob/test_ob_util.py index 35d9576..da569f2 100644 --- a/ob/test_ob_util.py +++ b/ob/test_ob_util.py @@ -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) diff --git a/ob_cli.py b/ob_cli.py index 2657c8b..ae2890b 100755 --- a/ob_cli.py +++ b/ob_cli.py @@ -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() @@ -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') @@ -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)