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
99 for cls
in indexed_tables
.values():
100 q
= session
.query(cls
)
102 # Only index base Pokémon formes
103 if hasattr(cls
, 'forme_base_pokemon_id'):
104 q
= q
.filter_by(forme_base_pokemon_id
=None)
106 for row
in q
.yield_per(5):
107 row_key
= dict(table
=cls
.__tablename__
, row_id
=row
.id)
109 name
= row
.name
.lower()
110 writer
.add_document(name
=name
, **row_key
)
111 speller_entries
.append(name
)
113 for extra_key_func
in extra_keys
.get(cls
, []):
114 extra_key
= extra_key_func(row
)
115 writer
.add_document(name
=extra_key
, **row_key
)
117 # Pokemon also get other languages
118 if cls
== tables
.Pokemon
:
119 for foreign_name
in row
.foreign_names
:
120 name
= foreign_name
.name
.lower()
121 writer
.add_document(name
=name
,
122 language
=foreign_name
.language
.name
,
124 speller_entries
.append(name
)
126 if foreign_name
.language
.name
== 'Japanese':
128 roomaji
= romanize(foreign_name
.name
).lower()
129 writer
.add_document(name
=roomaji
,
132 speller_entries
.append(roomaji
)
137 # Construct and populate a spell-checker index. Quicker to do it all
138 # at once, as every call to add_* does a commit(), and those seem to be
140 speller
= whoosh
.spelling
.SpellChecker(index
.storage
)
141 speller
.add_words(speller_entries
)
143 return index
, speller
146 LookupResult
= namedtuple('LookupResult', ['object', 'language', 'exact'])
147 def lookup(name
, session
=None, indices
=None, exact_only
=False):
148 """Attempts to find some sort of object, given a database session and name.
150 Returns a list of named (object, language, exact) tuples. `object` is a
151 database object, `language` is the name of the language in which the name
152 was found, and `exact` is True iff this was an exact match.
154 This function currently ONLY does fuzzy matching if there are no exact
157 Formes are not returned; "Shaymin" will return only grass Shaymin.
160 - Pokémon names: "Eevee"
163 Name of the thing to look for.
166 A database session to use for retrieving objects. As with get_index,
167 if this is not provided, a connection to the default database will be
171 Tuple of index, speller as returned from `open_index()`. Defaults to
172 a call to `open_index()`.
175 If True, only exact matches are returned. If set to False (the
176 default), and the provided `name` doesn't match anything exactly,
177 spelling correction will be attempted.
184 index
, speller
= indices
186 index
, speller
= open_index()
192 # Look for exact name. A Term object does an exact match, so we don't have
193 # to worry about a query parser tripping on weird characters in the input
194 searcher
= index
.searcher()
195 query
= whoosh
.query
.Term('name', name
.lower())
196 results
= searcher
.search(query
)
198 # Look for some fuzzy matches if necessary
199 if not exact_only
and not results
:
203 for suggestion
in speller
.suggest(name
, 10):
204 query
= whoosh
.query
.Term('name', suggestion
)
205 results
.extend(searcher
.search(query
))
207 ### Convert results to db objects
210 for result
in results
:
212 seen_key
= result
['table'], result
['row_id']
215 seen
[seen_key
] = True
217 cls
= indexed_tables
[result
['table']]
218 obj
= session
.query(cls
).get(result
['row_id'])
219 objects
.append(LookupResult(obj
, result
['language'], exact
))