微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?