AI

A small Python AI that plays to Baltoslav, Guess the Language.

Classes

class baltoslav.ai.Languages(languages=None)

Implements a database as a dictionnary {language name: [words]}. The basic manipulations on dict are still available:

  • len function

  • in operator

  • keys function

  • values function

  • items function

Attributes

languagesdict

The database, under the form of a dict: {name: [words]}.

Methods

load(filename: str = 'bs_languages')

Loads languages from a file.

Parameters

filenamestr, optionnal

The name of the file to load.

save(filename: str = 'bs_languages')

Save languages into a file.

Parameters

filenamestr, optionnal

The name of the file to save to.

add_languages(languages: dict)

Adds new languages and new words to the database.

Parameters

languagesdict

The languages to add, must be a dict like: {name: [words]}.

identification(unknown_words: tuple)

Tries to identify the unknown words given by comparing these words with those in the database.

Parameters

unknown_wordstuple

The words to identify. It must be an iterable.

Returns

matcheslist

The list of the languages that matched: [(name, confidence)]. The languages are sorted by decreasing confidence.

feed_database()

Artificially expand the database.

class baltoslav.ai.BaltoslavAI(languages: Languages, game_lang: str = 'en')

This class implements a small AI that plays Baltoslav.

Attributes

limitint

The maximum number of guess before exiting the game.

languagesLanguages

The database, under the form of a Languages’s instance.

game_langstr

The language code.

Methods

__init__(languages: Languages, game_lang: str = 'en')

Constructor method.

Parameters

languagesbaltoslav.Languages

The database, under the form of a Languages’s instance.

game_langstr, optionnal

The language code.

run_training(iterations: int = 1)

Runs the AI in training mode: create a web driver and launches training.

Parameters

iterationsint, optionnal

The number of training sessions.

run_playing()

Runs the AI in playing mode.

Exemples

Assuming ia.py was imported as follow

>>> import baltoslav.ia as bs

We can create an instance

>>> database = Languages()       # creates a new database
>>> my_bs = bs.BaltoslavAI(database)
>>> my_bs.run_training(10)       # launchs a training session
>>> database.save('bs_exemple')  # saves the changes