viyyer (viyyer) wrote,
viyyer
viyyer

Some pywordnet sample python code

Some pywordnet sample python code





###get word,words

from wordnet import

from wntools import


import sys

import string

import MySQLdb



default = "default"

def lookupdict(inputlist):#takes an input of all the words in a list

OutputList = []

for count in range(len(inputlist)):

tryword=inputlist[count]

OutputList.append(tryword)

for values in [N,V,ADV,ADJ]:

try:

wnword = values[tryword]

DictSenses = wnword.getSenses()

Scount=len(DictSenses)

#print "Total number of Senses:-",Scount

for j in range(Scount):

WCount=len(DictSenses[j].synset)

#print "Total number of Synsets:-",

WCount,"sense count = ",j

for i in range(WCount):

#try:

teststr = DictSenses[j].\

synset[i].word()._str_()

#print "test no1",teststr

#print teststr[-4]

if(teststr[-4]=='('):

teststr = teststr[:-4]

#print "test no2",teststr

elif(teststr[-6]=='('):

teststr = teststr[:-6]

#print "test no2",teststr

else:

indexval=teststr.index('(')

teststr = teststr[:-indexval]

#print teststr

if(OutputList._contains_(teststr)==0):

OutputList.append(teststr)

#print OutputList

#print DictSenses[j].synset[i].\

#print DictSenses[j].synset[i].word()._str_(),"word count = ",i



#DictSearch(wnWord,ResultList)

except KeyError:

print tryword,"not in ",values

#print OutputList

return OutputList



#

#

#

### find similar word,words using wordnet

def searchtheddatabase(database,wlist):

resultlist=[]

# search database

cursor=database.cursor()

#cursor.execute("show tables")

#tablelist=cursor.fetchall()

#for tname in tablelist:

cursor.execute("select * from Document")

completelist=cursor.fetchall()

i=0

#for(i in range(len(completelist)))):

while(i
[Error: Irreparable invalid markup ('<len(completelist)):<br>') in entry. Owner must fix manually. Raw contents below.]

Some pywordnet sample python code <br />
<br />
<br />
<blockquote>###get word,words<br />
from wordnet import <strong><br />
from wntools import </strong><br />
import sys<br />
import string<br />
import MySQLdb<br />
<br />
default = "default"<br />
def lookupdict(inputlist):#takes an input of all the words in a list<br />
OutputList = []<br />
for count in range(len(inputlist)):<br />
tryword=inputlist[count]<br />
OutputList.append(tryword)<br />
for values in [N,V,ADV,ADJ]:<br />
try:<br />
wnword = values[tryword] <br />
DictSenses = wnword.getSenses()<br />
Scount=len(DictSenses)<br />
#print "Total number of Senses:-",Scount<br />
for j in range(Scount):<br />
WCount=len(DictSenses[j].synset)<br />
#print "Total number of Synsets:-",<br />
WCount,"sense count = ",j<br />
for i in range(WCount):<br />
#try:<br />
teststr = DictSenses[j].\<br />
synset[i].word().<u>_str_</u>()<br />
#print "test no1",teststr <br />
#print teststr[-4]<br />
if(teststr[-4]=='('):<br />
teststr = teststr[:-4]<br />
#print "test no2",teststr<br />
elif(teststr[-6]=='('):<br />
teststr = teststr[:-6]<br />
#print "test no2",teststr<br />
else:<br />
indexval=teststr.index('(')<br />
teststr = teststr[:-indexval]<br />
#print teststr<br />
if(OutputList.<u>_contains_</u>(teststr)==0):<br />
OutputList.append(teststr)<br />
#print OutputList<br />
#print DictSenses[j].synset[i].\<br />
#print DictSenses[j].synset[i].word().<u>_str_</u>(),"word count = ",i<br />
<br />
#DictSearch(wnWord,ResultList)<br />
except KeyError:<br />
print tryword,"not in ",values<br />
#print OutputList <br />
return OutputList<br />
<br />
#<br />
#<br />
#<br />
### find similar word,words using wordnet<br />
def searchtheddatabase(database,wlist):<br />
resultlist=[]<br />
# search database<br />
cursor=database.cursor()<br />
#cursor.execute("show tables")<br />
#tablelist=cursor.fetchall()<br />
#for tname in tablelist:<br />
cursor.execute("select * from Document")<br />
completelist=cursor.fetchall()<br />
i=0<br />
#for(i in range(len(completelist)))):<br />
while(i<len(completelist)):<br />
j=0<br />
#for(j in range(len(completelist[i]))):<br />
while(j<len(completelist[i])): <br />
if(type(completelist[i][j])==type("")):<br />
entrylist=completelist[i][j].lower()<br />
k=0<br />
#for(k in range(len(wlist)):<br />
while(k<len(wlist)):<br />
if(entrylist.<u>_contains_</u>(wlist[k])):<br />
if(resultlist.<u>_contains_</u>(completelist[i])==0):<br />
resultlist.append(completelist[i])<br />
else:<br />
pass<br />
k=k+1<br />
j=j+1<br />
i=i+1<br />
#print resultlist<br />
return resultlist<br />
<br />
def subtractlist(list2 , list1):<br />
list3 = list(list2)<br />
#for(i in range(len(list1))):<br />
i=0<br />
while(i<len(list1)): <br />
if list3.<u>_contains_</u>(list1[i]):<br />
list3.remove(list1[i])<br />
i=i+1<br />
return list3<br />
<br />
if <u>_name_</u>=="<u>_main_</u>":<br />
trywordlist = [] ### rightnow.. only one word <img src="http://blogs.srijan.in/viyyer/templates/default/img/emoticons/smile.png" alt=":-)" style="display: inline; vertical-align: bottom;" class="emoticon" /><br />
if len(sys.argv) > 1:<br />
trywordlist.append(sys.argv[1])<br />
else:<br />
# try:<br />
trywordlist.append(raw_input("Enter word for wordnet \<br />
to find similar words for:-").lower())<br />
# except InputError:<br />
trywordlist.append(default)<br />
wordnetlist=lookupdict(trywordlist)<br />
print wordnetlist<br />
mydb = MySQLdb.connect(host="projects.sarai.kit", user="viyyer",\<br />
passwd = "viyyer",db="archive")<br />
<br />
###search any given table and return tuples that are interesting. <img src="http://blogs.srijan.in/viyyer/templates/default/img/emoticons/laugh.png" alt=":-D" style="display: inline; vertical-align: bottom;" class="emoticon" /><br />
onelist=searchtheddatabase(mydb,trywordlist)<br />
print len(onelist),"is the size of the list of base words"<br />
twolist=searchtheddatabase(mydb,wordnetlist)<br />
print len(twolist),"is the size of the list of wordnet extracted words"<br />
sublist = subtractlist(twolist,onelist)<br />
print sublist<br />
</blockquote><a style="text-align: right" href="http://blogs.srijan.in/viyyer/index.php?/archives/57-Some-pywordnet-sample-python-code.html#comments">post/read comments</a>
Subscribe
Comments for this post were disabled by the author