The program takes a list of English words written in the first column of an excel file named "List" using pandas, the headline of the column is "words" and the words are located in "Sheet1".
The words are then stored as a list of strings.
PyDictionary and Googletrans are utilized by creating dictionary and translator where the translator is called by translations in order to translate the words from the list into the destination language "Danish".
A simple for loop is then created where every translation in the list of translated words is printed with its origin -> its destination and its definition.
This is presented in the code below:
from googletrans import Translator
import pandas as pd
from PyDictionary import PyDictionary
# Load excel file and parse the list of words as strings
file_location = "/Users/.../List.xlsx"
xl_workbook = pd.ExcelFile(file_location)
df = xl_workbook.parse("Sheet1")
aList = df['words'].tolist()
[str(i) for i in aList]
# Use PyDictionary to load definitions of words
dictionary = PyDictionary()
# Translate the list of strings into target language and give definitions
translator = Translator()
translations = translator.translate(aList, dest='da')
# Simple for-loop printing the words
for translation in translations:
print(
translation.origin, ' -> ', translation.text,
dictionary.meaning(translation.origin)
)
This program actually runs and produced the result I wished. However, the problem occurs in the next step as explained here:
I wish to input the words into my favorite flash-card program Anki. Anki is written in Python and has an un-official distribution named Genanki. However, this is when I encounter my issues.
I now add the following 4 things to my code above:
- I define my_model as proposed by genanki and create a simple model of a flashcard. This includes a random hardcoded number (which Anki requires), a name of the model, some fields and a template for the card type.
- I define my_deck as the specific deck of flashcards with a hardcoded random number and a name.
- I change my for loop to now run the translations and definitions directly into a variable named aNote which consists of the genanki.note operator iterating over the translations each time adding the note to my_deck.
- I write my anki-file that I can open with Anki.
This can be seen in the code below:
from googletrans import Translator
import pandas as pd
from PyDictionary import PyDictionary
import genanki
# Load excel file and parse the list of words as strings
file_location = "/Users/.../List.xlsx"
xl_workbook = pd.ExcelFile(file_location)
df = xl_workbook.parse("Sheet1")
aList = df['words'].tolist()
[str(i) for i in aList]
# Use PyDictionary to load definitions of words
dictionary = PyDictionary()
# Use genanki to define a flashcard model
my_model = genanki.Model(
2042686211,
'Simple Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
# Specify the deck with genanki
my_deck = genanki.Deck(
1724897887,
'TestV3v1')
# Translate the list of strings with definition and add as note to anki
translator = Translator()
translations = translator.translate(aList, dest='da')
for translation in translations:
aNote = genanki.Note(
model=my_model, fields=[translation.origin, translation.text]
)
my_deck.add_note(aNote)
# Output anki file in desired folder
genanki.Package(my_deck).write_to_file(
'/Users/.../TestV3v1.apkg')
This code also executes just fine and generates a file which can be opened in Anki, the card then shows the original word on the front of the flashcards, with the translation on the back.
My problem
In order to finish my project, I wish to add a definition to the backside of each card accompanying the translation. I originally thought that I would just have to correct the my_model = genanki.model(...) variable, by adding another field so that I could just add dictionary.meaning(translation.origin) to the note generator in the for-loop.
However, when trying to add just the definitions to make sure they run smoothly, I run into problems. Consider the code below:
for translation in translations:
aNote = genanki.Note(
model=my_model, fields=[translation.origin,
dictionary.meaning(translation.origin)
]
)
my_deck.add_note(aNote)
I expected the cards to be printed as usual, with the original word on the frontside and the definition on the backside, but running the full code with this for-loop instead gives me the following error(s):
Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: The Following Error occured: list index out of range
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: A Term must be only a single word
Error: The Following Error occured: list index out of range
Error: A Term must be only a single word
Error: The Following Error occured: list index out of range
Traceback (most recent call last):
File "/Users/Lehmann/Desktop/XYZ/Programming/Translator/TranslatorProgramv3.py", line 51, in <module>
'/Users/Lehmann/Desktop/XYZ/Programming/Translator/TestV3v1.apkg')
File "/anaconda3/lib/python3.6/site-packages/genanki/__init__.py", line 313, in write_to_file
self.write_to_db(cursor, now_ts)
File "/anaconda3/lib/python3.6/site-packages/genanki/__init__.py", line 331, in write_to_db
deck.write_to_db(cursor, now_ts)
File "/anaconda3/lib/python3.6/site-packages/genanki/__init__.py", line 267, in write_to_db
note.write_to_db(cursor, now_ts, self.deck_id)
File "/anaconda3/lib/python3.6/site-packages/genanki/__init__.py", line 228, in write_to_db
self._format_fields(), # flds
File "/anaconda3/lib/python3.6/site-packages/genanki/__init__.py", line 240, in _format_fields
return '\x1f'.join(self.fields)
TypeError: sequence item 1: expected str instance, dict found
I suspect it is the part "Expected str instance, dict found" that causes me trouble, however, this is my very first python project and I am not a programmer, so I hope someone out there can help me understand the issue.
BR
Mikkel