Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nlp 如何使用spaCy获取令牌ID(我想将一个文本语句映射到整数序列)_Nlp_Spacy_Word Embedding - Fatal编程技术网

Nlp 如何使用spaCy获取令牌ID(我想将一个文本语句映射到整数序列)

Nlp 如何使用spaCy获取令牌ID(我想将一个文本语句映射到整数序列),nlp,spacy,word-embedding,Nlp,Spacy,Word Embedding,我想使用spacy对句子进行标记,以获得一系列整数标记ID,用于下游任务。我希望使用它的东西如下。请填写? import spacy # Load English tokenizer, tagger, parser, NER and word vectors nlp = spacy.load('en_core_web_lg') # Process whole documents text = (u"When Sebastian Thrun started working on self-dr

我想使用spacy对句子进行标记,以获得一系列整数标记ID,用于下游任务。我希望使用它的东西如下。请填写

import spacy

# Load English tokenizer, tagger, parser, NER and word vectors
nlp = spacy.load('en_core_web_lg')

# Process whole documents
text = (u"When Sebastian Thrun started working on self-driving cars at ")

doc = nlp(text)

idxs = ??????

print(idxs)
# Want output to be something like;
>> array([ 8045, 70727, 24304, 96127, 44091, 37596, 24524, 35224, 36253])
优选地,整数指的是
en\u core\u web\u lg
中的一些特殊嵌入id

spacy.io/usage/vectors-similarity没有给出在文档中查找哪个属性的提示


我问这个问题,但它被确定为OT。谷歌搜索/描述这个问题的适当术语也很有帮助。

Spacy使用文本哈希来获得唯一的ID。所有
Token
对象对于
文档中给定
Token
的不同用例都有多种形式

如果只需要
标记的标准化形式
s,则使用
.norm
属性,该属性是文本的整数表示形式(散列)

您还可以使用其他属性,例如小写整数属性
.lower
或许多其他属性。使用
文档
令牌
上的
help()

>>> help(doc[0])
Help on Token object:

class Token(builtins.object)
 |  An individual token – i.e. a word, punctuation symbol, whitespace,
 |  etc.
 |  
...
解决方案

import spacy
nlp = spacy.load('en_core_web_md')
text = (u"When Sebastian Thrun started working on self-driving cars at ")

doc = nlp(text)

ids = []
for token in doc:
    if token.has_vector:
        id = nlp.vocab.vectors.key2row[token.norm]
    else:
        id = None
    ids.append(id)

print([token for token in doc])
print(ids)
#>> [When, Sebastian, Thrun, started, working, on, self, -, driving, cars, at]
#>> [71, 19994, None, 369, 422, 19, 587, 32, 1169, 1153, 41]
打破这一点

# A Vocabulary for which __getitem__ can take a chunk of text and returns a hash
nlp.vocab 
# >>  <spacy.vocab.Vocab at 0x12bcdce48>
nlp.vocab['hello'].norm # hash
# >> 5983625672228268878


# The tensor holding the word-vector
nlp.vocab.vectors.data.shape
# >> (20000, 300)

# A dict mapping hash -> row in this array
nlp.vocab.vectors.key2row
# >> {12646065887601541794: 0,
# >>  2593208677638477497: 1,
# >>  ...}

# So to get int id of 'earth'; 
i = nlp.vocab.vectors.key2row[nlp.vocab['earth'].norm]
nlp.vocab.vectors.data[i]

# Note that tokens have hashes but may not have vector
# (Hence no entry in .key2row)
nlp.vocab['Thrun'].has_vector
# >> False
#一种词汇表,uu getitem_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
nlp.vocab
# >>  
nlp.vocab['hello'].norm#hash
# >> 5983625672228268878
#表示向量的张量
nlp.vocab.vectors.data.shape
# >> (20000, 300)
#此数组中的dict映射哈希->行
nlp.vocab.vectors.key2row
# >> {12646065887601541794: 0,
# >>  2593208677638477497: 1,
# >>  ...}
#所以要得到“地球”的int-id;
i=nlp.vocab.vectors.key2row[nlp.vocab['earth'].norm]
nlp.vocab.vectors.data[i]
#注意,标记有散列,但可能没有向量
#(因此,.key2row中没有条目)
nlp.vocab['Thrun']具有向量
#>>错

使用任意
字符串
int
散列函数。然后执行
{word:hash(word)for words in words}
以构建所需的映射。不需要spaCy。如果你需要充实的话,请告诉我。我想你是从这里得到的。可能有很多理由不自己写。我想要相同的管道将文本块映射到空间词向量。例如,原始标记器是否将许多不同的块映射到同一个标记(例如通过词干或小写)?我不知道。vocab中有一百万个单词,我敢打赌它还有一个高度优化的哈希函数。无论如何,我认为有很多理由依赖API而不是围绕它工作。谢谢你,这对我有帮助。我的理解来自手套嵌入,其中1m令牌表示为[1E6384]张量,因此如果我知道将文本映射到令牌到整数[0,1e6-1]的哈希函数,我可以安全地将嵌入加载到张量中,并使用PyTorch、TensorFlow等对其进行重新训练。我希望spaCy能够提供此哈希函数(管道)。是的,现在我找到了,请看下面的答案。我很高兴你有了答案,但我不清楚为什么索引int比散列int更有好处?这是因为您希望将UNK标记表示为
None
类型吗?基本上,我希望spaCy处理有关单词到单词向量映射的所有事情。当我有这个索引时,我可以将它用于torch.nn.EmbeddingBag来调整(预训练的)嵌入。很多单词可以映射到一个散列(如果我理解正确的话),反过来很多散列可以映射到一个单词向量(这是我唯一关心的索引)。哦,是的,这很有意义
# A Vocabulary for which __getitem__ can take a chunk of text and returns a hash
nlp.vocab 
# >>  <spacy.vocab.Vocab at 0x12bcdce48>
nlp.vocab['hello'].norm # hash
# >> 5983625672228268878


# The tensor holding the word-vector
nlp.vocab.vectors.data.shape
# >> (20000, 300)

# A dict mapping hash -> row in this array
nlp.vocab.vectors.key2row
# >> {12646065887601541794: 0,
# >>  2593208677638477497: 1,
# >>  ...}

# So to get int id of 'earth'; 
i = nlp.vocab.vectors.key2row[nlp.vocab['earth'].norm]
nlp.vocab.vectors.data[i]

# Note that tokens have hashes but may not have vector
# (Hence no entry in .key2row)
nlp.vocab['Thrun'].has_vector
# >> False