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 * ...@@ -57,5 +57,5 @@ from util import *
from cache import * from cache import *
from lexname import * from lexname import *
from similarity import * from similarity import *
from wordnet import * from synset import *
from browse import * from browse import *
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
# URL: <http://nltk.sf.net> # URL: <http://nltk.sf.net>
# For license information, see LICENSE.TXT # For license information, see LICENSE.TXT
from nltk.wordnet import * from util import *
from dictionary import *
from textwrap import TextWrapper from textwrap import TextWrapper
from string import join from string import join
from random import randint from random import randint
...@@ -77,23 +78,58 @@ def browse(word="", index=0): ...@@ -77,23 +78,58 @@ def browse(word="", index=0):
input = raw_input(synsets[index][0] + "_" + `index` + "/" + `len(synsets)` + "> ") input = raw_input(synsets[index][0] + "_" + `index` + "/" + `len(synsets)` + "> ")
else: else:
input = raw_input("> ") # safety net 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 index = 0
# sense selection
elif input[0] in "0123456789": elif input[0] in "0123456789":
if int(input) < len(synsets): if int(input) < len(synsets):
index = int(input) index = int(input)
print_gloss(synsets, index) print_gloss(synsets, index)
else: else:
print "There are %d synsets" % len(synsets) print "There are %d synsets" % len(synsets)
# more info
elif input[0] is "a": elif input[0] is "a":
print_all(synsets) print_all(synsets)
elif input[0] is "g": elif input[0] is "g":
print_gloss(synsets, index) print_gloss(synsets, index)
elif input[0] is "v": elif input[0] is "v":
print_all_glosses(synsets) print_all_glosses(synsets)
elif input[0] is "h": elif input[0] is "s":
help() 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": elif input[0] is "r":
synsets = _random_synset(D) synsets = _random_synset(D)
elif input[0] is "u": elif input[0] is "u":
...@@ -114,27 +150,14 @@ def browse(word="", index=0): ...@@ -114,27 +150,14 @@ def browse(word="", index=0):
index = 0 index = 0
except IndexError: except IndexError:
print "Cannot go down" print "Cannot go down"
elif input[0] is "s":
print "Synonyms:", join(word for word in synsets[index]) # miscellany
elif input[0] in "N": # nouns elif input[0] is "h":
if word in N: help()
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]
elif input[0] is "q": elif input[0] is "q":
print "Goodbye" print "Goodbye"
break break
else: else:
print "Unrecognised command, type 'h' for help" print "Unrecognised command, type 'h' for help"
......
...@@ -63,7 +63,7 @@ class Dictionary(object): ...@@ -63,7 +63,7 @@ class Dictionary(object):
pos = self.pos pos = self.pos
def loader(key=key, line=line, indexFile=self.indexFile): def loader(key=key, line=line, indexFile=self.indexFile):
from wordnet import Word from synset import Word
line = line or indexFile.get(key) line = line or indexFile.get(key)
return line and Word(line) return line and Word(line)
...@@ -82,7 +82,7 @@ class Dictionary(object): ...@@ -82,7 +82,7 @@ class Dictionary(object):
""" """
def loader(pos=self.pos, offset=offset, dataFile=self.dataFile): def loader(pos=self.pos, offset=offset, dataFile=self.dataFile):
from wordnet import Synset from synset import Synset
dataFile.seek(offset) dataFile.seek(offset)
line = dataFile.readline() line = dataFile.readline()
return Synset(pos, offset, line) return Synset(pos, offset, line)
......
...@@ -641,14 +641,14 @@ def _equalsIgnoreCase(a, b): ...@@ -641,14 +641,14 @@ def _equalsIgnoreCase(a, b):
def demo(): def demo():
from nltk.wordnet import N, V, ADJ, ADV, HYPERNYM from nltk import wordnet
from pprint import pprint from pprint import pprint
dog = N['dog'] dog = wordnet.N['dog']
cat = N['cat'] cat = wordnet.N['cat']
print "N['dog']" print "wordnet.N['dog']"
print 'dog' in N print 'dog' in wordnet.N
print dog print dog
print dog.pos, dog.form print dog.pos, dog.form
print dog.taggedSenseCount print dog.taggedSenseCount
...@@ -659,15 +659,15 @@ def demo(): ...@@ -659,15 +659,15 @@ def demo():
# N['dog'] < V['dog'] # N['dog'] < V['dog']
print "Verb Frames:", print "Verb Frames:",
print V['think'][0].verbFrameStrings print wordnet.V['think'][0].verbFrameStrings
print "Relations:" print "Relations:"
print dog[0].relations() print dog[0].relations()
print dog[0].relation(HYPERNYM) print dog[0][wordnet.HYPERNYM]
print "Glosses:" print "Glosses:"
print dog[0].gloss print dog[0].gloss
print dog[0].relation(HYPERNYM)[0].gloss print dog[0].relation(wordnet.HYPERNYM)[0].gloss
print print
print "Paths and Distances:" print "Paths and Distances:"
...@@ -681,22 +681,22 @@ def demo(): ...@@ -681,22 +681,22 @@ def demo():
print "Closures and Trees:" print "Closures and Trees:"
print print
print ADJ['red'][0].closure(SIMILAR, depth=1) print wordnet.ADJ['red'][0].closure(wordnet.SIMILAR, depth=1)
print ADJ['red'][0].closure(SIMILAR, depth=2) print wordnet.ADJ['red'][0].closure(wordnet.SIMILAR, depth=2)
pprint(dog[0].tree(HYPERNYM)) pprint(dog[0].tree(wordnet.HYPERNYM))
# Adjectives that are transitively SIMILAR to any of the senses of 'red' # Adjectives that are transitively SIMILAR to any of the senses of 'red'
#flatten1(map(lambda sense:closure(sense, SIMILAR), ADJ['red'])) # too verbose #flatten1(map(lambda sense:closure(sense, SIMILAR), ADJ['red'])) # too verbose
print "All the words in the hyponym synsets of dog[0]" 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 "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 # BROKEN
print "Senses of 'raise'(v.) and 'lower'(v.) that are antonyms:" 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
print "Similarity: dog~cat" 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