This script allows us to extract definitions of a word from the WordNik dictionary.
Definitions of the word with the parts-of-speech of the word specified can also be obtained.
Requirement:
Wordnik API key. This can be obtained by registering at Wordnik.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# !/usr/bin/python2.7 # -*- coding: utf-8 -*- """ Created on Thu Jul 24, 2014 @author: Vasanthi Vuppuluri Last Modified on: August 4, 2014 """ """ PURPOSE: -------- - To obtain definitions from online dictionaries using Wordnik API - In this case, definitions are from https://www.wordnik.com/ - Requirements: wordnik module is to be installed first, sudo pip install wordnik API_Key for can be obtained after registering at wordnik.com - Can extract definitions when POS is specified FUNCTIONS: ---------- - There are multiple functions in this script: 1. wordnik_def(word, POS, debug): When parts-of-speech of the word is specified 2. wordnik_def_no_POS(word, debug): When there is no POS tag and definitions are to be obtained """ from wordnik import * import os, nltk def wordnik_def(word, POS, debug): apiUrl = 'http://api.wordnik.com/v4' apiKey = '' # API key should go here client = swagger.ApiClient(apiKey, apiUrl) wordApi = WordApi.WordApi(client) word = word POS = POS debug = debug try: definitions = wordApi.getDefinitions(word, partOfSpeech=POS, limit=10) list_of_definitions = [] # To store definitions as elements of a list deinition = definitions[0].text ascii_text = '' ascii_text = ascii_text + ''.join(i for i in deinition if ord(i) < 128) # Removing ascii characters from definitions obtained list_of_definitions.append(ascii_text) if (debug == '--debug'): print "Definitions from WordNik dictionary: ", list_of_definitions if (len(list_of_definitions) == 0): return 0 else: return list_of_definitions except Exception, e: print "Exception raised in Wordnik!!\n", e return 0 def wordnik_def_no_POS(word, debug): apiUrl = 'http://api.wordnik.com/v4' apiKey = '' # API key should go here client = swagger.ApiClient(apiKey, apiUrl) wordApi = WordApi.WordApi(client) word = word debug = debug try: definitions = wordApi.getDefinitions(word, limit=10) list_of_definitions = [] # To store definitions as elements of a list list_of_definitions.append(definitions[0].text) if (debug == '--debug'): print "Definitions from WordNik dictionary: ",list_of_definitions if (len(list_of_definitions) == 0): return 0 else: return list_of_definitions except Exception, e: print "Exception raised in Wordnik!!\n", e return 0 |
Your style is really unique compared to other folks I have read stuff
from. Thanks for posting when you’ve got the opportunity,
Guess I will just bookmark this web site.
Thank you Brian. I appreciate your interest in my posts.