如何解决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,ndarray
,to_array
似乎无法获取字符串标记列表:
将给定的令牌属性导出到 numpy ndarray
。如果 attr_ids
是 M
属性序列,则输出数组的形状为 (N,M)
,其中 N
是 Doc
的长度(以标记为单位) .如果 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]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。