import csv
import genanki
import random
from googletrans import Translator
translator = Translator()

uid = random.randrange(1 << 30, 1 << 31)

deck = genanki.Deck(
  uid,
  'indonesian Common Words')

model = genanki.Model(
  uid,
  'Simple Model',
  fields=[
    {'name': 'Question'},
    {'name': 'Answer'},
  ],
  templates=[
    {
      'name': 'Card 1',
      'qfmt': '{{Question}}',
      'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
    },
  ])

file = open('words.txt', 'r')

words = []

for line in file:
	row = line.split()
	rank = int(row.pop())
	word = str(row[0])

	if rank >= 745 and len(word) > 2:
		deck.add_note(genanki.Note(model=model, fields=[word, '']))
		words.append([word, rank])

# Save anki deck
genanki.Package(deck).write_to_file('result.apkg')

# Save output in csv
with open('result.csv', 'w', newline='\n') as result:
	writer = csv.writer(result, quotechar='"', delimiter=',',
                     quoting=csv.QUOTE_ALL, skipinitialspace=True)

	writer.writerow(['indonesian', 'dutch', 'rank'])

	for word in words:
		writer.writerow([str(word[0].capitalize()), '', str(word.pop())])