def run_lexicon(self, code: str) -> None: """Check all user lexicons.""" if code.endswith(" ra"): language_code = code[1:3] language = construct_language(language_code) lexicon = self.user_data.get_lexicon(language) lexicon.check( self.interface, self.user_data.get_frequency_list_for_lexicon(language), None, Dictionaries(self.get_dictionaries(language)), "most frequent", False, False, None, learning=self.user_data.get_course(f"ru_{language_code}"), ) return priority_path: Path = self.path / "priority" priority_path.mkdir(parents=True, exist_ok=True) if code: languages = [construct_language(code[1:])] else: languages = sorted( self.user_data.get_lexicon_languages(), key=lambda x: -self.user_data.get_lexicon(x). get_last_rate_number(), ) for language in languages: lexicon: Lexicon = self.user_data.get_lexicon(language) now: datetime = datetime.now() need: int = 5 - lexicon.count_unknowns( "log", now - timedelta(days=7), now) need = max(need, 100 - lexicon.count_unknowns("log")) if need <= 0: continue self.interface.header(f"Lexicon for {language.get_name()}") lexicon.check( self.interface, self.user_data.get_frequency_list_for_lexicon(language), None, Dictionaries(self.get_dictionaries(language)), "frequency", False, False, need, ) break
def from_directory(cls, path: Path, user_id: str): """ :param path: path to the user data directory :param user_id: unique string user identifier """ with (path / user_id / "exclude_sentences.json").open() as input_file: exclude_sentences = json.load(input_file) with ( path / user_id / "exclude_translations.json" ).open() as input_file: exclude_translations = json.load(input_file) with (path / user_id / "config.json").open() as config_file: config: dict[str, Any] = json.load(config_file) return cls( user_id, config["name"], path, set(construct_language(x) for x in config["native_languages"]), set(config["learn"].keys()), config["lexicon"], config["priority"], config["learn"], exclude_sentences, exclude_translations, )
def __init__( self, cache_directory_name: Path, interface: Interface, user_data: UserData, sentence_db: SentenceDatabase, frequency_db: FrequencyDatabase, learning: Learning, lexicon: Lexicon, get_dictionaries=None, ) -> None: self.interface: Interface = interface self.user_data: UserData = user_data self.known_language: Language = learning.language self.learning_language: Optional[Language] try: self.learning_language = construct_language(learning.subject) except KeyError: self.learning_language = None self.max_for_day: int = learning.ratio self.learning: Learning = learning self.dictionaries: Dictionaries = Dictionaries( get_dictionaries(self.learning_language) ) self.lexicon: Lexicon = lexicon self.sentences: Sentences = Sentences( cache_directory_name, sentence_db, self.known_language, self.learning_language, ) self.words: list[tuple[int, str]] = [] log("getting words") for frequency_list_id in learning.frequency_list_ids: frequency_list_id: str for index, word, _ in frequency_db.get_words(frequency_list_id): index: int word: str if ( not self.learning.check_lexicon or not self.lexicon or not self.lexicon.has(word) or self.lexicon.get(word) == LexiconResponse.DONT ): self.words.append((index, word)) self.skip = set() self.stop_after_answer: bool = False
def __init__(self, file_path: Path, config: dict[str, any], course_id: str) -> None: self.file_path: Path = file_path self.records: list[LearningRecord] = [] self.knowledges: dict[str, Knowledge] = {} self.config: dict[str, str] = config self.course_id: str = course_id self.is_learning: bool = True # Create learning file if it doesn't exist. if not self.file_path.is_file(): self.write() with self.file_path.open() as log_file: content = json.load(log_file) records = content["log"] self.frequency_list_ids: list[str] = config["frequency_lists"] # Config defaults. self.ratio: int = 10 self.language: Optional[Language] = None self.subject: Optional[str] = None self.check_lexicon = True self.ask_lexicon = False self.name: str = "Unknown" if "ratio" in self.config: self.ratio = int(self.config["ratio"]) if "language" in self.config: self.language = construct_language(self.config["language"]) if "subject" in self.config: self.subject = self.config["subject"] if "check_lexicon" in self.config: self.check_lexicon = self.config["check_lexicon"] if "ask_lexicon" in self.config: self.ask_lexicon = self.config["ask_lexicon"] if "name" in self.config: self.name = self.config["name"] if "is_learning" in self.config: self.is_learning = self.config["is_learning"] for record_structure in records: record: LearningRecord = LearningRecord.from_structure( record_structure, self.course_id) self.records.append(record) self._update_knowledge(record)
def __init__( self, learning: Learning, user_data: UserData, cache_directory: Path, sentence_db, ): self.user_data: UserData = user_data self.learning: Learning = learning self.known_language: Language = learning.language self.learning_language: Optional[Language] try: self.learning_language = construct_language(learning.subject) except LanguageNotFound: self.learning_language = None self.interface: ui.Interface = ui.TelegramInterface() self.dictionaries: list[Dictionary] = [ EnglishWiktionary(cache_directory, self.learning_language) ] self.sentences: Sentences = Sentences( cache_directory, sentence_db, self.known_language, self.learning_language, ) self.skip: set[str] = set() # Current word status. self.word: Optional[str] = None self.interval = None self.index: int = 0 self.alternative_forms: set[str] = set() self.current_sentences: list[Translation] = [] self.items: list[DictionaryItem] = []
def test_process_definition() -> None: dictionary: EnglishWiktionary = EnglishWiktionary(Path("cache"), construct_language("eo")) definition: Definition = dictionary.process_definition( "A single act of teasing") assert definition.values[0].value == "A single act of teasing"
def test_process_definition2() -> None: dictionary: EnglishWiktionary = EnglishWiktionary(Path("cache"), construct_language("eo")) link: Link = dictionary.process_definition("present participle of tease") assert link == Link("present participle", "tease")
from pathlib import Path from emmio.dictionary import Definition, DefinitionValue, Link from emmio.external.en_wiktionary import EnglishWiktionary from emmio.language import construct_language DICTIONARY: EnglishWiktionary = EnglishWiktionary(Path("cache"), construct_language("eo")) def check_form(text: str, value: tuple) -> None: """Check whether the form parsing is valid.""" definition: Definition = Definition( [DefinitionValue(x[0], "" if len(x) == 1 else x[1]) for x in value[1]], value[0], ) assert DICTIONARY.process_definition(text) == definition def check_link(text: str, link_type: str, link_value: str) -> None: """Check whether the link parsing is valid.""" assert DICTIONARY.process_definition(text) == Link(link_type, link_value) def test_link_with_dot() -> None: check_link("past of nehmen, to take.", "past", "nehmen") def test_link_with_dot_2() -> None: check_link( "Masculine singular past participle of hacer.",
def fill_data(self, language, fl) -> None: fr = self.user_data.get_frequency_list(fl) words = {} learn: Learning = self.user_data.get_course(f"ru_{language}") for record in learn.records: if record.question_id not in words: words[record.question_id] = { "word": record.question_id, "language": language, "addTime": record.time, "nextQuestionTime": record.time + record.interval, "vector": record.answer.value, "index": fr.get_index(record.question_id), } elif record.question_id in words: words[record.question_id]["nextQuestionTime"] = ( record.time + record.interval) words[record.question_id]["vector"] += record.answer.value lexicon: Lexicon = self.user_data.get_lexicon( construct_language(language)) for word in lexicon.words: if word not in words: words[word] = { "word": word, "language": language, "addTime": datetime.now(), "nextQuestionTime": datetime.now(), "vector": "N" if lexicon.words[word].knowing == LexiconResponse.DO_NOT_KNOW else "Y", "index": fr.get_index(word), } min_add_time = min(words[x]["addTime"] for x in words) max_add_time = max(words[x]["addTime"] for x in words) min_next_question_time = min(words[x]["nextQuestionTime"] for x in words) max_next_question_time = max(words[x]["nextQuestionTime"] for x in words) min_time = min(min_add_time, min_next_question_time) max_time = max(max_add_time, max_next_question_time) for word in words: words[word]["addTime"] = (words[word]["addTime"] - min_add_time).total_seconds() words[word]["nextQuestionTime"] = ( words[word]["nextQuestionTime"] - min_time).total_seconds() w = [] for word in words: w.append(words[word]) w = list(sorted(w, key=lambda x: x["index"])) with (Path("web") / f"{language}.js").open("w") as output_file: output_file.write(f"{language} = ") json.dump(w, output_file) output_file.write(";")
def learn(self) -> None: sorted_ids: list[str] = sorted( self.user_data.course_ids, key=lambda x: -self.user_data.get_course(x).to_repeat(), ) for course_id in sorted_ids: learning: Learning = self.user_data.get_course(course_id) if not learning.is_learning: continue lexicon: Optional[Lexicon] try: lexicon = self.user_data.get_lexicon( construct_language(learning.subject)) except LanguageNotFound: lexicon = None for frequency_list_id in learning.frequency_list_ids: frequency_list: Optional[ FrequencyList] = self.user_data.get_frequency_list( frequency_list_id) if frequency_list.update and self.frequency_db.has_table( frequency_list_id): self.frequency_db.drop_table(frequency_list_id) if not self.frequency_db.has_table(frequency_list_id): self.frequency_db.add_table(frequency_list_id, frequency_list) if learning.to_repeat() > 0: self.interface.header(f"Repeat learned for {learning.name}") teacher: Teacher = Teacher( Path("cache"), self.interface, self.user_data, self.sentence_db, self.frequency_db, learning, lexicon, get_dictionaries=self.get_dictionaries, ) if not teacher.repeat(): return sorted_ids = sorted( self.user_data.course_ids, key=lambda x: (self.user_data.get_course(x).learning() / self. user_data.get_course(x).ratio), ) for course_id in sorted_ids: learning = self.user_data.get_course(course_id) lexicon: Lexicon = self.user_data.get_lexicon( construct_language(learning.subject)) if learning.ratio > learning.new_today(): self.interface.header( f"Learn new and repeat for {learning.name}") learner = Teacher( Path("cache"), self.interface, self.user_data, self.sentence_db, self.frequency_db, learning, lexicon, get_dictionaries=self.get_dictionaries, ) proceed = learner.start() if not proceed: return print() now: datetime = datetime.now() time_to_repetition: timedelta = ( min(x.get_nearest() for x in self.user_data.courses.values()) - now) time_to_new: timedelta = util.day_end(now) - now if time_to_repetition < time_to_new: print(f" Repetition in {time_to_repetition}.") else: print(f" New question in {time_to_new}.") print()