为了账号安全,请及时绑定邮箱和手机立即绑定

在 python 中使用 sklearn 计算 TF-IDF 用于变量 n-gram

在 python 中使用 sklearn 计算 TF-IDF 用于变量 n-gram

呼如林 2022-06-22 18:13:04
问题:使用 scikit-learn 查找特定词汇的可变 n-gram 的命中数。解释。我从这里得到了例子。想象一下,我有一个语料库,我想找出有多少命中(计数)具有如下词汇:myvocabulary = [(window=4, words=['tin', 'tan']),                (window=3, words=['electrical', 'car'])                (window=3, words=['elephant','banana'])我在这里所说的窗口是单词可以出现的单词跨度的长度。如下:'tin tan' 被击中(4 个字以内)'tin dog tan' 被击中(4 个字以内)'tin dog cat tan被击中(4个字以内)'tin car sun eclipse tan' 没有被击中。tin 和 tan 相距超过 4 个单词。我只想计算 (window=4, words=['tin', 'tan']) 出现在文本中的次数,所有其他的都相同,然后将结果添加到 pandas 以计算tf-idf 算法。我只能找到这样的东西:from sklearn.feature_extraction.text import TfidfVectorizertfidf = TfidfVectorizer(vocabulary = myvocabulary, stop_words = 'english')tfs = tfidf.fit_transform(corpus.values())其中词汇表是一个简单的字符串列表,可以是单个单词或多个单词。除了来自 scikitlearn:class sklearn.feature_extraction.text.CountVectorizerngram_range : tuple (min_n, max_n)要提取的不同 n-gram 的 n 值范围的下边界和上边界。将使用所有满足 min_n <= n <= max_n 的 n 值。也无济于事。有任何想法吗?谢谢。
查看完整描述

1 回答

?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

我不确定这是否可以使用CountVectorizeror来完成TfidfVectorizer。我为此编写了自己的函数,如下所示:


import pandas as pd

import numpy as np

import string 


def contained_within_window(token, word1, word2, threshold):

  word1 = word1.lower()

  word2 = word2.lower()

  token = token.translate(str.maketrans('', '', string.punctuation)).lower()

  if (word1 in token) and word2 in (token):

      word_list = token.split(" ")

      word1_index = [i for i, x in enumerate(word_list) if x == word1]

      word2_index = [i for i, x in enumerate(word_list) if x == word2]

      count = 0

      for i in word1_index:

        for j in word2_index:

          if np.abs(i-j) <= threshold:

            count=count+1

      return count

  return 0

样本:


corpus = [

    'This is the first document. And this is what I want',

    'This document is the second document.',

    'And this is the third one.',

    'Is this the first document?',

    'I like coding in sklearn',

    'This is a very good question'

]


df = pd.DataFrame(corpus, columns=["Test"])

你的df会看起来像这样:


    Test

0   This is the first document. And this is what I...

1   This document is the second document.

2   And this is the third one.

3   Is this the first document?

4   I like coding in sklearn

5   This is a very good question

现在你可以申请contained_within_window如下:


sum(df.Test.apply(lambda x: contained_within_window(x,word1="this", word2="document",threshold=2)))

你得到:


2

您可以运行一个for循环来检查不同的实例。你这个来构建你的 pandasdf并应用TfIdf它,这是直截了当的。


希望这可以帮助!


查看完整回答
反对 回复 2022-06-22
  • 1 回答
  • 0 关注
  • 198 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号