+class LanguageWeighting(whoosh.scoring.Weighting):
+ """A scoring class that forces otherwise-equal English results to come
+ before foreign results.
+ """
+
+ def score(self, searcher, fieldnum, text, docnum, weight, QTF=1):
+ doc = searcher.stored_fields(docnum)
+ if doc['language'] == None:
+ # English (well, "default"); leave it at 1
+ return weight
+ elif doc['language'] == u'Roomaji':
+ # Give Roomaji a bit of a boost, as it's most likely to be searched
+ return weight * 0.95
+ else:
+ # Everything else can drop down the totem pole
+ return weight * 0.9
+
+rx_is_number = re.compile('^\d+$')
+
+LookupResult = namedtuple('LookupResult',
+ ['object', 'name', 'language', 'iso3166', 'exact'])
+
+def _parse_table_name(name):
+ """Takes a singular table name, table name, or table object and returns the
+ table name.
+
+ Returns None for a bogus name.
+ """
+ if hasattr(name, '__tablename__'):
+ return getattr(name, '__tablename__')
+ elif name in indexed_tables:
+ return name
+ elif name + 's' in indexed_tables:
+ return name + 's'
+ else:
+ # Bogus. Be nice and return dummy
+ return None
+
+def _whoosh_records_to_results(records, session, exact=True):
+ """Converts a list of whoosh's indexed records to LookupResult tuples
+ containing database objects.
+ """
+ # XXX this 'exact' thing is getting kinda leaky. would like a better way
+ # to handle it, since only lookup() cares about fuzzy results
+ seen = {}
+ results = []
+ for record in records:
+ # Skip dupes
+ seen_key = record['table'], record['row_id']
+ if seen_key in seen:
+ continue
+ seen[seen_key] = True
+
+ cls = indexed_tables[record['table']]
+ obj = session.query(cls).get(record['row_id'])
+
+ results.append(LookupResult(object=obj,
+ name=record['display_name'],
+ language=record['language'],
+ iso3166=record['iso3166'],
+ exact=exact))
+
+ return results
+
+
+def lookup(input, valid_types=[], session=None, indices=None, exact_only=False):