In this assignment we will work with the Princeton WordNet.
Read NLTK book chapter 2.5 about Princeton WordNet, and test the examples.
Note 1: the focus of this assignment is on the synonymy relation with its corresponding concept synset (Senses and Synonyms in chapter 2.5). But have a look at the other relations.
Note 2: WordNet is an extremely rich lexical-semantic resource that we could spend a whole course on. Here we use it to make things a bit more interesting.
Create a file assign1.py containing the code below. (replace NAME with your full names). Define all procedures and functions in this file.
"""Assignment 1: WordNet (deadline: 2010-09-24) Name 1: NAME Name 2: NAME """ from nltk.corpus import wordnet as wn import random
Define a procedure print_synsets(word) that given word, lookups up the synsets of word and prints, for every synset, its lemma_names, definition, and examples (if there is no examples, print no examples). If word is missing from WordNet, print no synsets.
Hint: before you start, think through what needs to be done, and define help functions where appropriate.
Hint 2: use string formatting
The output of print_synsets is exemplified below.
>>> import assign1
>>> assign1.print_synsets('house')
synset 1: {house}
def: "a dwelling that serves as living quarters for one or more families"
example: "he has a house on Cape Cod"
example: "she felt she had to get out of the house"
synset 2: {firm, house, business_firm}
def: "the members of a business organization that owns or operates one or more establishments"
example: "he worked for a brokerage house"
synset 3: {house}
def: "the members of a religious community living together"
no examples
synset 4: {house}
def: "the audience gathered together in a theatre or cinema"
example: "the house applauded"
example: "he counted the house"
synset 5: {house}
def: "an official assembly having legislative powers"
example: "a bicameral legislature has two houses"
synset 6: {house}
def: "aristocratic family line"
example: "the House of York"
synset 7: {house}
def: "play in which children take the roles of father or mother or children and pretend to interact like adults"
example: "the children were playing house"
synset 8: {sign_of_the_zodiac, star_sign, sign, mansion, house, planetary_house}
def: "(astrology) one of 12 equal areas into which the zodiac is divided"
no examples
synset 9: {house}
def: "the management of a gambling house or casino"
example: "the house gets a percentage of every bet"
synset 10: {family, household, house, home, menage}
def: "a social unit living together"
example: "he moved his family to Virginia"
example: "It was a good Christian household"
example: "I waited until the whole house was asleep"
example: "the teacher asked how many people made up his home"
synset 11: {theater, theatre, house}
def: "a building where theatrical performances or motion-picture shows can be presented"
example: "the house was full"
synset 12: {house}
def: "a building in which something is sheltered or located"
example: "they had a large carriage house"
synset 13: {house}
def: "contain or cover"
example: "This box houses the gears"
synset 14: {house, put_up, domiciliate}
def: "provide housing for"
example: "The immigrants were housed in a new development outside the town"
>>> assign1.print_synsets('qrsx')
no synsets
Define a function synonyms(word) that returns a set of lemma_names of the synsets of word.
The output of synonyms is exemplified below.
>>> assign1.synonyms('house')
set(['firm', 'put_up', 'theater', 'family', 'planetary_house',
'menage', 'house', 'household', 'theatre', 'star_sign',
'sign_of_the_zodiac', 'sign', 'domiciliate', 'mansion',
'home', 'business_firm'])
>>> assign1.synonyms('computer')
set(['data_processor', 'computing_machine', 'information_processing_system',
'calculator', 'computing_device', 'estimator', 'electronic_computer',
'computer', 'figurer', 'reckoner'])
>>> assign1.synonyms('qrsx')
set([])
Define a function random_pick(lst) that randomly selects an element from a list.
Hint: use random.randint in module random. Try out random.randint(1,10) (execute it a couple of times).
The output of random_pick is exemplified below.
>>> assign1.random_pick(['a','b','c','d','e']) 'e' >>> assign1.random_pick(['a','b','c','d','e']) 'b' >>> assign1.random_pick(['a','b','c','d','e']) 'c'
Define a function random_synonym(word) that randomly generate one of the synonyms of word, except word itself, unless it is the only word.
If word is not in WordNet, then the function returns *word* (a star in the beginning and end of word).
Hint: use synonyms and random_pick.
The output of random_synonym is exemplified below.
>>> assign1.random_synonym('house')
'household'
>>> assign1.random_synonym('house')
'menage'
>>> assign1.random_synonym('computer')
'computing_machine'
>>> assign1.random_synonym('xqrs')
'*xqrs*'
Define a function synonymify(sentence) that apply random_synonym to every word in sentence.
The output of synonymify is exemplified below.
>>> assign1.synonymify('bill sits in front a computer in his house')
'bank_note ride Indiana front_man adenine reckoner inward *his* sign'
>>> assign1.synonymify('bill sits in front a computer in his house')
'account pose Hoosier_State look ampere figurer inch *his* star_sign'
>>> assign1.synonymify('bill sits in front a computer in his house')
'flier sit_down indium breast angstrom_unit computing_device inch *his* domiciliate'