0

I am trying to get the actual output from the generator but i get the ouput as generator object. Please help me achieve the actual output from the generator

import spacy
nlp = spacy.load('en')

def lemmatizer(words):
     yield from [w.lemma_ for w in nlp(words)]

list1 = ['birds hanging on street','people playing cards']

a = list(map(lemmatizer,list1))

Output:

a
[<generator object....>,
<generator object....>]

Expected output:

a
['birds hang on street',
'people play card']
prog
  • 1,073
  • 5
  • 17
  • 2
    `a = list(map(list, map(lemmatizer,list1)))` – Patrick Artner Dec 28 '21 at 10:52
  • @PatrickArtner please write the answer, now i get the output as list within list of tokens, pls join the inner list as each sentence, thanks.. should i change the lemmatizer function a bit for this? – prog Dec 28 '21 at 12:04

2 Answers2

1

This worked for me with the help of @PatrickArtner comment

a = list(map(list, map(lemmatizer,list1)))
b = list(map(' '.join, a))
prog
  • 1,073
  • 5
  • 17
-1

Use next to yield from the generator. Adding next like a = list(next(map(lemmatizer,list1))) should work.

import spacy
nlp = spacy.load('en')

def lemmatizer(words):
     yield from [w.lemma_ for w in nlp(words)]

list1 = ['birds hanging on street','people playing cards']

a = list(next(map(lemmatizer,list1)))
cyborg
  • 554
  • 2
  • 7