Paraphrasing using NLTK approach in Python

Question:

Trying to do simple paraphraser using tokenizing in NLTK.

Doing the following functions:

from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
from nltk.corpus import wordnet as wn

def tag(sentence):
 words = word_tokenize(sentence)
 words = pos_tag(words)
 return words

def paraphraseable(tag):
 return tag.startswith('NN') or tag == 'VB' or tag.startswith('JJ')

def pos(tag):
 if tag.startswith('NN'):
  return wn.NOUN
 elif tag.startswith('V'):
  return wn.VERB

def synonyms(word, tag):
 return set([lemma.name for lemma in sum([ss.lemmas for ss in    wn.synsets(word, pos(tag))],[])])

def synonymIfExists(sentence):
 for (word, t) in tag(sentence):
   if paraphraseable(t):
    syns = synonyms(word, t)
    if syns:
     if len(syns) > 1:
      yield [word, list(syns)]
      continue
   yield [word, []]

def paraphrase(sentence):
 return [x for x in synonymIfExists(sentence)]

paraphrase("The quick brown fox jumps over the lazy dog")

After doing the last line (paraphrase(“The quick brown fox jumps over the lazy dog”)) it gives me error just like that:

can only concatenate list (not “method”) to list

What seems to be wrong in that case?

Asked By: Keithx

||

Answers:

The error is in synonyms(): lemmas is a class method of Synset, and name is a class method of Lemma. This means that you have to call them explicitly as functions by supplying () as well, like so:

def synonyms(word, tag):
    lemma_lists = [ss.lemmas() for ss in wn.synsets(word, pos(tag))]
    lemmas = [lemma.name() for lemma in sum(lemma_lists, [])]
    return set(lemmas)

If you fix that, your error message disappears.

Answered By: Schmuddi

Lemmas is a class method of Synset, and name is a class method of Lemma, so the error is in the synonyms() function. This implies that you must explicitly call them as functions by including (), as in the following example:
Click me for more answer

Answered By: DarkNepal
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.