Last active
March 11, 2018 08:17
-
-
Save ronaldseoh/d9dbb2f066b1a8b9d6d823d72b2c9035 to your computer and use it in GitHub Desktop.
Make your Wordnik list into a html file (with Markdown) - I use this to view lists on my Kindle (Use Send to Kindle for conversion). Now with the ability to add Korean translations for each word (via Naver Papago API)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
''' | |
import random | |
from wordnik import AccountApi, WordListApi, WordApi, swagger | |
from papago import Translator | |
import markdown | |
apiUrl = 'http://api.wordnik.com:80/v4' | |
apiKey = 'YOUR API KEY HERE' | |
client = swagger.ApiClient(apiKey, apiUrl) | |
papago_client_id = 'YOUR PAPAGO CLIENT ID' | |
papago_client_secret = 'YOUR PAPAGO CLIENT SECRET' | |
papago_translator = Translator(papago_client_id, papago_client_secret) | |
account_api = AccountApi.AccountApi(client) | |
auth_token = account_api.authenticatePost('WORDNIK ID', 'WORDNIK PASSWORD') | |
available_wordlists = account_api.getWordListsForLoggedInUser(auth_token.token) | |
wordlist_selection_complete = False | |
selected_wordlist_permalink = '' | |
while not wordlist_selection_complete: | |
question_string = "Please select the word list you want to work with: \n" | |
for i, wordlist in enumerate(available_wordlists): | |
question_string += "[" + str(i)+ "] " + wordlist.permalink + "\n" | |
question_string += "[" + str(i+1)+ "] " + "Enter permalink directly" + "\n" | |
question_string += "Enter the list number: " | |
selection = input(question_string) | |
if int(selection) < len(available_wordlists): | |
selected_wordlist_permalink = available_wordlists[int(selection)].permalink | |
wordlist_selection_complete = True | |
elif int(selection) == len(available_wordlists): | |
selected_wordlist_permalink = input("Enter the permanlink you want to work with: ") | |
wordlist_selection_complete = True | |
markdown_source = '' | |
markdown_source += '# ' + selected_wordlist_permalink + '\n\n' | |
wordlist_api = WordListApi.WordListApi(client) | |
word_list = wordlist_api.getWordListWords(selected_wordlist_permalink, auth_token.token, limit=10000) | |
word_api = WordApi.WordApi(client) | |
for word in random.sample(word_list, len(word_list)): | |
print(word.word) | |
definitions = word_api.getDefinitions(word.word) | |
examples = word_api.getExamples(word.word) | |
related_words = word_api.getRelatedWords(word.word) | |
markdown_source += '## ' + word.word + '\n' | |
last_part_of_speech = '' | |
last_part_of_speech_count = 0 | |
total_definition_count = 0 | |
definition_string = '' | |
markdown_source += '### Definitions\n' | |
if definitions is not None: | |
for definition in definitions: | |
if (definition.partOfSpeech == last_part_of_speech) and last_part_of_speech_count == 2: | |
continue | |
else: | |
if last_part_of_speech != definition.partOfSpeech: | |
last_part_of_speech = definition.partOfSpeech | |
last_part_of_speech_count = 0 | |
total_definition_count += 1 | |
last_part_of_speech_count += 1 | |
definition_string += str(total_definition_count) + '. ' + definition.partOfSpeech + ": " | |
definition_string += definition.text + ' \n' | |
markdown_source += definition_string | |
else: | |
markdown_source += '1. No definition found.\n' | |
# Let's get Korean definitions for the word using Papago | |
korean_definition_string = '* 한국어 뜻: ' | |
papago_response = papago_translator.translate(word.word, 'en', 'ko') | |
if papago_response.SUCCESS_CODE == 0: | |
korean_definition_string += papago_response.text | |
else: | |
korean_definition_string += '(파파고 조회 실패)' | |
korean_definition_string += '\n' | |
markdown_source += korean_definition_string | |
markdown_source += '### Examples\n' | |
if examples is not None: | |
markdown_source += examples.examples[0].text + '\n' | |
else: | |
markdown_source += '1. No examples found.\n' | |
markdown_source += '### Related Words\n' | |
if related_words is not None: | |
for related_group in related_words: | |
if related_group.relationshipType == 'synonym': | |
markdown_source += str(related_group.words) + '\n' | |
else: | |
markdown_source += 'No synonyms found.\n' | |
markdown_source += '\n' | |
markdown_source = markdown.markdown(markdown_source) | |
markdown_source = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body>' + markdown_source | |
markdown_source = markdown_source + '</body></html>' | |
with open(selected_wordlist_permalink + '.html', 'w', encoding='utf-8') as output_file: | |
output_file.write(markdown_source) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment