classify module

Classes and functions for classifying and extracting information from documents.

See the keyword extraction guide for a task-oriented introduction to query expansion and “more like this”.

Expander

class whoosh.classify.Expander(ixreader: Any, fieldname: str, model: type[ExpansionModel] | ExpansionModel = <class 'whoosh.classify.Bo1Model'>)[source]

Uses an ExpansionModel to expand the set of query terms based on the top N result documents.

Parameters:
  • reader – A :class: whoosh.reading.IndexReader object.

  • fieldname – The name of the field in which to search.

  • model – (classify.ExpansionModel) The model to use for expanding the query terms. If you omit this parameter, the expander uses Bo1Model by default.

add(vector: Iterable[tuple[str, float]]) None[source]

Adds forward-index information about one of the “top N” documents.

Parameters:

vector – A series of (text, weight) tuples, such as is returned by Reader.vector_as(“weight”, docnum, fieldname).

expanded_terms(number: int, normalize: bool = True) list[tuple[str, float]][source]

Returns the N most important terms in the vectors added so far.

Parameters:
  • number – The number of terms to return.

  • normalize – Whether to normalize the weights.

Returns:

A list of (“term”, weight) tuples.

Expansion models

class whoosh.classify.ExpansionModel(doc_count: int, field_length: float)[source]
class whoosh.classify.Bo1Model(doc_count: int, field_length: float)[source]
class whoosh.classify.Bo2Model(doc_count: int, field_length: float)[source]
class whoosh.classify.KLModel(doc_count: int, field_length: float)[source]