2 from collections
import namedtuple
9 from sqlalchemy
.sql
import func
11 import whoosh
.filedb
.filestore
12 import whoosh
.filedb
.fileindex
14 from whoosh
.qparser
import QueryParser
16 import whoosh
.spelling
18 from pokedex
.db
import connect
19 import pokedex
.db
.tables
as tables
20 from pokedex
.roomaji
import romanize
22 __all__
= ['open_index', 'lookup', 'random_lookup']
24 INTERMEDIATE_LOOKUP_RESULTS
= 25
25 MAX_LOOKUP_RESULTS
= 10
27 # Dictionary of table name => table class.
28 # Need the table name so we can get the class from the table name after we
29 # retrieve something from the index
38 indexed_tables
[cls
.__tablename__
] = cls
40 def open_index(directory
=None, session
=None, recreate
=False):
41 """Opens the whoosh index stored in the named directory and returns (index,
42 speller). If the index doesn't already exist, it will be created.
45 Directory containing the index. Defaults to a location within the
46 `pokedex` egg directory.
49 If the index needs to be created, this database session will be used.
50 Defaults to an attempt to connect to the default SQLite database
51 installed by `pokedex setup`.
54 If set to True, the whoosh index will be created even if it already
60 directory
= pkg_resources
.resource_filename('pokedex',
66 # Attempt to open or create the index
67 directory_exists
= os
.path
.exists(directory
)
68 if directory_exists
and not recreate
:
69 # Already exists; should be an index!
71 index
= whoosh
.index
.open_dir(directory
, indexname
='MAIN')
72 spell_store
= whoosh
.filedb
.filestore
.FileStorage(directory
)
73 speller
= whoosh
.spelling
.SpellChecker(spell_store
)
75 except whoosh
.index
.EmptyIndexError
as e
:
76 # Apparently not a real index. Fall out of the if and create it
79 # Delete and start over if we're going to bail anyway.
80 if directory_exists
and recreate
:
81 # Be safe and only delete if it looks like a whoosh index, i.e.,
82 # everything starts with _
83 if all(f
[0] == '_' for f
in os
.listdir(directory
)):
84 shutil
.rmtree(directory
)
85 directory_exists
= False
87 if not directory_exists
:
92 schema
= whoosh
.fields
.Schema(
93 name
=whoosh
.fields
.ID(stored
=True),
94 table
=whoosh
.fields
.ID(stored
=True),
95 row_id
=whoosh
.fields
.ID(stored
=True),
96 language
=whoosh
.fields
.STORED
,
97 display_name
=whoosh
.fields
.STORED
, # non-lowercased name
98 forme_name
=whoosh
.fields
.ID
,
101 index
= whoosh
.index
.create_in(directory
, schema
=schema
, indexname
='MAIN')
102 writer
= index
.writer()
104 # Index every name in all our tables of interest
105 # speller_entries becomes a list of (word, score) tuples; the score is 2
106 # for English names, 1.5 for Roomaji, and 1 for everything else. I think
107 # this biases the results in the direction most people expect, especially
108 # when e.g. German names are very similar to English names
110 for cls
in indexed_tables
.values():
111 q
= session
.query(cls
)
113 for row
in q
.yield_per(5):
114 # XXX need to give forme_name a dummy value because I can't search
115 # for explicitly empty fields. boo.
116 row_key
= dict(table
=unicode(cls
.__tablename__
),
117 row_id
=unicode(row
.id),
120 def add(name
, language
, score
):
121 writer
.add_document(name
=name
.lower(), display_name
=name
,
124 speller_entries
.append((name
.lower(), score
))
126 # If this is a form, mark it as such
127 if getattr(row
, 'forme_base_pokemon_id', None):
128 row_key
['forme_name'] = row
.forme_name
133 # Pokemon also get other languages
134 for foreign_name
in getattr(row
, 'foreign_names', []):
135 moonspeak
= foreign_name
.name
136 if name
== moonspeak
:
137 # Don't add the English name again as a different language;
138 # no point and it makes spell results confusing
141 add(moonspeak
, foreign_name
.language
.name
, 3)
144 if foreign_name
.language
.name
== 'Japanese':
145 roomaji
= romanize(foreign_name
.name
)
146 add(roomaji
, u
'Roomaji', 8)
150 # Construct and populate a spell-checker index. Quicker to do it all
151 # at once, as every call to add_* does a commit(), and those seem to be
153 speller
= whoosh
.spelling
.SpellChecker(index
.storage
)
154 speller
.add_scored_words(speller_entries
)
156 return index
, speller
159 class LanguageWeighting(whoosh
.scoring
.Weighting
):
160 """A scoring class that forces otherwise-equal English results to come
161 before foreign results.
164 def score(self
, searcher
, fieldnum
, text
, docnum
, weight
, QTF
=1):
165 doc
= searcher
.stored_fields(docnum
)
166 if doc
['language'] == None:
167 # English (well, "default"); leave it at 1
169 elif doc
['language'] == u
'Roomaji':
170 # Give Roomaji a bit of a boost, as it's most likely to be searched
173 # Everything else can drop down the totem pole
176 rx_is_number
= re
.compile('^\d+$')
178 LookupResult
= namedtuple('LookupResult',
179 ['object', 'name', 'language', 'exact'])
181 def _parse_table_name(name
):
182 """Takes a singular table name, table name, or table object and returns the
185 Returns None for a bogus name.
187 if hasattr(name
, '__tablename__'):
188 return getattr(name
, '__tablename__')
189 elif name
in indexed_tables
:
191 elif name
+ 's' in indexed_tables
:
194 # Bogus. Be nice and return dummy
198 def lookup(input, valid_types
=[], session
=None, indices
=None, exact_only
=False):
199 """Attempts to find some sort of object, given a database session and name.
201 Returns a list of named (object, name, language, exact) tuples. `object`
202 is a database object, `name` is the name under which the object was found,
203 `language` is the name of the language in which the name was found, and
204 `exact` is True iff this was an exact match.
206 This function currently ONLY does fuzzy matching if there are no exact
209 Formes are not returned unless requested; "Shaymin" will return only grass
212 Extraneous whitespace is removed with extreme prejudice.
215 - Names: "Eevee", "Surf", "Run Away", "Payapa Berry", etc.
216 - Foreign names: "Iibui", "Eivui"
217 - Fuzzy names in whatever language: "Evee", "Ibui"
218 - IDs: "133", "192", "250"
220 - Type restrictions. "type:psychic" will only return the type. This is
221 how to make ID lookup useful. Multiple type specs can be entered with
222 commas, as "move,item:1". If `valid_types` are provided, any type prefix
224 - Alternate formes can be specified merely like "wash rotom".
227 Name of the thing to look for.
230 A list of table objects or names, e.g., `['pokemon', 'moves']`. If
231 this is provided, only results in one of the given tables will be
235 A database session to use for retrieving objects. As with get_index,
236 if this is not provided, a connection to the default database will be
240 Tuple of index, speller as returned from `open_index()`. Defaults to
241 a call to `open_index()`.
244 If True, only exact matches are returned. If set to False (the
245 default), and the provided `name` doesn't match anything exactly,
246 spelling correction will be attempted.
253 index
, speller
= indices
255 index
, speller
= open_index()
257 name
= unicode(input).strip().lower()
261 # Remove any type prefix (pokemon:133) before constructing a query
263 prefix_chunk
, name
= name
.split(':', 1)
267 # Only use types from the query string if none were explicitly
269 prefixes
= prefix_chunk
.split(',')
270 valid_types
= [_
.strip() for _
in prefixes
]
274 return random_lookup(indices
=(index
, speller
),
276 valid_types
=valid_types
)
278 # Do different things depending what the query looks like
279 # Note: Term objects do an exact match, so we don't have to worry about a
280 # query parser tripping on weird characters in the input
281 if '*' in name
or '?' in name
:
283 query
= whoosh
.query
.Wildcard(u
'name', name
)
284 elif rx_is_number
.match(name
):
285 # Don't spell-check numbers!
287 query
= whoosh
.query
.Term(u
'row_id', name
)
290 query
= whoosh
.query
.Term(u
'name', name
) \
291 & whoosh
.query
.Term(u
'forme_name', u
'XXX')
293 # If there's a space in the input, this might be a form
295 form
, formless_name
= name
.split(' ', 1)
296 form_query
= whoosh
.query
.Term(u
'name', formless_name
) \
297 & whoosh
.query
.Term(u
'forme_name', form
)
298 query
= query | form_query
300 ### Filter by type of object
302 for valid_type
in valid_types
:
303 table_name
= _parse_table_name(valid_type
)
304 type_terms
.append(whoosh
.query
.Term(u
'table', table_name
))
307 query
= query
& whoosh
.query
.Or(type_terms
)
311 searcher
= index
.searcher()
312 searcher
.weighting
= LanguageWeighting() # XXX kosher? docs say search()
313 # takes a weighting kw but it
315 results
= searcher
.search(query
, limit
=INTERMEDIATE_LOOKUP_RESULTS
)
317 # Look for some fuzzy matches if necessary
318 if not exact_only
and not results
:
322 for suggestion
in speller
.suggest(name
, INTERMEDIATE_LOOKUP_RESULTS
):
323 query
= whoosh
.query
.Term('name', suggestion
)
324 results
.extend(searcher
.search(query
))
326 ### Convert results to db objects
329 for result
in results
:
331 seen_key
= result
['table'], result
['row_id']
334 seen
[seen_key
] = True
336 cls
= indexed_tables
[result
['table']]
337 obj
= session
.query(cls
).get(result
['row_id'])
339 objects
.append(LookupResult(object=obj
,
340 name
=result
['display_name'],
341 language
=result
['language'],
344 # Only return up to 10 matches; beyond that, something is wrong.
345 # We strip out duplicate entries above, so it's remotely possible that we
346 # should have more than 10 here and lost a few. The speller returns 25 to
347 # give us some padding, and should avoid that problem. Not a big deal if
348 # we lose the 25th-most-likely match anyway.
349 return objects
[:MAX_LOOKUP_RESULTS
]
352 def random_lookup(valid_types
=[], session
=None, indices
=None):
353 """Takes similar arguments as `lookup()`, but returns a random lookup
354 result from one of the provided `valid_types`.
358 for valid_type
in valid_types
:
359 table_name
= _parse_table_name(valid_type
)
361 tables
.append(indexed_tables
[table_name
])
364 tables
= indexed_tables
.values()
366 # Rather than create an array of many hundred items and pick randomly from
367 # it, just pick a number up to the total number of potential items, then
368 # pick randomly from that, and partition the whole range into chunks
372 count
= session
.query(table
).count()
374 partitions
.append((table
, count
))
376 n
= random
.randint(1, total
)
377 while n
> partitions
[0][1]:
378 n
-= partitions
[0][1]
381 return lookup(unicode(n
), valid_types
=[ partitions
[0][0] ],
382 indices
=indices
, session
=session
)