+
+def lookup(input, valid_types=[], session=None, indices=None, exact_only=False):
+ """Attempts to find some sort of object, given a database session and name.
+
+ Returns a list of named (object, name, language, exact) tuples. `object`
+ is a database object, `name` is the name under which the object was found,
+ `language` is the name of the language in which the name was found, and
+ `exact` is True iff this was an exact match.
+
+ This function currently ONLY does fuzzy matching if there are no exact
+ matches.
+
+ Formes are not returned unless requested; "Shaymin" will return only grass
+ Shaymin.
+
+ Extraneous whitespace is removed with extreme prejudice.
+
+ Recognizes:
+ - Names: "Eevee", "Surf", "Run Away", "Payapa Berry", etc.
+ - Foreign names: "Iibui", "Eivui"
+ - Fuzzy names in whatever language: "Evee", "Ibui"
+ - IDs: "133", "192", "250"
+ Also:
+ - Type restrictions. "type:psychic" will only return the type. This is
+ how to make ID lookup useful. Multiple type specs can be entered with
+ commas, as "move,item:1". If `valid_types` are provided, any type prefix
+ will be ignored.
+ - Alternate formes can be specified merely like "wash rotom".
+
+ `input`
+ Name of the thing to look for.
+
+ `valid_types`
+ A list of table objects or names, e.g., `['pokemon', 'moves']`. If
+ this is provided, only results in one of the given tables will be
+ returned.
+
+ `session`
+ A database session to use for retrieving objects. As with get_index,
+ if this is not provided, a connection to the default database will be
+ attempted.
+
+ `indices`
+ Tuple of index, speller as returned from `open_index()`. Defaults to
+ a call to `open_index()`.
+
+ `exact_only`
+ If True, only exact matches are returned. If set to False (the
+ default), and the provided `name` doesn't match anything exactly,
+ spelling correction will be attempted.
+ """
+
+ if not session:
+ session = connect()
+
+ if indices:
+ index, speller = indices
+ else:
+ index, speller = open_index()
+
+ name = unicode(input).strip().lower()
+ exact = True
+ form = None
+
+ # Remove any type prefix (pokemon:133) before constructing a query
+ if ':' in name:
+ prefix_chunk, name = name.split(':', 1)
+ name = name.strip()
+
+ if not valid_types:
+ # Only use types from the query string if none were explicitly
+ # provided
+ prefixes = prefix_chunk.split(',')
+ valid_types = [_.strip() for _ in prefixes]
+
+ # Random lookup
+ if name == 'random':
+ return random_lookup(indices=(index, speller),
+ session=session,
+ valid_types=valid_types)
+
+ # Do different things depending what the query looks like
+ # Note: Term objects do an exact match, so we don't have to worry about a
+ # query parser tripping on weird characters in the input
+ if '*' in name or '?' in name:
+ exact_only = True
+ query = whoosh.query.Wildcard(u'name', name)
+ elif rx_is_number.match(name):
+ # Don't spell-check numbers!
+ exact_only = True
+ query = whoosh.query.Term(u'row_id', name)
+ else:
+ # Not an integer
+ query = whoosh.query.Term(u'name', name) \
+ & whoosh.query.Term(u'forme_name', u'XXX')
+
+ # If there's a space in the input, this might be a form
+ if ' ' in name:
+ form, formless_name = name.split(' ', 1)
+ form_query = whoosh.query.Term(u'name', formless_name) \
+ & whoosh.query.Term(u'forme_name', form)
+ query = query | form_query
+
+ ### Filter by type of object
+ type_terms = []
+ for valid_type in valid_types:
+ table_name = _parse_table_name(valid_type)
+ type_terms.append(whoosh.query.Term(u'table', table_name))
+
+ if type_terms:
+ query = query & whoosh.query.Or(type_terms)
+
+
+ ### Actual searching