2653b9a5e40298c9d84c2e12d30a74777f663a02
2 from collections
import namedtuple
7 from sqlalchemy
.sql
import func
9 import whoosh
.filedb
.filestore
10 import whoosh
.filedb
.fileindex
12 from whoosh
.qparser
import QueryParser
13 import whoosh
.spelling
15 from pokedex
.db
import connect
16 import pokedex
.db
.tables
as tables
17 from pokedex
.roomaji
import romanize
19 # Dictionary of table name => table class.
20 # Need the table name so we can get the class from the table name after we
21 # retrieve something from the index
30 indexed_tables
[cls
.__tablename__
] = cls
32 # Dictionary of extra keys to file types of objects under, e.g. Pokémon can
33 # also be looked up purely by number
36 lambda row
: u
"move %d" % row
.id,
39 lambda row
: unicode(row
.id),
43 def open_index(directory
=None, session
=None, recreate
=False):
44 """Opens the whoosh index stored in the named directory and returns (index,
45 speller). If the index doesn't already exist, it will be created.
48 Directory containing the index. Defaults to a location within the
49 `pokedex` egg directory.
52 If the index needs to be created, this database session will be used.
53 Defaults to an attempt to connect to the default SQLite database
54 installed by `pokedex setup`.
57 If set to True, the whoosh index will be created even if it already
63 directory
= pkg_resources
.resource_filename('pokedex',
69 # Attempt to open or create the index
70 directory_exists
= os
.path
.exists(directory
)
71 if directory_exists
and not recreate
:
72 # Already exists; should be an index!
74 index
= whoosh
.index
.open_dir(directory
, indexname
='MAIN')
75 spell_store
= whoosh
.filedb
.filestore
.FileStorage(directory
)
76 speller
= whoosh
.spelling
.SpellChecker(spell_store
)
78 except whoosh
.index
.EmptyIndexError
as e
:
79 # Apparently not a real index. Fall out of the if and create it
82 if not directory_exists
:
87 schema
= whoosh
.fields
.Schema(
88 name
=whoosh
.fields
.ID(stored
=True),
89 table
=whoosh
.fields
.STORED
,
90 row_id
=whoosh
.fields
.STORED
,
91 language
=whoosh
.fields
.STORED
,
94 index
= whoosh
.index
.create_in(directory
, schema
=schema
, indexname
='MAIN')
95 writer
= index
.writer()
97 # Index every name in all our tables of interest
98 # speller_entries becomes a list of (word, score) tuples; the score is 2
99 # for English names, 1.5 for Roomaji, and 1 for everything else. I think
100 # this biases the results in the direction most people expect, especially
101 # when e.g. German names are very similar to English names
103 for cls
in indexed_tables
.values():
104 q
= session
.query(cls
)
106 # Only index base Pokémon formes
107 if hasattr(cls
, 'forme_base_pokemon_id'):
108 q
= q
.filter_by(forme_base_pokemon_id
=None)
110 for row
in q
.yield_per(5):
111 row_key
= dict(table
=cls
.__tablename__
, row_id
=row
.id)
113 name
= row
.name
.lower()
114 writer
.add_document(name
=name
, **row_key
)
115 speller_entries
.append((name
, 1))
117 for extra_key_func
in extra_keys
.get(cls
, []):
118 extra_key
= extra_key_func(row
)
119 writer
.add_document(name
=extra_key
, **row_key
)
121 # Pokemon also get other languages
122 for foreign_name
in getattr(row
, 'foreign_names', []):
123 moonspeak
= foreign_name
.name
.lower()
124 if name
== moonspeak
:
125 # Don't add the English name again as a different language;
126 # no point and it makes spell results confusing
129 writer
.add_document(name
=moonspeak
,
130 language
=foreign_name
.language
.name
,
132 speller_entries
.append((moonspeak
, 3))
135 if foreign_name
.language
.name
== 'Japanese':
136 roomaji
= romanize(foreign_name
.name
).lower()
137 writer
.add_document(name
=roomaji
, language
='Roomaji',
139 speller_entries
.append((roomaji
, 8))
144 # Construct and populate a spell-checker index. Quicker to do it all
145 # at once, as every call to add_* does a commit(), and those seem to be
147 speller
= whoosh
.spelling
.SpellChecker(index
.storage
)
148 speller
.add_scored_words(speller_entries
)
150 return index
, speller
153 LookupResult
= namedtuple('LookupResult',
154 ['object', 'name', 'language', 'exact'])
155 def lookup(name
, session
=None, indices
=None, exact_only
=False):
156 """Attempts to find some sort of object, given a database session and name.
158 Returns a list of named (object, name, language, exact) tuples. `object`
159 is a database object, `name` is the name under which the object was found,
160 `language` is the name of the language in which the name was found, and
161 `exact` is True iff this was an exact match.
163 This function currently ONLY does fuzzy matching if there are no exact
166 Formes are not returned; "Shaymin" will return only grass Shaymin.
169 - Pokémon names: "Eevee"
172 Name of the thing to look for.
175 A database session to use for retrieving objects. As with get_index,
176 if this is not provided, a connection to the default database will be
180 Tuple of index, speller as returned from `open_index()`. Defaults to
181 a call to `open_index()`.
184 If True, only exact matches are returned. If set to False (the
185 default), and the provided `name` doesn't match anything exactly,
186 spelling correction will be attempted.
193 index
, speller
= indices
195 index
, speller
= open_index()
201 # Look for exact name. A Term object does an exact match, so we don't have
202 # to worry about a query parser tripping on weird characters in the input
203 searcher
= index
.searcher()
204 query
= whoosh
.query
.Term('name', name
.lower())
205 results
= searcher
.search(query
)
207 # Look for some fuzzy matches if necessary
208 if not exact_only
and not results
:
212 for suggestion
in speller
.suggest(name
, 10):
213 query
= whoosh
.query
.Term('name', suggestion
)
214 results
.extend(searcher
.search(query
))
216 ### Convert results to db objects
219 for result
in results
:
221 # Note! The speller prefers English names, but the query does not. So
222 # "latias" comes over "ratiasu". "latias" matches only the English
223 # row, comes out first, and all is well.
224 # However! The speller could then return "foo" which happens to be the
225 # name for two different things in different languages, and the
226 # non-English one could appear preferred. This is not very likely.
227 seen_key
= result
['table'], result
['row_id']
230 seen
[seen_key
] = True
232 cls
= indexed_tables
[result
['table']]
233 obj
= session
.query(cls
).get(result
['row_id'])
234 objects
.append(LookupResult(object=obj
,
236 language
=result
['language'],