Remove absolute HP from the capture chance formula.
[zzz-pokedex.git] / pokedex / lookup.py
index 1363c7a..5287c72 100644 (file)
@@ -26,7 +26,7 @@ __all__ = ['PokedexLookup']
 rx_is_number = re.compile('^\d+$')
 
 LookupResult = namedtuple('LookupResult',
-                          ['object', 'name', 'language', 'iso3166', 'exact'])
+    ['object', 'indexed_name', 'name', 'language', 'iso3166', 'exact'])
 
 class LanguageWeighting(whoosh.scoring.Weighting):
     """A scoring class that forces otherwise-equal English results to come
@@ -58,6 +58,7 @@ class PokedexLookup(object):
         for cls in (
             tables.Ability,
             tables.Item,
+            tables.Location,
             tables.Move,
             tables.Pokemon,
             tables.Type,
@@ -129,7 +130,6 @@ class PokedexLookup(object):
             language=whoosh.fields.STORED,
             iso3166=whoosh.fields.STORED,
             display_name=whoosh.fields.STORED,  # non-lowercased name
-            forme_name=whoosh.fields.ID,
         )
 
         self.index = whoosh.index.create_in(directory, schema=schema,
@@ -146,32 +146,39 @@ class PokedexLookup(object):
             q = session.query(cls)
 
             for row in q.yield_per(5):
-                # Need to give forme_name a dummy value because I can't
-                # search for explicitly empty fields.  Boo.
                 row_key = dict(table=unicode(cls.__tablename__),
-                               row_id=unicode(row.id),
-                               forme_name=u'__empty__')
+                               row_id=unicode(row.id))
 
                 def add(name, language, iso3166, score):
                     normalized_name = self.normalize_name(name)
+
                     writer.add_document(
                         name=normalized_name, display_name=name,
                         language=language, iso3166=iso3166,
                         **row_key
                     )
+
                     speller_entries.append((normalized_name, score))
 
-                # If this is a form, mark it as such
-                if getattr(row, 'forme_base_pokemon_id', None):
-                    row_key['forme_name'] = row.forme_name
 
-                name = row.name
-                add(name, None, u'us', 1)
+                # Add the basic English name to the index
+                if cls == tables.Pokemon:
+                    # Pokémon need their form name added
+                    # XXX kinda kludgy
+                    add(row.full_name, None, u'us', 1)
+
+                    # If this is a default form, ALSO add the unadorned name,
+                    # so 'Deoxys' alone will still do the right thing
+                    if row.forme_name and not row.forme_base_pokemon_id:
+                        add(row.name, None, u'us', 1)
+                else:
+                    add(row.name, None, u'us', 1)
 
-                # Pokemon also get other languages
+                # Some things also have other languages' names
+                # XXX other language form names..?
                 for foreign_name in getattr(row, 'foreign_names', []):
                     moonspeak = foreign_name.name
-                    if name == moonspeak:
+                    if row.name == moonspeak:
                         # Don't add the English name again as a different
                         # language; no point and it makes spell results
                         # confusing
@@ -253,6 +260,7 @@ class PokedexLookup(object):
             obj = self.session.query(cls).get(record['row_id'])
 
             results.append(LookupResult(object=obj,
+                                        indexed_name=record['name'],
                                         name=record['display_name'],
                                         language=record['language'],
                                         iso3166=record['iso3166'],
@@ -336,15 +344,7 @@ class PokedexLookup(object):
             query = whoosh.query.Term(u'row_id', name)
         else:
             # Not an integer
-            query = whoosh.query.Term(u'name', name) \
-                  & whoosh.query.Term(u'forme_name', u'__empty__')
-
-            # If there's a space in the input, this might be a form
-            if ' ' in name:
-                form, formless_name = name.split(' ', 1)
-                form_query = whoosh.query.Term(u'name', formless_name) \
-                           & whoosh.query.Term(u'forme_name', form)
-                query = query | form_query
+            query = whoosh.query.Term(u'name', name)
 
         ### Filter by type of object
         type_terms = []