spacy 以字符串形式获取令牌而不是 uint8

问题描述

我想知道是否有办法以字符串的形式而不是格式 uint8 使用 tokenizer(s).to_array("LOWERCASE")

from spacy.lang.en import English 
from spacy.tokenizer import Tokenizer

s = "Lets pray for the people that can be the victim of the possible eruption of Taal Volcano ?? keep safe everyone."

# Create nlp obj
nlp = English()
tokenizer = Tokenizer(nlp.vocab)
 
#Get a list of tokens through list comprehension
tokens = [word.text for word in tokenizer(s)]
#Out > ["Lets","pray","for",...,"everyone"]

# But easier method where you can also can apply Lowercase to the tokens as well by using,tokens = tokenizer(s).to_array("LOWER") 
#Out > array([565864407007422797,10267499103039061205,13330460590412905967],dtype=uint64) 

#But the format you end with results is dtype Unit8

在 spacy 中有没有办法以字符串格式获取它?

它会使删除停用词之类的事情变得更容易

 sp = spacy.load("en_core_web_sm")
 all_stop_words = sp.Defaults.stop_words
 token_without_stopwords = [word for word in tokenizer(s).to_array("LOWER") if word not in all_stopwords]
 # This will ofcourse not work since they are two diffrent sata types from what I understand.

解决方法

由于 Doc.to_array return type,ndarrayto_array 似乎无法获取字符串标记列表:

将给定的令牌属性导出到 numpy ndarray。如果 attr_idsM 属性序列,则输出数组的形状为 (N,M),其中 NDoc 的长度(以标记为单位) .如果 attr_ids 是单个属性,则输出形状将为 (N,)。您可以通过整数 ID(例如 spacy.attrs.LEMMA)或字符串名称(例如“LEMMA”或“lemma”)指定属性。这些值将是 64 位整数。

你可以使用

token_without_stopwords = [word for word in map(lambda x: x.text.lower(),tokenizer(s)) if word not in all_stopwords]

其中 map(lambda x: x.text.lower(),tokenizer(s)) 获取一个地图对象,其中所有标记文本均为小写。

,

你可以这样做。

sp = spacy.load("en_core_web_sm")
all_stop_words = sp.Defaults.stop_words
lower_words = [word.text.lower() for word in sp(s)]
filtered = [word for word in lower_words if word not in all_stopwords]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...