Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
talit:tutorial_oop3 [2025-05-11 14:53] – [Auswahl eines Wortes] hof | talit:tutorial_oop3 [2025-05-26 14:11] (aktuell) – [Aufgabe F] hof | ||
---|---|---|---|
Zeile 227: | Zeile 227: | ||
</ | </ | ||
- | ### Refactoring | + | ## Refactoring |
Dein Code in `ConsoleLearner.learn()` könnte irgendwie so aussehen: | Dein Code in `ConsoleLearner.learn()` könnte irgendwie so aussehen: | ||
Zeile 244: | Zeile 244: | ||
Wir möchten einige Teile dieses Verhaltens anpassen: | Wir möchten einige Teile dieses Verhaltens anpassen: | ||
- | #### Auswahl eines Wortes | + | ### Auswahl eines Wortes |
Statt immer alle Paare durchzugehen, | Statt immer alle Paare durchzugehen, | ||
* zufällige Wahl eines Wortpaars | * zufällige Wahl eines Wortpaars | ||
Zeile 252: | Zeile 252: | ||
<code python> | <code python> | ||
- | class SelectionStrategy: | + | class LearningStrategy: |
def select(self, | def select(self, | ||
pass # has to be implemented by subclasses. | pass # has to be implemented by subclasses. | ||
</ | </ | ||
- | Hier ein Beispiel für das lineare | + | Hier ein Beispiel für das lineare |
<code python> | <code python> | ||
- | class LinearStrategy: | + | class LinearStrategy(LearningStrategy): |
""" | """ | ||
def __init__(self): | def __init__(self): | ||
Zeile 274: | Zeile 274: | ||
<code python> | <code python> | ||
- | def learn(self, unit, selection_strategy=LinearStrategy()): | + | def learn(self, unit, learning_strategy=LinearStrategy()): |
for _ in range(len(unit.pairs)): | for _ in range(len(unit.pairs)): | ||
- | pair = selection_strategy.select(unit) | + | pair = learning_strategy.select(unit) |
guess = input(f' | guess = input(f' | ||
correct = guess == pair.word2 | correct = guess == pair.word2 | ||
Zeile 293: | Zeile 293: | ||
* s. [[https:// | * s. [[https:// | ||
+ | Du hast damit das [[wp> | ||
+ | ### Aufgabe D: Wie lange lernen? | ||
+ | Ähnlich wie die Entscheidung über das nächste Wortpaar möchten wir auch die Entscheidung, | ||
+ | |||
+ | Schreibe eine Klasse `StopCriterion` | ||
+ | |||
+ | * `ScoreCriterion`: | ||
+ | * `TimeCriterion`: | ||
+ | * `CountingCriterion`: | ||
+ | * `OrCriterion`: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Wie sieht die `ConsoleLearner.learn` Methode jetzt aus? | ||
+ | ## Speichern & Lesen | ||
+ | Wir möchten `VocabularyUnits` in eine Datei speichern und von dort wieder lesen können. Es bietet sich an, eine Unit als JSON-Objekt zu speichern. JSON (JavaScript Object Notation) sind Dictionaries, | ||
+ | |||
+ | <code python> | ||
+ | {" | ||
+ | </ | ||
+ | |||
+ | Eine ganze Unit wäre dann eine Liste solcher Objekte: | ||
+ | |||
+ | <code python> | ||
+ | [ | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | </ | ||
+ | ### Aufgabe E: JSON-Serialisierung | ||
+ | |||
+ | * Füge eine Methode `to_dict(self)` zu `WordPair` hinzu, die ein Wort-Paar und seine Stats in ein Dictionary verwandelt und zurückgibt. | ||
+ | * Füge eine Methode `save_to(self, | ||
+ | |||
+ | <code python> | ||
+ | def save_to(self, | ||
+ | json_list = [pair.to_dict() for pair in self.pairs] | ||
+ | import json | ||
+ | with open(filename, | ||
+ | json.dump(json_list, | ||
+ | </ | ||
+ | |||
+ | |||
+ | ### Statische Methoden | ||
+ | Fürs Einlesen kommt die umgekehrte `json.load` Funktion zum Einsatz. Allerdings haben wir noch ein kleines Problem: Eine VocabularyUnit existiert ja noch gar nicht, wenn wir sie einlesen wollen aus der Datei. Wir benötigen also eine Funktion, die nicht an eine bestimmte Unit gebunden ist. Diese werden mit `@staticmethod` annotiert und haben keinen `self` Parameter. Statische Funktionen werden direkt über den Klassennamen aufgerufen. | ||
+ | |||
+ | <code python> | ||
+ | class WordPair: | ||
+ | ... | ||
+ | | ||
+ | @staticmethod | ||
+ | def from_dict(data): | ||
+ | """ | ||
+ | pair = WordPair(data[' | ||
+ | # TODO also read stats if available | ||
+ | |||
+ | |||
+ | class VocabularyUnit: | ||
+ | ... | ||
+ | | ||
+ | @staticmethod | ||
+ | def read_from(filename): | ||
+ | """ | ||
+ | import json | ||
+ | pairs = [] | ||
+ | with open(filename, | ||
+ | json_pairs = json.load(infile) | ||
+ | pairs = [WordPair.from_dict(p) for p in json_pairs] | ||
+ | return VocabularyUnit(pairs) | ||
+ | </ | ||
+ | ### Aufgabe F | ||
+ | Füge statische Methoden zu `VocabularyUnit` und `WordPair` hinzu, um die gespeicherten Daten wieder einlesen zu können. | ||
+ | |||
+ | Ein Beispielprogramm für unseren Code könnte nun so aussehen: | ||
+ | |||
+ | <code python> | ||
+ | from voci import * | ||
+ | |||
+ | filename = ' | ||
+ | unit = VocabularyUnit.read_from(filename) | ||
+ | |||
+ | learner = ConsoleLearner() | ||
+ | try: | ||
+ | learner.learn(unit) | ||
+ | unit.print_stats() | ||
+ | finally: | ||
+ | unit.save_to(filename) | ||
+ | </ | ||
+ | |||
+ | |||
+ | ### Aufgabe G - Webapp | ||
+ | S. auch [[talit: |