Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   wordnet semantic similarity: how to refer to elements of a pair in alist? can we sort dictionary according to the value? (http://www.velocityreviews.com/forums/t953138-wordnet-semantic-similarity-how-to-refer-to-elements-of-a-pair-in-alist-can-we-sort-dictionary-according-to-the-value.html)

 Token Type 10-07-2012 04:15 PM

wordnet semantic similarity: how to refer to elements of a pair in alist? can we sort dictionary according to the value?

In order to solve the following question, http://nltk.googlecode.com/svn/trunk...ook/ch02.html:
★ Use one of the predefined similarity measures to score the similarity of each of the following pairs of words. Rank the pairs in order of decreasing similarity. How close is your ranking to the order given here, an order that was established experimentally by (Miller & Charles, 1998): car-automobile, gem-jewel, journey-voyage, boy-lad, coast-shore, asylum-madhouse, magician-wizard, midday-noon, furnace-stove, food-fruit, bird-cock, bird-crane, tool-implement, brother-monk, lad-brother, crane-implement, journey-car, monk-oracle, cemetery-woodland, food-rooster, coast-hill, forest-graveyard, shore-woodland, monk-slave, coast-forest, lad-wizard, chord-smile, glass-magician, rooster-voyage, noon-string.

(1) First, I put the word pairs in a list eg.
pairs = [(car, automobile), (gem, jewel), (journey, voyage) ]. According to http://nltk.googlecode.com/svn/trunk/doc/book/ch02.html, I need to put them in the following format so as to calculate teh semantic similarity : wn..synset('right_whale.n.01').path_similarity(wn. synset('minke_whale.n.01')).

In this case, I need to use loop to iterate each element in the above pairs.. How can I refer to each element in the above pairs, i.e. pairs = [(car,automobile), (gem, jewel), (journey, voyage) ]. What's the index for 'car'and for 'automobile'? Thanks for your tips.

(2) Since I can't solve the above index issue. I try to use dictionary as follows:
>>> import nltk
>>> from nltk.corpus import wordnet as wn
>>> pairs = {'car':'automobile', 'gem':'jewel', 'journey':'voyage'}
>>> for key in pairs:

word1 = wn.synset(str(key) + '.n.01')
word2 = wn.synset(str(pairs[key])+'.n.01')
similarity = word1.path_similarity(word2)
print key+'-'+pairs[key],similarity

car-automobile 1.0
journey-voyage 0.25
gem-jewel 0.125

Now it seems that I can calculate the semantic similarity for each groups in the above dictionary. However, I want to sort according to the similarityvalue in the result before print the result out. Can sort dictionary elements according to their values? This is one of the requirement in this exercise. How can we make each group of words (e.g. car-automobile, jounrney-voyage, gem-jewel)
sorted according to their similarity value?
Thanks for your tips.

 Mark Lawrence 10-07-2012 05:49 PM

Re: wordnet semantic similarity: how to refer to elements of a pairin a list? can we sort dictionary according to the value?

On 07/10/2012 17:15, Token Type wrote:
> In order to solve the following question, http://nltk.googlecode.com/svn/trunk...ook/ch02.html:
> ★ Use one of the predefined similarity measures to score the similarity of each of the following pairs of words. Rank the pairs in order of decreasing similarity. How close is your ranking to the order given here, an order that was established experimentally by (Miller & Charles, 1998): car-automobile, gem-jewel, journey-voyage, boy-lad, coast-shore, asylum-madhouse, magician-wizard, midday-noon, furnace-stove, food-fruit, bird-cock, bird-crane, tool-implement, brother-monk, lad-brother, crane-implement, journey-car, monk-oracle, cemetery-woodland, food-rooster, coast-hill, forest-graveyard, shore-woodland, monk-slave, coast-forest, lad-wizard, chord-smile, glass-magician, rooster-voyage, noon-string.
>
> (1) First, I put the word pairs in a list eg.
> pairs = [(car, automobile), (gem, jewel), (journey, voyage) ]. According to http://nltk.googlecode.com/svn/trunk/doc/book/ch02.html, I need to put them in the following format so as to calculate teh semantic similarity : wn.synset('right_whale.n.01').path_similarity(wn.s ynset('minke_whale.n.01')).
>
> In this case, I need to use loop to iterate each element in the above pairs. How can I refer to each element in the above pairs, i.e. pairs = [(car, automobile), (gem, jewel), (journey, voyage) ]. What's the index for 'car' and for 'automobile'? Thanks for your tips.
>
> (2) Since I can't solve the above index issue. I try to use dictionary as follows:
>>>> import nltk
>>>> from nltk.corpus import wordnet as wn
>>>> pairs = {'car':'automobile', 'gem':'jewel', 'journey':'voyage'}
>>>> for key in pairs:

> word1 = wn.synset(str(key) + '.n.01')
> word2 = wn.synset(str(pairs[key])+'.n.01')
> similarity = word1.path_similarity(word2)
> print key+'-'+pairs[key],similarity
>
>
> car-automobile 1.0
> journey-voyage 0.25
> gem-jewel 0.125
>
> Now it seems that I can calculate the semantic similarity for each groups in the above dictionary. However, I want to sort according to the similarity value in the result before print the result out. Can sort dictionary elements according to their values? This is one of the requirement in this exercise. How can we make each group of words (e.g. car-automobile, jounrney-voyage, gem-jewel)
> sorted according to their similarity value?
> Thanks for your tips.
>

In your for loop save the data in a list rather than print it out and
sort according to this
http://wiki.python.org/moin/HowTo/So...dule_Functions

--
Cheers.

Mark Lawrence.

 Terry Reedy 10-07-2012 07:53 PM

Re: wordnet semantic similarity: how to refer to elements of a pairin a list? can we sort dictionary according to the value?

On 10/7/2012 12:15 PM, Token Type wrote:

> In this case, I need to use loop to iterate each element in the above
> pairs. How can I refer to each element in the above pairs, i.e. pairs
> = [(car, automobile), (gem, jewel), (journey, voyage) ]. What's the
> index for 'car' and for 'automobile'? Thanks for your tips.

>>> pairs = [('car', 'automobile'), ('gem', 'jewel')]
>>> pairs[0][0]

'car'
>>> pairs[1][1]

'jewel'
>>> for a,b in pairs: a,b

('car', 'automobile')
('gem', 'jewel')

--
Terry Jan Reedy

 yujian 10-08-2012 03:02 AM

How to control the internet explorer?

I want to save all the URLs in current opened windows, and then close
all the windows.

 alex23 10-08-2012 03:37 AM

Re: How to control the internet explorer?

On Oct 8, 1:03*pm, yujian <yujian4newsgr...@gmail.com> wrote:
> I want to save all the URLs in current opened windows, *and then close
> all the windows.

Try mechanize or Selenium.

 Token Type 10-09-2012 03:13 AM

Re: wordnet semantic similarity: how to refer to elements of a pairin a list? can we sort dictionary according to the value?

yes, thanks all your tips. I did try sorted with itemgetter. However, the sorted results are same as follows whether I set reverse=True or reverse= False. Isn't it strange? Thanks.

>>> import nltk
>>> from nltk.corpus import wordnet as wn
>>> pairs = {'car':'automobile', 'gem':'jewel', 'journey':'voyage'}
>>> for key in pairs:

list_simi=[]
from operator import itemgetter
word1 = wn.synset(str(key) + '.n.01')
word2 = wn.synset(str(pairs[key])+'.n.01')
similarity = word1.path_similarity(word2)
list_simi.append((key+'-'+pairs[key],similarity))
sorted(list_simi, key=itemgetter(1), reverse=True)

[('car-automobile', 1.0)]
[('journey-voyage', 0.25)]
[('gem-jewel', 0.125)]
>>> for key in pairs:

list_simi=[]
from operator import itemgetter
word1 = wn.synset(str(key) + '.n.01')
word2 = wn.synset(str(pairs[key])+'.n.01')
similarity = word1.path_similarity(word2)
list_simi.append((key+'-'+pairs[key],similarity))
sorted(list_simi, key=itemgetter(1), reverse=False)

[('car-automobile', 1.0)]
[('journey-voyage', 0.25)]
[('gem-jewel', 0.125)]

 Token Type 10-09-2012 03:13 AM

Re: wordnet semantic similarity: how to refer to elements of a pairin a list? can we sort dictionary according to the value?

yes, thanks all your tips. I did try sorted with itemgetter. However, the sorted results are same as follows whether I set reverse=True or reverse= False. Isn't it strange? Thanks.

>>> import nltk
>>> from nltk.corpus import wordnet as wn
>>> pairs = {'car':'automobile', 'gem':'jewel', 'journey':'voyage'}
>>> for key in pairs:

list_simi=[]
from operator import itemgetter
word1 = wn.synset(str(key) + '.n.01')
word2 = wn.synset(str(pairs[key])+'.n.01')
similarity = word1.path_similarity(word2)
list_simi.append((key+'-'+pairs[key],similarity))
sorted(list_simi, key=itemgetter(1), reverse=True)

[('car-automobile', 1.0)]
[('journey-voyage', 0.25)]
[('gem-jewel', 0.125)]
>>> for key in pairs:

list_simi=[]
from operator import itemgetter
word1 = wn.synset(str(key) + '.n.01')
word2 = wn.synset(str(pairs[key])+'.n.01')
similarity = word1.path_similarity(word2)
list_simi.append((key+'-'+pairs[key],similarity))
sorted(list_simi, key=itemgetter(1), reverse=False)

[('car-automobile', 1.0)]
[('journey-voyage', 0.25)]
[('gem-jewel', 0.125)]

 Token Type 10-09-2012 03:23 AM

Re: wordnet semantic similarity: how to refer to elements of a pairin a list? can we sort dictionary according to the value?

Dear all, the problem has been solved as follows. Thanks anyway:
>>> import nltk
>>> from nltk.corpus import wordnet as wn
>>> pairs = {'car':'automobile', 'gem':'jewel', 'journey':'voyage'}
>>> list_simi=[]
>>> for key in pairs:

word1 = wn.synset(str(key) + '.n.01')
word2 = wn.synset(str(pairs[key])+'.n.01')
similarity = word1.path_similarity(word2)
list_simi.append((key+'-'+pairs[key],similarity))

>>> from operator import itemgetter
>>> sorted(list_simi, key=itemgetter(1), reverse=False)

[('gem-jewel', 0.125), ('journey-voyage', 0.25), ('car-automobile', 1.0)]
>>> sorted(list_simi, key=itemgetter(1), reverse=True)

[('car-automobile', 1.0), ('journey-voyage', 0.25), ('gem-jewel', 0.125)]
>>> sorted(list_simi, key=itemgetter(1))

[('gem-jewel', 0.125), ('journey-voyage', 0.25), ('car-automobile', 1.0)]

 Token Type 10-09-2012 03:23 AM

Re: wordnet semantic similarity: how to refer to elements of a pairin a list? can we sort dictionary according to the value?

Dear all, the problem has been solved as follows. Thanks anyway:
>>> import nltk
>>> from nltk.corpus import wordnet as wn
>>> pairs = {'car':'automobile', 'gem':'jewel', 'journey':'voyage'}
>>> list_simi=[]
>>> for key in pairs:

word1 = wn.synset(str(key) + '.n.01')
word2 = wn.synset(str(pairs[key])+'.n.01')
similarity = word1.path_similarity(word2)
list_simi.append((key+'-'+pairs[key],similarity))

>>> from operator import itemgetter
>>> sorted(list_simi, key=itemgetter(1), reverse=False)

[('gem-jewel', 0.125), ('journey-voyage', 0.25), ('car-automobile', 1.0)]
>>> sorted(list_simi, key=itemgetter(1), reverse=True)

[('car-automobile', 1.0), ('journey-voyage', 0.25), ('gem-jewel', 0.125)]
>>> sorted(list_simi, key=itemgetter(1))

[('gem-jewel', 0.125), ('journey-voyage', 0.25), ('car-automobile', 1.0)]

 Ian Kelly 10-09-2012 03:24 AM

Re: wordnet semantic similarity: how to refer to elements of a pairin a list? can we sort dictionary according to the value?

On Mon, Oct 8, 2012 at 9:13 PM, Token Type <typetoken@gmail.com> wrote:
> yes, thanks all your tips. I did try sorted with itemgetter. However, the sorted results are same as follows whether I set reverse=True or reverse= False. Isn't it strange? Thanks.

First of all, "sorted" does not sort the list in place as you seem to
be expecting.
It returns a new sorted list. Since your code does not store the
return value of the sorted call anywhere, the sorted list is discarded
and only the original list is kept. If you want to sort a list in
place, use the list.sort method instead.

Second, you're not sorting the overall list. On each iteration your
code: 1) assigns a new empty list to list_simi; 2) processes one of
the pairs; 3) adds the pair to the empty list; and 4) sorts the list.
On the next iteration you then start all over again with a new empty
list, and so when you get to the sorting step you're only sorting one
item each time. You need to accumulate the list instead of wiping it
out on each iteration, and only sort it after the loop has completed.

All times are GMT. The time now is 04:59 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.