3
votes

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?

1
It would be a lot easier to answer your question if you had included the full stack trace, which shows where the error was triggered.alexis
corrected the synonymIfExistsKeithx

1 Answers

5
votes

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.