本文整理汇总了Python中nltk.corpus.wordnet.path_similarity函数的典型用法代码示例。如果您正苦于以下问题:Python path_similarity函数的具体用法?Python path_similarity怎么用?Python path_similarity使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了path_similarity函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: checkFirstSentence_test5
def checkFirstSentence_test5(paragraphID, paragraph):
if paragraphID in cache:
tokens = cache[paragraphID]
else:
tokens = getParagraphTokenIntersectionByID(paragraphID)
cache[paragraphID] = tokens
for token in tokens:
if token in paragraph[0]:
return True
for token in tokens:
tokenSynset = getSynset(token, paragraphID)
if tokenSynset:
partOfSpeechMap = POSCache[paragraphID]
tokenPos = partOfSpeechMap[token]
for word in paragraph[0].split(" "):
word = word.strip().lower()
if word in partOfSpeechMap:
wordPos = partOfSpeechMap[word]
if wordPos == tokenPos:
wordSynset = getSynset(word, paragraphID)
if wordSynset:
if (
wn.path_similarity(tokenSynset, wordSynset)
and wn.path_similarity(tokenSynset, wordSynset) > 0.13
):
return True
return False
示例2: similarity_by_path
def similarity_by_path(sense1, sense2, option="path"):
if option.lower() in ["path", "path_similarity"]: # Path similaritys
return max(wn.path_similarity(sense1,sense2),
wn.path_similarity(sense1,sense2))
elif option.lower() in ["wup", "wupa", "wu-palmer", "wu-palmer"]: # Wu-Palmer
return wn.wup_similarity(sense1, sense2)
elif option.lower() in ['lch', "leacock-chordorow"]: # Leacock-Chodorow
if sense1.pos != sense2.pos: # lch can't do diff POS
return 0
return wn.lch_similarity(sense1, sense2)
示例3: wnsensesim
def wnsensesim(synset1, synset2, metric):
#return wn similarity of two synsets according to metric
#if metric == 'path_similarity':
print "synset1:%r"%synset1
print "synset2:%r"%synset2
if metric == 'path_similarity':
print wn.path_similarity(synset1, synset2)
return wn.path_similarity(synset1, synset2)
else:#add more similarity measures e.g., jcn
print "Unsupported wn similarity measure requested"
示例4: similarity_by_path
def similarity_by_path(sense1, sense2, option="path"):
""" Returns maximum path similarity between two senses. """
if option.lower() in ["path", "path_similarity"]: # Path similaritys
return max(wn.path_similarity(sense1,sense2),
wn.path_similarity(sense1,sense2))
elif option.lower() in ["wup", "wupa", "wu-palmer", "wu-palmer"]: # Wu-Palmer
return wn.wup_similarity(sense1, sense2)
elif option.lower() in ['lch', "leacock-chordorow"]: # Leacock-Chodorow
if sense1.pos != sense2.pos: # lch can't do diff POS
return 0
return wn.lch_similarity(sense1, sense2)
return wn.lin_similarity(sense1, sense2, wnic.ic('ic-bnc-add1.dat'))
示例5: get_relation
def get_relation(syn1,syn2,sim_metric):
from nltk.corpus import wordnet as wn
if sim_metric == "path":
# https://stackoverflow.com/questions/20075335/is-wordnet-path-similarity-commutative
sim_score = min(wn.path_similarity(syn1,syn2), wn.path_similarity(syn2,syn1))
elif sim_metric == "lch":
if syn1.pos() == syn2.pos():
sim_score = syn1.lch_similarity(syn2)
else:
sim_score = 0
elif sim_metric == "wup":
sim_score = syn1.wup_similarity(syn2)
if sim_score: return sim_score
else: return 0
示例6: get_score
def get_score(tags, groups):
sscore = 0
scount = 0
illegal_word = 0
if (tags != None ) :
for g in groups:
for x in k.tags:
try :
#print str(x.text),
#check substring else calculate words similarity score
if g in str(x.text).lower():
sscore += 2.0
scount += 1
else:
tag = wn.synset(str(x.text).lower()+'.n.01')
group = wn.synset(g+ '.n.01')
sem = wn.path_similarity(group,tag)
if sem >= 0.3 :
sscore += sem
scount += 1
except:
illegal_word += 1
if scount != 0 :
return sscore/scount
else :
return 0
示例7: internal_word_max_WSD
def internal_word_max_WSD(sentence, word):
"""
Auxiliary function for sem_wsd()
Input: a sentence and a word in the sentence,
sentence is a list of words, not a string
Return: synset(sense) of the word that maximize one similarity with another word in the sentence
Derived from code at http://www.jaist.ac.jp/~s1010205/sitemap-2/styled-7/
"""
wordsynsets = wn.synsets(word)
bestScore = 0.0
result = None
for synset in wordsynsets:
for w in sentence:
score = 0.0
for wsynset in wn.synsets(w):
sim = wn.path_similarity(wsynset, synset)
if(sim == None):
continue
else:
score += sim
if (score > bestScore):
bestScore = score
result = synset
return result
示例8: __similarity
def __similarity(self, word, compareto):
try:
score = word.jcn_similarity(compareto, self.wordnet_ic, True)
except:
score = wordnet.path_similarity(word, compareto)
if score == -1: score = None #No path between the words was found
return score
示例9: create_graphs
def create_graphs(doc_list):
documents = doc_list
if documents is None:
documents = default_document_list()
distance_functions = [
(wn.lch_similarity(SYNSETS[0], SYNSETS[0]), 'lch', lambda sense_1, sense_2: wn.lch_similarity(sense_1, sense_2)),
(1.0, 'lin', lambda sense_1, sense_2: wn.lin_similarity(sense_1, sense_2, CORPUS)),
(10.636958516573292, 'res', lambda sense_1, sense_2: wn.res_similarity(sense_1, sense_2, CORPUS)),
(wn.jcn_similarity(SYNSETS[0], SYNSETS[0], CORPUS), 'jcn', lambda sense_1, sense_2: wn.jcn_similarity(sense_1, sense_2, CORPUS)),
(1.0, 'path', lambda sense_1, sense_2: wn.path_similarity(sense_1, sense_2)),
]
all_senses = []
for doc in documents:
for sense in doc.top_senses():
all_senses.append((sense, doc.name))
against_colors = ['r', 'b', 'g']
against_to = [wn.synset(word) for word in ["economy.n.01", "philosophy.n.02", "politics.n.01"]]
create_against_graph('phyl_eco_pol', documents, all_senses, against_to, distance_functions, against_colors)
against_to = SYNSETS
against_colors = [(random(), random(), random()) for _i in range(0, len(SYNSETS))]
create_against_graph('handpicked', documents, all_senses, against_to, distance_functions, against_colors)
create_graph_top_senses(documents, all_senses, distance_functions)
示例10: disambiguateWordSenses
def disambiguateWordSenses(self,sentence,word):
wordsynsets = wn.synsets(word)
bestScore = 0.0
result = None
for synset in wordsynsets:
for w in nltk.word_tokenize(sentence):
score = 0.0
for wsynset in wn.synsets(w):
sim = wn.path_similarity(wsynset, synset)
if(sim == None):
continue
else:
score += sim
if (score > bestScore):
bestScore = score
result = synset
if result:
pos = result.pos()
offset = result.offset()
pos_score=0.0
neg_score=0.0
if (pos, offset) in self.db:
pos_score, neg_score = self.db[(pos, offset)]
obj = 1.0-(pos_score+neg_score)
#print "%%%%%%%%%%"
#print pos_score,neg_score, obj
else:
obj=1.0
pos=None
pos_score=0.0
neg_score=0.0
return obj,pos,pos_score,neg_score
示例11: relevancy_score
def relevancy_score(desiredDoc):
#Each word has score between 0 to 1 in terms of similarity. "None" is returned
#there is no similarity.
newWord =searchWord + ".n.01"
searchWordwn = wn.synset(newWord)
## print (newWord)
## print (searchWordwn)
relevancyScore = 0
currentWordScore = 0
memo = {}
for i in range(len(keywords)):
currentWord = keywords[i][0]
if currentWord in memo:
currentWordScore = memo[currentWord]
if currentWordScore != None:
relevancyScore += currentWordScore
else:
if wn.synsets(currentWord, pos = wn.NOUN) != []:
currentWordwn = wn.synsets(currentWord, pos = wn.NOUN)[0]
currentWordScore = wn.path_similarity(searchWordwn,currentWordwn)
memo[currentWord] = currentWordScore
if currentWordScore != None:
relevancyScore += currentWordScore
return relevancyScore
示例12: syn
def syn(conw,candw):
syn_strings = lambda x : str(x)[8:-2]
pos = lambda y : y[-4:]
#find the synsets of the context word
ssets = wn.synsets(conw)
sset_strings = map(syn_strings,ssets)
#synsets of the candidate word
csets = wn.synsets(candw)
cset_strings = map(syn_strings,csets)
#take a synset whose part of speech matches
matches = [(i,j) for i in range(len(sset_strings)) for j in range(len(cset_strings)) if pos(sset_strings[i]) == pos(cset_strings[j])]
similarity = 0
if matches != []:
(k,l) = matches[0]
similarity = wn.path_similarity(ssets[k],csets[l])
else:
similarity = 0
if similarity is None:
return 0
else:
return similarity
示例13: disambiguate_word_senses
def disambiguate_word_senses(self, sentence, word):
"""
Attempts to determine the proper sense of the target
word from the sentence in which it appears.
Args:
sentence: String representation of the sentence
word: String represtnation of word
Returns:
Returns a synset which is the best guess.
Example:
disambiguateWordSenses('A cat is a good pet', 'cat')
OUT: Synset('cat.v.01')
"""
wordsynsets = wn.synsets(word)
bestScore = 0.0
result = None
for synset in wordsynsets:
for w in nltk.word_tokenize(sentence):
score = 0.0
for wsynset in wn.synsets(w):
sim = wn.path_similarity(wsynset, synset)
if(sim == None):
continue
else:
score += sim
if (score > bestScore):
bestScore = score
result = synset
return result
示例14: findMaxPathSimilarity
def findMaxPathSimilarity(ingredSynsets, foodSynsets):
maxPathSimilarity = 0
for synseta in ingredSynsets:
for synsetb in foodSynsets:
pathSim = wn.path_similarity(synseta, synsetb)
if pathSim > maxPathSimilarity:
maxPathSimilarity = pathSim
return maxPathSimilarity
示例15: word_similarity
def word_similarity(self, word1, word2):
w1synsets = wn.synsets(word1)
w2synsets = wn.synsets(word2)
maxsim = 0
for w1s in w1synsets:
for w2s in w2synsets:
current = wn.path_similarity(w1s, w2s)
if (current > maxsim and current > 0):
maxsim = current
return maxsim