Spacy - Convert Token type into list
Asked Answered
A

2

5

I have few elements which I got after performing operation in spacy having type Input -

li = ['India', 'Australia', 'Brazil']
for i in li:
    print(type(i))

Output:

<class 'spacy.tokens.token.Token'>

<class 'spacy.tokens.token.Token'>

<class 'spacy.tokens.token.Token'>

I want to make all elements in list with str type for iteration. Expected output -

li = ['India', 'Australia', 'Brazil']
for i in li:
    print(type(i))

Output

<class 'str'>

<class 'str'>

<class 'str'>

please suggest some optimized way..

Ambulance answered 2/11, 2018 at 12:30 Comment(0)
Z
10

Spacy Token has a attribute called text. Here's a complete example:

import spacy
nlp = spacy.load('en_core_web_sm')
t = (u"India Australia Brazil")
li = nlp(t)
for i in li:
    print(i.text)

or if you want the list of tokens as list of strings:

list_of_strings  = [i.text for i in li]
Zwickau answered 2/11, 2018 at 13:45 Comment(3)
No you got me wrong. I have already list of that after some operation i performed using spacy. I want to convert that token into string now. Is there any way for that.?Ambulance
i.text gives the string representation of the token. Isn't that what you need ? covert token to string ??Zwickau
Exactly. converting list of tokens into list of string. Let me try this for my code.Ambulance
A
0

Thanks for the solution and for sharing your knowledge. It works very well to convert a spacy doc/span to a string or list of strings to further use them in string operations.

you can also use this:-

    for i in li:
        print(str(i))
Arbitration answered 20/6, 2022 at 5:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.