Last active
December 22, 2017 03:37
-
-
Save ronaldseoh/bffe409a658e8094338aac7862ef173a to your computer and use it in GitHub Desktop.
save wordnik word list into xlsx file
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/>. | |
''' | |
from wordnik import AccountApi, WordListApi, WordApi, swagger | |
from openpyxl import Workbook | |
apiUrl = 'http://api.wordnik.com:80/v4' | |
apiKey = 'YOUR API KEY HERE' | |
client = swagger.ApiClient(apiKey, apiUrl) | |
account_api = AccountApi.AccountApi(client) | |
auth_token = account_api.authenticatePost('YOUR ID HERE', 'YOUR PASSWORD HERE') | |
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 += "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 | |
wordlist_api = WordListApi.WordListApi(client) | |
word_list = wordlist_api.getWordListWords(selected_wordlist_permalink, auth_token.token) | |
wb = Workbook() | |
ws = wb.active | |
word_api = WordApi.WordApi(client) | |
for word in word_list: | |
print(word.word) | |
definitions = word_api.getDefinitions(word.word) | |
examples = word_api.getExamples(word.word) | |
related_words = word_api.getRelatedWords(word.word) | |
ws_row_list = [] | |
ws_row_list.append(word.word) | |
last_part_of_speech = '' | |
last_part_of_speech_count = 0 | |
total_definition_count = 0 | |
definition_string = '' | |
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 + ' ' | |
ws_row_list.append(definition_string) | |
else: | |
ws_row_list.append('No definition found.') | |
if examples is not None: | |
ws_row_list.append(examples.examples[0].text) | |
else: | |
ws_row_list.append('No examples found.') | |
if related_words is not None: | |
for related_group in related_words: | |
if related_group.relationshipType == 'synonym': | |
ws_row_list.append(str(related_group.words)) | |
else: | |
ws_row_list.append('No synonyms found.') | |
ws.append(ws_row_list) | |
wb.save("output.xlsx") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment