nltk.pdf
Unformatted Attachment Preview
import nltk
from nltk.corpus import cmudict
from nltk.corpus import wordnet
from random import randint
allWordsFromFile=[]
randomWordsList=[]
proDict = cmudict.dict()
#Abbreviation to full form, for the POS determined by “synsets”
posDict = {‘n’: ‘NOUN’,’v’: ‘VERB’, ‘a’: ‘ADJECTIVE’, ‘s’: ‘ADJECTIVE’, ‘r’: ‘ADVERB’}
#The theme for which words are generated
inputTheme=”autumn”
#This method counts the number of Syllables in a word
#It uses cmudict.dict(), which is a pronounciation dictionary
def countSyllables(word):
global posDict
return max([len([y for y in x if y[-1].isdigit()]) for x in proDict[word]])
#It creates a record for given word and theme
#Just like what is mentioned in the question
#e.g “Halloween, 3, noun, autumn”
def getRecordFor(word,theme):
global posDict
themeNet = wordnet.synsets(theme)[0]
finalToken=themeNet
maxMatch = -1
#Using synsets to compare all the word POS forms,
#And to choose the one with best match with the theme
for candidate in wordnet.synsets(word.lower()):
matchP = candidate.wup_similarity(themeNet)
#Sometimes wup_similarity() can return “None”
#Hence needed to be checked for type
if isinstance(matchP,float):
if matchP > maxMatch:
finalToken = candidate
maxMatch = matchP
#Extracting POS abbreviation
temp = finalToken.name().split(‘.’)
#Getting the full name of that abbreviation
pos = posDict[temp[1]]
#Creation of record
result = []
result.append(word)
result.append(countSyllables(word))
result.append(pos)
result.append(theme)
return result
#=========================================================
#Begins here
#Extract all words from file, remove any newline or feed characters
with open(“linuxwords.txt”) as file:
for word in file:
word=word.strip(‘nf’)
allWordsFromFile.append(word)
#From the list of words, extract random 150 words
#Just for testing purposes, reducing the value to 10
while len(randomWordsList) < 10:
index = randint(0,len(allWordsFromFile))
word = allWordsFromFile[index]
#These words must be in the dictionary, so as to be helpful
if word in proDict:
randomWordsList.append(word)
del allWordsFromFile[index]
#All records go here
wordListFormatted=[]
#Generating all records
for word in randomWordsList:
wordListFormatted.append(getRecordFor(word,inputTheme))
print(wordListFormatted)
...
Purchase answer to see full
attachment

How it works
- Paste your instructions in the instructions box. You can also attach an instructions file
- Select the writer category, deadline, education level and review the instructions
- Make a payment for the order to be assignment to a writer
- Download the paper after the writer uploads it
Will the writer plagiarize my essay?
You will get a plagiarism-free paper and you can get an originality report upon request.
Is this service safe?
All the personal information is confidential and we have 100% safe payment methods. We also guarantee good grades