Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9132705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:21:32+00:00 2026-06-17T08:21:32+00:00

I need to work on a project that require NLTK so I started learning

  • 0

I need to work on a project that require NLTK so I started learning Python two weeks ago but struggling to understand Python and NLTK.

From the NLTK documentation, I can understand the following codes and they work well if I manually add the word apple and pear into the codes below.

from nltk.corpus import wordnet as wn

apple = wn.synset('apple.n.01')
pear = wn.synset('pear.n.01')

print apple.lch_similarity(pear)

Output: 2.53897387106

However, I need to use the NLTK to work with a list of items. For example, I have a list of items below and I would like to compare the items from list1 with list2 – for example: compare word1 from list1 with every word in list 2, then word2 from list1 with every word from list2 until all words in list1 is compared.

list1 = ["apple", "honey", "drinks", "flowers", "paper"]
list2 = ["pear", "shell", "movie", "fire", "tree", "candle"]

wordFromList1 = list1[0]
wordFromList2 = list2[0]

wordFromList1 = wn.synset(wordFromList1)
wordFromList2 = wn.synset(wordFromList2)    

print wordFromList1.lch_similarity(wordFromList2)

The codes above will of course gives an error. Can anyone show me how I can pass a variable into synset method [wn.synset(*pass_variable_in_here*)] so that I can use a double loop to get the lch_similarity values for them. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:21:33+00:00Added an answer on June 17, 2026 at 8:21 am

    wordnet.synset expects a 3-part
    name
    string of the form:
    word.pos.nn.

    You did not specify the pos.nn part for each word in list1 and
    list2.

    It seems reasonable to assume that all the words are nouns, so we could try
    appending the string '.n.01' to each string in list1 and list2:

    for word1, word2 in IT.product(list1, list2):
        wordFromList1 = wordnet.synset(word1+'.n.01')
        wordFromList2 = wordnet.synset(word2+'.n.02')
    

    That does not work, however. wordnet.synset('drinks.n.01') raises a WordNetError.

    On the other hand, the same doc
    page
    shows you can
    lookup similar words using the synsets method:

    For example, wordnet.synsets('drinks') returns the list:

    [Synset('drink.n.01'),
     Synset('drink.n.02'),
     Synset('beverage.n.01'),
     Synset('drink.n.04'),
     Synset('swallow.n.02'),
     Synset('drink.v.01'),
     Synset('drink.v.02'),
     Synset('toast.v.02'),
     Synset('drink_in.v.01'),
     Synset('drink.v.05')]
    

    So at this point, you need to give some thought to what you want the program to do. If you are okay with just picking the first item in this list as a proxy for drinks,
    then you could use

    for word1, word2 in IT.product(list1, list2):
        wordFromList1 = wordnet.synsets(word1)[0]
        wordFromList2 = wordnet.synsets(word2)[0]
    

    which would result in a program that looks like this:

    import nltk.corpus as corpus
    import itertools as IT
    
    wordnet = corpus.wordnet
    list1 = ["apple", "honey", "drinks", "flowers", "paper"]
    list2 = ["pear", "shell", "movie", "fire", "tree", "candle"]
    
    for word1, word2 in IT.product(list1, list2):
        # print(word1, word2)
        wordFromList1 = wordnet.synsets(word1)[0]
        wordFromList2 = wordnet.synsets(word2)[0]
        print('{w1}, {w2}: {s}'.format(
            w1 = wordFromList1.name,
            w2 = wordFromList2.name,
            s = wordFromList1.lch_similarity(wordFromList2)))
    

    which yields

    apple.n.01, pear.n.01: 2.53897387106
    apple.n.01, shell.n.01: 1.07263680226
    apple.n.01, movie.n.01: 1.15267950994
    apple.n.01, fire.n.01: 1.07263680226
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey there, I've got a project at work that may require me to load/unload
I have a project where I need to work on a new feature that
I've started work on a project that requires an SQL Server Database. I will
I'm trying to work through a small Perl learning project that requires reading 4
I need to work on C++ project on my windows machine. My project will
I need some help with a work project I have been assigned. At the
I work on a pretty large rails project at work. Sometimes I need to
We are starting a rather complex new project at work, and need some sort
I need to work out the best way to read data that is being
I need to work with some old C++ code that was developed in Visual

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.