2 from collections
import namedtuple
8 from sqlalchemy
.sql
import func
10 import whoosh
.filedb
.filestore
11 import whoosh
.filedb
.fileindex
13 from whoosh
.qparser
import QueryParser
15 import whoosh
.spelling
17 from pokedex
.db
import connect
18 import pokedex
.db
.tables
as tables
19 from pokedex
.roomaji
import romanize
21 __all__
= ['open_index', 'lookup']
23 INTERMEDIATE_LOOKUP_RESULTS
= 25
24 MAX_LOOKUP_RESULTS
= 10
26 # Dictionary of table name => table class.
27 # Need the table name so we can get the class from the table name after we
28 # retrieve something from the index
37 indexed_tables
[cls
.__tablename__
] = cls
39 def open_index(directory
=None, session
=None, recreate
=False):
40 """Opens the whoosh index stored in the named directory and returns (index,
41 speller). If the index doesn't already exist, it will be created.
44 Directory containing the index. Defaults to a location within the
45 `pokedex` egg directory.
48 If the index needs to be created, this database session will be used.
49 Defaults to an attempt to connect to the default SQLite database
50 installed by `pokedex setup`.
53 If set to True, the whoosh index will be created even if it already
59 directory
= pkg_resources
.resource_filename('pokedex',
65 # Attempt to open or create the index
66 directory_exists
= os
.path
.exists(directory
)
67 if directory_exists
and not recreate
:
68 # Already exists; should be an index!
70 index
= whoosh
.index
.open_dir(directory
, indexname
='MAIN')
71 spell_store
= whoosh
.filedb
.filestore
.FileStorage(directory
)
72 speller
= whoosh
.spelling
.SpellChecker(spell_store
)
74 except whoosh
.index
.EmptyIndexError
as e
:
75 # Apparently not a real index. Fall out of the if and create it
78 # Delete and start over if we're going to bail anyway.
79 if directory_exists
and recreate
:
80 # Be safe and only delete if it looks like a whoosh index, i.e.,
81 # everything starts with _
82 if all(f
[0] == '_' for f
in os
.listdir(directory
)):
83 shutil
.rmtree(directory
)
84 directory_exists
= False
86 if not directory_exists
:
91 schema
= whoosh
.fields
.Schema(
92 name
=whoosh
.fields
.ID(stored
=True),
93 table
=whoosh
.fields
.ID(stored
=True),
94 row_id
=whoosh
.fields
.ID(stored
=True),
95 language
=whoosh
.fields
.STORED
,
96 display_name
=whoosh
.fields
.STORED
, # non-lowercased name
97 forme_name
=whoosh
.fields
.ID
,
100 index
= whoosh
.index
.create_in(directory
, schema
=schema
, indexname
='MAIN')
101 writer
= index
.writer()
103 # Index every name in all our tables of interest
104 # speller_entries becomes a list of (word, score) tuples; the score is 2
105 # for English names, 1.5 for Roomaji, and 1 for everything else. I think
106 # this biases the results in the direction most people expect, especially
107 # when e.g. German names are very similar to English names
109 for cls
in indexed_tables
.values():
110 q
= session
.query(cls
)
112 for row
in q
.yield_per(5):
113 # XXX need to give forme_name a dummy value because I can't search
114 # for explicitly empty fields. boo.
115 row_key
= dict(table
=unicode(cls
.__tablename__
),
116 row_id
=unicode(row
.id),
119 def add(name
, language
, score
):
120 writer
.add_document(name
=name
.lower(), display_name
=name
,
123 speller_entries
.append((name
.lower(), score
))
125 # If this is a form, mark it as such
126 if getattr(row
, 'forme_base_pokemon_id', None):
127 row_key
['forme_name'] = row
.forme_name
132 # Pokemon also get other languages
133 for foreign_name
in getattr(row
, 'foreign_names', []):
134 moonspeak
= foreign_name
.name
135 if name
== moonspeak
:
136 # Don't add the English name again as a different language;
137 # no point and it makes spell results confusing
140 add(moonspeak
, foreign_name
.language
.name
, 3)
143 if foreign_name
.language
.name
== 'Japanese':
144 roomaji
= romanize(foreign_name
.name
)
145 add(roomaji
, u
'Roomaji', 8)
149 # Construct and populate a spell-checker index. Quicker to do it all
150 # at once, as every call to add_* does a commit(), and those seem to be
152 speller
= whoosh
.spelling
.SpellChecker(index
.storage
)
153 speller
.add_scored_words(speller_entries
)
155 return index
, speller
158 class LanguageWeighting(whoosh
.scoring
.Weighting
):
159 """A scoring class that forces otherwise-equal English results to come
160 before foreign results.
163 def score(self
, searcher
, fieldnum
, text
, docnum
, weight
, QTF
=1):
164 doc
= searcher
.stored_fields(docnum
)
165 if doc
['language'] == None:
166 # English (well, "default"); leave it at 1
168 elif doc
['language'] == u
'Roomaji':
169 # Give Roomaji a bit of a boost, as it's most likely to be searched
172 # Everything else can drop down the totem pole
175 rx_is_number
= re
.compile('^\d+$')
177 LookupResult
= namedtuple('LookupResult',
178 ['object', 'name', 'language', 'exact'])
179 def lookup(input, valid_types
=[], session
=None, indices
=None, exact_only
=False):
180 """Attempts to find some sort of object, given a database session and name.
182 Returns a list of named (object, name, language, exact) tuples. `object`
183 is a database object, `name` is the name under which the object was found,
184 `language` is the name of the language in which the name was found, and
185 `exact` is True iff this was an exact match.
187 This function currently ONLY does fuzzy matching if there are no exact
190 Formes are not returned; "Shaymin" will return only grass Shaymin.
193 - Names: "Eevee", "Surf", "Run Away", "Payapa Berry", etc.
194 - Foreign names: "Iibui", "Eivui"
195 - Fuzzy names in whatever language: "Evee", "Ibui"
196 - IDs: "133", "192", "250"
198 - Type restrictions. "type:psychic" will only return the type. This is
199 how to make ID lookup useful. Multiple type specs can be entered with
200 commas, as "move,item:1". If `valid_types` are provided, any type prefix
202 - Alternate formes can be specified merely like "wash rotom".
205 Name of the thing to look for.
208 A list of table objects or names, e.g., `['pokemon', 'moves']`. If
209 this is provided, only results in one of the given tables will be
213 A database session to use for retrieving objects. As with get_index,
214 if this is not provided, a connection to the default database will be
218 Tuple of index, speller as returned from `open_index()`. Defaults to
219 a call to `open_index()`.
222 If True, only exact matches are returned. If set to False (the
223 default), and the provided `name` doesn't match anything exactly,
224 spelling correction will be attempted.
231 index
, speller
= indices
233 index
, speller
= open_index()
235 name
= unicode(input).lower()
239 # Remove any type prefix (pokemon:133) before constructing a query
241 prefix_chunk
, name
= name
.split(':', 2)
242 prefixes
= prefix_chunk
.split(',')
244 # Only use types from the query string if none were explicitly
246 valid_types
= prefixes
248 # Do different things depending what the query looks like
249 # Note: Term objects do an exact match, so we don't have to worry about a
250 # query parser tripping on weird characters in the input
251 if '*' in name
or '?' in name
:
253 query
= whoosh
.query
.Wildcard(u
'name', name
)
254 elif rx_is_number
.match(name
):
255 # Don't spell-check numbers!
257 query
= whoosh
.query
.Term(u
'row_id', name
)
260 query
= whoosh
.query
.Term(u
'name', name
) \
261 & whoosh
.query
.Term(u
'forme_name', u
'XXX')
263 # If there's a space in the input, this might be a form
265 form
, formless_name
= name
.split(' ', 2)
266 form_query
= whoosh
.query
.Term(u
'name', formless_name
) \
267 & whoosh
.query
.Term(u
'forme_name', form
)
268 query
= query | form_query
270 ### Filter by type of object
272 for valid_type
in valid_types
:
273 if hasattr(valid_type
, '__tablename__'):
274 table_name
= getattr(valid_type
, '__tablename__')
275 elif valid_type
in indexed_tables
:
276 table_name
= valid_type
277 elif valid_type
+ 's' in indexed_tables
:
278 table_name
= valid_type
+ 's'
280 # Bogus. Be nice and ignore it
283 type_terms
.append(whoosh
.query
.Term(u
'table', table_name
))
286 query
= query
& whoosh
.query
.Or(type_terms
)
290 searcher
= index
.searcher()
291 searcher
.weighting
= LanguageWeighting() # XXX kosher? docs say search()
292 # takes a weighting kw but it
294 results
= searcher
.search(query
, limit
=INTERMEDIATE_LOOKUP_RESULTS
)
296 # Look for some fuzzy matches if necessary
297 if not exact_only
and not results
:
301 for suggestion
in speller
.suggest(name
, INTERMEDIATE_LOOKUP_RESULTS
):
302 query
= whoosh
.query
.Term('name', suggestion
)
303 results
.extend(searcher
.search(query
))
305 ### Convert results to db objects
308 for result
in results
:
310 seen_key
= result
['table'], result
['row_id']
313 seen
[seen_key
] = True
315 cls
= indexed_tables
[result
['table']]
316 obj
= session
.query(cls
).get(result
['row_id'])
318 objects
.append(LookupResult(object=obj
,
319 name
=result
['display_name'],
320 language
=result
['language'],
323 # Only return up to 10 matches; beyond that, something is wrong.
324 # We strip out duplicate entries above, so it's remotely possible that we
325 # should have more than 10 here and lost a few. The speller returns 25 to
326 # give us some padding, and should avoid that problem. Not a big deal if
327 # we lose the 25th-most-likely match anyway.
328 return objects
[:MAX_LOOKUP_RESULTS
]