Skip to content

Instantly share code, notes, and snippets.

@B1Z0N
Created January 19, 2025 10:27
Show Gist options
  • Save B1Z0N/3e9b0f22992c820bbd82d7a02e3749cd to your computer and use it in GitHub Desktop.
Save B1Z0N/3e9b0f22992c820bbd82d7a02e3749cd to your computer and use it in GitHub Desktop.
from german_nouns.lookup import Nouns
# Input list of words with their articles and translations
word_list = '''
das Dienen - служба
die Persón
die Ausstelung - виставка
der Obst
die Buch - книга
das Auto - авто
'''.strip().split('\n')
# Initialize the German nouns lookup
nouns = Nouns()
# Mapping of genus to definite and indefinite articles
definite_articles = {'m': 'der', 'f': 'die', 'n': 'das'}
def check_german_article(article, word):
"""
Check if the given article is correct for the provided German word.
Parameters:
article (str): The German article (e.g., 'der', 'die', 'das', 'ein', 'eine').
word (str): The German noun to check the article for.
Returns:
bool: True if the article is correct, False otherwise.
str: Explanation for the result.
"""
try:
word_data = nouns[word]
if not word_data:
raise KeyError()
for entry in word_data:
if article == definite_articles[entry['genus']]:
return True, f"The article '{article}' is correct for the word '{word}'."
if len(word_data) == 1:
should_be = definite_articles[word_data[0]['genus']]
else:
should_be = 'one of ' + ', '.join(definite_articles[v['genus']] for v in word_data)
return False, f"The article '{article}' is incorrect for the word '{word}'. It should be {should_be}."
except KeyError:
return False, f"The word '{word}' was not found in the nouns library."
def process_and_check_words(word_list):
"""
Parse the word list, extract the article and German word,
and check if the article is correct.
Parameters:
word_list (list): List of strings in the format "article word - translation".
Returns:
None
"""
for entry in word_list:
try:
if '-' in entry:
# Split the entry into the German part and the translation
german_part, _ = entry.split('-', 1)
else:
german_part = entry
# Split the German part into the article and the word
article, word = german_part.strip().split(' ', 1)
article = article.strip()
word = word.strip()
# Check if the article is correct
result, explanation = check_german_article(article, word)
if not result:
print(explanation)
except Exception as e:
print(f'Error: "{e}" for word "{entry}"')
# Run the function
process_and_check_words(word_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment