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
14 import whoosh
.spelling
16 from pokedex
.db
import connect
17 import pokedex
.db
.tables
as tables
18 from pokedex
.roomaji
import romanize
20 __all__
= ['open_index', 'lookup']
22 # Dictionary of table name => table class.
23 # Need the table name so we can get the class from the table name after we
24 # retrieve something from the index
33 indexed_tables
[cls
.__tablename__
] = cls
35 def open_index(directory
=None, session
=None, recreate
=False):
36 """Opens the whoosh index stored in the named directory and returns (index,
37 speller). If the index doesn't already exist, it will be created.
40 Directory containing the index. Defaults to a location within the
41 `pokedex` egg directory.
44 If the index needs to be created, this database session will be used.
45 Defaults to an attempt to connect to the default SQLite database
46 installed by `pokedex setup`.
49 If set to True, the whoosh index will be created even if it already
55 directory
= pkg_resources
.resource_filename('pokedex',
61 # Attempt to open or create the index
62 directory_exists
= os
.path
.exists(directory
)
63 if directory_exists
and not recreate
:
64 # Already exists; should be an index!
66 index
= whoosh
.index
.open_dir(directory
, indexname
='MAIN')
67 spell_store
= whoosh
.filedb
.filestore
.FileStorage(directory
)
68 speller
= whoosh
.spelling
.SpellChecker(spell_store
)
70 except whoosh
.index
.EmptyIndexError
as e
:
71 # Apparently not a real index. Fall out of the if and create it
74 if not directory_exists
:
79 schema
= whoosh
.fields
.Schema(
80 name
=whoosh
.fields
.ID(stored
=True),
81 table
=whoosh
.fields
.STORED
,
82 row_id
=whoosh
.fields
.ID(stored
=True),
83 language
=whoosh
.fields
.STORED
,
86 index
= whoosh
.index
.create_in(directory
, schema
=schema
, indexname
='MAIN')
87 writer
= index
.writer()
89 # Index every name in all our tables of interest
90 # speller_entries becomes a list of (word, score) tuples; the score is 2
91 # for English names, 1.5 for Roomaji, and 1 for everything else. I think
92 # this biases the results in the direction most people expect, especially
93 # when e.g. German names are very similar to English names
95 for cls
in indexed_tables
.values():
96 q
= session
.query(cls
)
98 # Only index base Pokémon formes
99 if hasattr(cls
, 'forme_base_pokemon_id'):
100 q
= q
.filter_by(forme_base_pokemon_id
=None)
102 for row
in q
.yield_per(5):
103 row_key
= dict(table
=cls
.__tablename__
, row_id
=unicode(row
.id))
105 name
= row
.name
.lower()
106 writer
.add_document(name
=name
, **row_key
)
107 speller_entries
.append((name
, 1))
109 # Pokemon also get other languages
110 for foreign_name
in getattr(row
, 'foreign_names', []):
111 moonspeak
= foreign_name
.name
.lower()
112 if name
== moonspeak
:
113 # Don't add the English name again as a different language;
114 # no point and it makes spell results confusing
117 writer
.add_document(name
=moonspeak
,
118 language
=foreign_name
.language
.name
,
120 speller_entries
.append((moonspeak
, 3))
123 if foreign_name
.language
.name
== 'Japanese':
124 roomaji
= romanize(foreign_name
.name
).lower()
125 writer
.add_document(name
=roomaji
, language
='Roomaji',
127 speller_entries
.append((roomaji
, 8))
132 # Construct and populate a spell-checker index. Quicker to do it all
133 # at once, as every call to add_* does a commit(), and those seem to be
135 speller
= whoosh
.spelling
.SpellChecker(index
.storage
)
136 speller
.add_scored_words(speller_entries
)
138 return index
, speller
141 class LanguageWeighting(whoosh
.scoring
.Weighting
):
142 """A scoring class that forces otherwise-equal English results to come
143 before foreign results.
146 def score(self
, searcher
, fieldnum
, text
, docnum
, weight
, QTF
=1):
147 doc
= searcher
.stored_fields(docnum
)
148 if doc
['language'] == None:
149 # English (well, "default"); leave it at 1
151 elif doc
['language'] == u
'Roomaji':
152 # Give Roomaji a bit of a boost, as it's most likely to be searched
155 # Everything else can drop down the totem pole
158 rx_is_number
= re
.compile('^\d+$')
160 LookupResult
= namedtuple('LookupResult',
161 ['object', 'name', 'language', 'exact'])
162 def lookup(input, session
=None, indices
=None, exact_only
=False):
163 """Attempts to find some sort of object, given a database session and name.
165 Returns a list of named (object, name, language, exact) tuples. `object`
166 is a database object, `name` is the name under which the object was found,
167 `language` is the name of the language in which the name was found, and
168 `exact` is True iff this was an exact match.
170 This function currently ONLY does fuzzy matching if there are no exact
173 Formes are not returned; "Shaymin" will return only grass Shaymin.
176 - Names: "Eevee", "Surf", "Run Away", "Payapa Berry", etc.
177 - Foreign names: "Iibui", "Eivui"
178 - Fuzzy names in whatever language: "Evee", "Ibui"
179 - IDs: "pokemon 133", "move 192", "item 250"
180 - Dex numbers: "sinnoh 55", "133", "johto 180"
183 Name of the thing to look for.
186 A database session to use for retrieving objects. As with get_index,
187 if this is not provided, a connection to the default database will be
191 Tuple of index, speller as returned from `open_index()`. Defaults to
192 a call to `open_index()`.
195 If True, only exact matches are returned. If set to False (the
196 default), and the provided `name` doesn't match anything exactly,
197 spelling correction will be attempted.
204 index
, speller
= indices
206 index
, speller
= open_index()
208 name
= unicode(input).lower()
211 # If the input provided is a number, match it as an id. Otherwise, name.
212 # Term objects do an exact match, so we don't have to worry about a query
213 # parser tripping on weird characters in the input
214 if rx_is_number
.match(name
):
215 # Don't spell-check numbers!
217 query
= whoosh
.query
.Term(u
'row_id', name
)
220 query
= whoosh
.query
.Term(u
'name', name
)
223 searcher
= index
.searcher()
224 searcher
.weighting
= LanguageWeighting() # XXX kosher? docs say search()
225 # takes a weighting kw but it
227 results
= searcher
.search(query
)
229 # Look for some fuzzy matches if necessary
230 if not exact_only
and not results
:
234 for suggestion
in speller
.suggest(name
, 25):
235 query
= whoosh
.query
.Term('name', suggestion
)
236 results
.extend(searcher
.search(query
))
238 ### Convert results to db objects
241 for result
in results
:
243 seen_key
= result
['table'], result
['row_id']
246 seen
[seen_key
] = True
248 cls
= indexed_tables
[result
['table']]
249 obj
= session
.query(cls
).get(result
['row_id'])
250 objects
.append(LookupResult(object=obj
,
252 language
=result
['language'],
255 # Only return up to 10 matches; beyond that, something is wrong.
256 # We strip out duplicate entries above, so it's remotely possible that we
257 # should have more than 10 here and lost a few. The speller returns 25 to
258 # give us some padding, and should avoid that problem. Not a big deal if
259 # we lose the 25th-most-likely match anyway.