我试图使用Textacy来计算跨标准语料库的一个单词的TF-以色列国防军得分,但是对于我收到的结果有点不清楚。
我期待有一个浮点数,它代表了语料库中单词的频率。那我为什么要收到一份清单(?)在7个结果中?
"acculer“实际上是一个法语单词,所以期望从英语语料库中得到0的结果。
word = 'acculer'
vectorizer = textacy.Vectorizer(tf_type='linear', apply_idf=True, idf_type='smooth')
tf_idf = vectorizer.fit_transform(word)
logger.info("tf_idf:")
logger.info(tfidf)输出
tf_idf:
(0, 0) 2.386294361119891
(1, 1) 1.9808292530117262
(2, 1) 1.9808292530117262
(3, 5) 2.386294361119891
(4, 3) 2.386294361119891
(5, 2) 2.386294361119891
(6, 4) 2.386294361119891问题的第二部分是我如何提供我自己的语料库在Textacy (尤指Textacy)中的TF-国防军功能。一种不同的语言?
编辑
正如@Vishal所提到的,我使用以下行记录了输出:
logger.info(vectorizer.vocabulary_terms)似乎提供的单词acculer已被拆分为字符。
{'a': 0, 'c': 1, 'u': 5, 'l': 3, 'e': 2, 'r': 4}(1)我如何才能使TF-国防军这个词针对语料库,而不是每个字符?
(2)我如何提供我自己的语料库,并指出它是一个副词?
(3)可以在判刑一级使用TF-国防军吗?这句话相对于语料库的相对频率是多少?
发布于 2019-04-23 11:01:56
基础知识
在研究实际问题之前,让我们先弄清楚定义。
假设我们的语料库包含3个文档(分别为d1、d2和d3 ):
corpus = ["this is a red apple", "this is a green apple", "this is a cat"]术语频率(tf)
tf (字)被定义为一个词在文档中出现的次数。
tf(word, document) = count(word, document) # Number of times word appears in the documenttf是为文档级的单词定义的。
tf('a',d1) = 1 tf('a',d2) = 1 tf('a',d3) = 1
tf('apple',d1) = 1 tf('apple',d2) = 1 tf('apple',d3) = 0
tf('cat',d1) = 0 tf('cat',d2) = 0 tf('cat',d3) = 1
tf('green',d1) = 0 tf('green',d2) = 1 tf('green',d3) = 0
tf('is',d1) = 1 tf('is',d2) = 1 tf('is',d3) = 1
tf('red',d1) = 1 tf('red',d2) = 0 tf('red',d3) = 0
tf('this',d1) = 1 tf('this',d2) = 1 tf('this',d3) = 1使用原始计数存在一个问题,即较长文档中单词的tf值与较短的文档相比具有较高的值。这个问题可以通过规范化原始计数值,除以文档长度(对应文档中的字数)来解决。这被称为l1规范化。文档d1现在可以用tf vector来表示,其中包含语料库中所有单词的所有tf值。另一种规范称为l2,它使文档的tf向量的l2范数等于1。
tf(word, document, normalize='l1') = count(word, document)/|document|
tf(word, document, normalize='l2') = count(word, document)/l2_norm(document)|d1| = 5, |d2| = 5, |d3| = 4
l2_norm(d1) = 0.447, l2_norm(d2) = 0.447, l2_norm(d3) = 0.5, 码: tf
corpus = ["this is a red apple", "this is a green apple", "this is a cat"]
# Convert docs to textacy format
textacy_docs = [textacy.Doc(doc) for doc in corpus]
for norm in [None, 'l1', 'l2']:
# tokenize the documents
tokenized_docs = [
doc.to_terms_list(ngrams=1, named_entities=True, as_strings=True, filter_stops=False, normalize='lower')
for doc in textacy_docs]
# Fit the tf matrix
vectorizer = textacy.Vectorizer(apply_idf=False, norm=norm)
doc_term_matrix = vectorizer.fit_transform(tokenized_docs)
print ("\nVocabulary: ", vectorizer.vocabulary_terms)
print ("TF with {0} normalize".format(norm))
print (doc_term_matrix.toarray())输出:
Vocabulary: {'this': 6, 'is': 4, 'a': 0, 'red': 5, 'apple': 1, 'green': 3, 'cat': 2}
TF with None normalize
[[1 1 0 0 1 1 1]
[1 1 0 1 1 0 1]
[1 0 1 0 1 0 1]]
Vocabulary: {'this': 6, 'is': 4, 'a': 0, 'red': 5, 'apple': 1, 'green': 3, 'cat': 2}
TF with l1 normalize
[[0.2 0.2 0. 0. 0.2 0.2 0.2 ]
[0.2 0.2 0. 0.2 0.2 0. 0.2 ]
[0.25 0. 0.25 0. 0.25 0. 0.25]]
Vocabulary: {'this': 6, 'is': 4, 'a': 0, 'red': 5, 'apple': 1, 'green': 3, 'cat': 2}
TF with l2 normalize
[[0.4472136 0.4472136 0. 0. 0.4472136 0.4472136 0.4472136]
[0.4472136 0.4472136 0. 0.4472136 0.4472136 0. 0.4472136]
[0.5 0. 0.5 0. 0.5 0. 0.5 ]]tf矩阵中的行对应于文档(因此我们的语料库为3行),列对应于词汇表中的每个单词(词汇表中所示单词的索引)。
逆文档频率
有些词传达的信息比另一些少。例如,像,a,an,this这样的词是非常常见的词,它们传递的信息非常少。以国防军衡量这个词的重要性。我们认为,与少数文件中出现的词相比,出现在许多文件中的单词所提供的信息较少。
idf(word, corpus) = log(|corpus| / No:of documents containing word) + 1 # standard idf对于我们直观的idf(apple, corpus) < idf(cat,corpus)语料库
idf('apple', corpus) = log(3/2) + 1 = 1.405
idf('cat', corpus) = log(3/1) + 1 = 2.098
idf('this', corpus) = log(3/3) + 1 = 1.0代码:以色列国防军
textacy_docs = [textacy.Doc(doc) for doc in corpus]
tokenized_docs = [
doc.to_terms_list(ngrams=1, named_entities=True, as_strings=True, filter_stops=False, normalize='lower')
for doc in textacy_docs]
vectorizer = textacy.Vectorizer(apply_idf=False, norm=None)
doc_term_matrix = vectorizer.fit_transform(tokenized_docs)
print ("\nVocabulary: ", vectorizer.vocabulary_terms)
print ("standard idf: ")
print (textacy.vsm.matrix_utils.get_inverse_doc_freqs(doc_term_matrix, type_='standard'))输出:
Vocabulary: {'this': 6, 'is': 4, 'a': 0, 'red': 5, 'apple': 1, 'green': 3, 'cat': 2}
standard idf:
[1. 1.405 2.098 2.098 1. 2.098 1.]术语频率-逆文档频率(Tf):tf是衡量一个词在语料库中的重要程度的一种度量。与其idf加权的tf给我们提供了单词的tf-以色列国防军度量。
tf-idf(word, document, corpus) = tf(word, docuemnt) * idf(word, corpus)tf-idf('apple', 'd1', corpus) = tf('apple', 'd1') * idf('apple', corpus) = 1 * 1.405 = 1.405
tf-idf('cat', 'd3', corpus) = tf('cat', 'd3') * idf('cat', corpus) = 1 * 2.098 = 2.098码:tf-以色列国防军
textacy_docs = [textacy.Doc(doc) for doc in corpus]
tokenized_docs = [
doc.to_terms_list(ngrams=1, named_entities=True, as_strings=True, filter_stops=False, normalize='lower')
for doc in textacy_docs]
print ("\nVocabulary: ", vectorizer.vocabulary_terms)
print ("tf-idf: ")
vectorizer = textacy.Vectorizer(apply_idf=True, norm=None, idf_type='standard')
doc_term_matrix = vectorizer.fit_transform(tokenized_docs)
print (doc_term_matrix.toarray())输出:
Vocabulary: {'this': 6, 'is': 4, 'a': 0, 'red': 5, 'apple': 1, 'green': 3, 'cat': 2}
tf-idf:
[[1. 1.405 0. 0. 1. 2.098 1. ]
[1. 1.405 0. 2.098 1. 0. 1. ]
[1. 0. 2.098 0. 1. 0. 1. ]]现在来问问题:
(1)我如何才能使TF-国防军这个词针对语料库,而不是每个字符?
如上所述,没有独立定义的tf-idf,单词的tf-idf是相对于语料库中的文档而言的。
(2)我如何提供我自己的语料库,并指出它是一个副词?
在上面的样品中显示了这一点。
tf (raw counts): apply_idf=False, norm=Nonetf (l1 normalized): apply_idf=False, norm='l1'tf (l2 normalized): apply_idf=False, norm='l2'tf-idf (standard): apply_idf=True, idf_type='standard'
(3)可以在判刑一级使用TF-国防军吗?这句话相对于语料库的相对频率是多少?
是的,如果并且只有当你把每句话作为一个单独的文件来处理时,你才能做到。在这种情况下,相应文档的tf-idf向量(整行)可以被视为文档的向量表示(在您的情况下是一个句子)。
对于我们的语料库(实际上每个文档只包含一个句子),d1和d2的向量表示应该比向量d1和d3更接近。让我们检查cosin的相似性,看看:
cosine_similarity(doc_term_matrix)输出
array([[1. , 0.53044716, 0.35999211],
[0.53044716, 1. , 0.35999211],
[0.35999211, 0.35999211, 1. ]])您可以看到cosine_similarity( d1,d2) = 0.53和cosine_similarity( d1,d3) = 0.35,因此d1和d2确实比d1和d3更相似(1完全相似,0不相似-正交向量)。
一旦您训练了您的Vectorizer,您就可以将经过训练的对象拾取到磁盘中供以后使用。
结论
一个词的tf在文档级,一个词的idf在语料库层,一个词的tf-idf在文档上。它们非常适合于文档的矢量表示(或当文档由单个句子组成时的句子)。如果您对单词的向量表示感兴趣,那么就探索单词嵌入类(word2vec、快速文本、手套等)。
发布于 2019-04-22 20:32:43
你可以用TF-国防军的词来对付语料库。
docs = ['this is me','this was not that you thought', 'lets test them'] ## create a list of documents
from sklearn.feature_extraction.text import TfidfVectorizer
vec = TfidfVectorizer()
vec.fit(docs) ##fit your documents
print(vec.vocabulary_) #print vocabulary, don't run for 2.5 million documents输出:包含每个单词的in,并在输出中指定一个唯一的索引。
{u'me': 2, u'them': 6, u'that': 5, u'this': 7, u'is': 0, u'thought': 8, u'not': 3, u'lets': 1, u'test': 4, u'you': 10, u'was': 9}
print(vec.idf_) 输出:打印每个词汇表单词的idf值
[ 1.69314718 1.69314718 1.69314718 1.69314718 1.69314718 1.69314718 1.69314718 1.28768207 1.69314718 1.69314718 1.69314718]现在,根据你的问题,假设你想找一个tf-国防军作为一个词,然后你可以得到它:
word = 'thought' #example
index = vec.vocabulary_[word]
>8
print(vec.idf_[index]) #prints idf value
>1.6931471805599454参考资料: 1. 准备文本
现在使用textacy执行相同的操作。
import spacy
nlp = spacy.load('en') ## install it by python -m spacy download en (run as administrator)
doc_strings = [
'this is me','this was not that you thought', 'lets test them'
]
docs = [nlp(string.lower()) for string in doc_strings]
corpus = textacy.Corpus(nlp,docs =docs)
vectorizer = textacy.Vectorizer(tf_type='linear', apply_idf=True, idf_type='smooth')
doc_term_matrix = vectorizer.fit_transform((doc.to_terms_list(ngrams=1, normalize='lower',as_strings=True,filter_stops=False) for doc in corpus))
print(vectorizer.terms_list)
print(doc_term_matrix.toarray())输出
['is', 'lets', 'me', 'not', 'test', 'that', 'them', 'this', 'thought','was', 'you']
[[1.69314718 0. 1.69314718 0. 0. 0.
0. 1.28768207 0. 0. 0. ]
[0. 0. 0. 1.69314718 0. 1.69314718
0. 1.28768207 1.69314718 1.69314718 1.69314718]
[0. 1.69314718 0. 0. 1.69314718 0.
1.69314718 0. 0. 0. 0. ]]参考资料:链接
https://stackoverflow.com/questions/55764766
复制相似问题