我有一个词汇表文本文件,其中每行都是一个单词。词汇表中的几个单词如下:
AccountsAndTransactions_/get/v2/accounts/details_DELETE
AccountsAndTransactions_/get/v2/accounts/details_GET
AccountsAndTransactions_/get/v2/accounts/details_POST
AccountsAndTransactions_/get/v2/accounts/{accountId}/transactions_DELETE
AccountsAndTransactions_/get/v2/accounts/{accountId}/transactions_GET
AccountsAndTransactions_/get/v2/accounts/{accountId}/transactions_POST重要提示:AccountsAndTransactions_/get/v2/accounts/details_DELETE这是这个问题中的一个单词。
从文本文件中读取词汇:
with open(Path(VOCAB_FILE), "r") as f:
vocab = f.read().splitlines()生成doc_paths
doc_paths = [f for f in listdir(DOC_DIR) if isfile(join(DOC_DIR, f))]
r = re.compile(".*txt")
doc_paths = list(filter(r.match, doc_paths))
doc_paths = [Path(join(DOC_DIR, i)) for i in doc_paths]我正在文档上运行CountVectorizer。
tf_vectorizer = CountVectorizer(input='filename', lowercase=False, vocabulary=vocab)
tf = tf_vectorizer.fit_transform(doc_paths) # doc_paths is list of pathlib.Path(...) object.
X = tf.toarray() # returns zero matrix问题是X中的所有值都是零。(语料库-文档不为空。)
有人能帮帮我吗?我想要每个文档的词汇表中每个单词的词频。
发布于 2021-08-24 05:35:21
我通过覆盖CountVectorizer的默认analyzer解决了这个问题
def analyzer_custom(doc):
return doc.split()
tf_vectorizer = CountVectorizer(input='filename',
lowercase=False,
vocabulary=vocab,
analyzer=analyzer_custom)感谢@Chris解释了CountVectorizer的内部细节。
https://stackoverflow.com/questions/68901376
复制相似问题