Commit 8a3e49a7 by Steven Bird

renamed wordnet.py to synset.py, cleaned up demo code

svn/trunk@4665
parent f8a2c52b
......@@ -57,5 +57,5 @@ from util import *
from cache import *
from lexname import *
from similarity import *
from wordnet import *
from synset import *
from browse import *
......@@ -5,7 +5,8 @@
# URL: <http://nltk.sf.net>
# For license information, see LICENSE.TXT
from nltk.wordnet import *
from util import *
from dictionary import *
from textwrap import TextWrapper
from string import join
from random import randint
......@@ -77,23 +78,58 @@ def browse(word="", index=0):
input = raw_input(synsets[index][0] + "_" + `index` + "/" + `len(synsets)` + "> ")
else:
input = raw_input("> ") # safety net
if input[0] == '"' and input[-1] == '"':
D, synsets = _new_word(input[1:-1])
# word lookup
if (input[0] == '"' and input[-1] == '"') or (input[0] == "'" and input[-1] == "'"):
word = input[1:-1]
D, synsets = _new_word(word)
index = 0
# sense selection
elif input[0] in "0123456789":
if int(input) < len(synsets):
index = int(input)
print_gloss(synsets, index)
else:
print "There are %d synsets" % len(synsets)
# more info
elif input[0] is "a":
print_all(synsets)
elif input[0] is "g":
print_gloss(synsets, index)
elif input[0] is "v":
print_all_glosses(synsets)
elif input[0] is "h":
help()
elif input[0] is "s":
print "Synonyms:", join(word for word in synsets[index])
# choose part-of-speech
elif input[0] in "N": # nouns
if word in N:
D = N
synsets = D[word]
else:
print "No noun sense found"
elif input[0] is "V": # verbs
if word in V:
D = V
synsets = D[word]
else:
print "No verb sense found"
elif input[0] is "J": # adjectives
if word in ADJ:
D = ADJ
synsets = D[word]
else:
print "No adjective sense found"
elif input[0] is "R": # adverbs
if word in ADV:
D = ADV
synsets = D[word]
else:
print "No adverb sense found"
# navigation
elif input[0] is "r":
synsets = _random_synset(D)
elif input[0] is "u":
......@@ -114,27 +150,14 @@ def browse(word="", index=0):
index = 0
except IndexError:
print "Cannot go down"
elif input[0] is "s":
print "Synonyms:", join(word for word in synsets[index])
elif input[0] in "N": # nouns
if word in N:
D = N
synsets = D[word]
elif input[0] is "V": # verbs
if word in V:
D = V
synsets = D[word]
elif input[0] is "J": # adjectives
if word in ADJ:
D = ADJ
synsets = D[word]
elif input[0] is "R": # adverbs
if word in ADV:
D = ADV
synsets = D[word]
# miscellany
elif input[0] is "h":
help()
elif input[0] is "q":
print "Goodbye"
break
else:
print "Unrecognised command, type 'h' for help"
......
......@@ -63,7 +63,7 @@ class Dictionary(object):
pos = self.pos
def loader(key=key, line=line, indexFile=self.indexFile):
from wordnet import Word
from synset import Word
line = line or indexFile.get(key)
return line and Word(line)
......@@ -82,7 +82,7 @@ class Dictionary(object):
"""
def loader(pos=self.pos, offset=offset, dataFile=self.dataFile):
from wordnet import Synset
from synset import Synset
dataFile.seek(offset)
line = dataFile.readline()
return Synset(pos, offset, line)
......
......@@ -641,14 +641,14 @@ def _equalsIgnoreCase(a, b):
def demo():
from nltk.wordnet import N, V, ADJ, ADV, HYPERNYM
from nltk import wordnet
from pprint import pprint
dog = N['dog']
cat = N['cat']
dog = wordnet.N['dog']
cat = wordnet.N['cat']
print "N['dog']"
print 'dog' in N
print "wordnet.N['dog']"
print 'dog' in wordnet.N
print dog
print dog.pos, dog.form
print dog.taggedSenseCount
......@@ -659,15 +659,15 @@ def demo():
# N['dog'] < V['dog']
print "Verb Frames:",
print V['think'][0].verbFrameStrings
print wordnet.V['think'][0].verbFrameStrings
print "Relations:"
print dog[0].relations()
print dog[0].relation(HYPERNYM)
print dog[0][wordnet.HYPERNYM]
print "Glosses:"
print dog[0].gloss
print dog[0].relation(HYPERNYM)[0].gloss
print dog[0].relation(wordnet.HYPERNYM)[0].gloss
print
print "Paths and Distances:"
......@@ -681,22 +681,22 @@ def demo():
print "Closures and Trees:"
print
print ADJ['red'][0].closure(SIMILAR, depth=1)
print ADJ['red'][0].closure(SIMILAR, depth=2)
pprint(dog[0].tree(HYPERNYM))
print wordnet.ADJ['red'][0].closure(wordnet.SIMILAR, depth=1)
print wordnet.ADJ['red'][0].closure(wordnet.SIMILAR, depth=2)
pprint(dog[0].tree(wordnet.HYPERNYM))
# Adjectives that are transitively SIMILAR to any of the senses of 'red'
#flatten1(map(lambda sense:closure(sense, SIMILAR), ADJ['red'])) # too verbose
print "All the words in the hyponym synsets of dog[0]"
print [word for synset in dog[0][HYPONYM] for word in synset]
print [word for synset in dog[0][wordnet.HYPONYM] for word in synset]
print "Hyponyms of the first (and only) sense of 'animal' that are homophonous with verbs:"
print [word for synset in N['animal'][0].closure(HYPONYM) for word in synset if word in V]
print [word for synset in wordnet.N['animal'][0].closure(wordnet.HYPONYM) for word in synset if word in V]
# BROKEN
print "Senses of 'raise'(v.) and 'lower'(v.) that are antonyms:"
print filter(lambda p:p[0] in p[1][ANTONYM], [(r,l) for r in V['raise'] for l in V['lower']])
print filter(lambda p:p[0] in p[1][wordnet.ANTONYM], [(r,l) for r in wordnet.V['raise'] for l in wordnet.V['lower']])
print
print "Similarity: dog~cat"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment