The WordSelector class in loglan_core\addons\word_selector.py is used to construct complex queries to select words from a database. Here's an example usage of this class:
# Import the WordSelector class
from loglan_core.addons.word_selector import WordSelectorWhen creating WordSelector object, several parameters can be used:
model: class of the words to be filtered (BaseWordby default). It is used to return words by associated class.
The specified class must inherit from
BaseWord, otherwise aValueErrorexception will be raised if thedisable_model_checkoption isFalse.
is_sqlite: boolean that indicates whether the underlying database is SQLite. IfTrue, theWordSelectorwill use SQLite-compatible SQL syntax.
You must explicitly specify
is_sqliteoption when working with SQLite in order for case-sensitive search features to work correctly.
-
case_sensitive: boolean that indicates whether the search is case-sensitive. IfTrue, theWordSelectorwill use case-sensitive search in the underlying database. -
disable_model_check: boolean that indicates whether theWordSelectorshould perform a model check. IfTrue, theWordSelectorwill not perform a model check.
The simplest way to create a WordSelector object is to use the WordSelector constructor:
ws = WordSelector()By this way, we create a new WordSelector instance that will select all words from database.
Also, we can apply filters to our query.
For example, to select words associated with a specific event, use the by_event method:
ws_event = WordSelector().by_event(event_id=1)For filtering words by a specific name, use the by_name method:
ws_by_name = WordSelector().by_name(name='proga')Similarly, we can filter words by a specific key using the by_key method:
ws_by_key = WordSelector().by_key(key='program')Or use the by_type method for filtering words by a specific type:
ws_by_type = WordSelector().by_type("Afx")All these methods return a new instance of WordSelector with the filter applied.
We can also combine all these filters together:
ws_combined = WordSelector().by_event(event_id=1).by_name(name='proga')This will select words that are associated with the specified event AND have the specified name.
Three methods are available for getting derivatives of the word by word_id:
get_derivatives_of, get_complexes_of and get_affixes_of
ws_derivatives = WordSelector().get_derivatives_of(word_id=1)
ws_derivatives = WordSelector().get_complexes_of(word_id=1)
ws_derivatives = WordSelector().get_affixes_of(word_id=1)You can combine them with filters as well.
The WordSelector object returns a classic SQLAlchemy Select Object, so we can use it as normal within a session, like this:
all_words = session.scalars(ws_combined.get_statement()).all()
first_word = session.scalar(ws_combined.get_statement())Also, we can use the internal all, scalar or fetchmany methods:
all_words = ws_combined.all(session)
first_word = ws_combined.scalar(session)
first_five_words = ws_combined.fetchmany(session, size=5)These methods return a list of Word Objects or a single Word Object that match the applied filters.