Python 在sklearn中将一个句子映射到它的词汇表

Python 在sklearn中将一个句子映射到它的词汇表,python,python-3.x,python-2.7,machine-learning,scikit-learn,Python,Python 3.x,Python 2.7,Machine Learning,Scikit Learn,我正在使用CountVectorizer获取字符串列表中的单词列表 from sklearn.feature_extraction.text import CountVectorizer raw_text = [ 'The dog hates the black cat', 'The black dog is good' ] raw_text = [x.lower() for x in raw_text] vocabulary = vectorizer.vocabulary_

我正在使用CountVectorizer获取字符串列表中的单词列表

from sklearn.feature_extraction.text import CountVectorizer
raw_text = [
    'The dog hates the black cat',
    'The black dog is good'
]
raw_text = [x.lower() for x in raw_text]
vocabulary = vectorizer.vocabulary_ 
vocabulary = dict((v, k) for k, v in vocabulary.iteritems())
vocabulary
在词汇表中,我有以下数据,它们是正确的

{0: u'black', 1: u'cat', 2: u'dog', 3: u'good', 4: u'hates', 5: u'is', 6: u'the'}
我现在想得到的是映射到这些新值的原始语句,例如:

expected_output = [
    [6, 2, 4, 6, 0, 1],
    [6, 0, 2, 5, 3]
]
我试着浏览Sklearn文档,但我找不到任何能做到这一点的东西,我甚至不知道我要执行的操作的正确术语,所以我在谷歌上找不到任何结果


有什么方法可以达到这个效果吗?

您可以尝试以下方法吗:

mydict = {0: u'black', 1: u'cat', 2: u'dog',
          3: u'good', 4: u'hates', 5: u'is', 6: u'the'}


def get_val_key(val):
    return list(mydict.keys())[list(mydict.values()).index(val.lower())]


raw_text = [
    'The dog hates the black cat',
    'The black dog is good'
]
expected_output = [list(map(get_val_key, text.split())) for text in raw_text]
print(expected_output)
输出:

[[6, 2, 4, 6, 0, 1], [6, 0, 2, 5, 3]]

你能试一下吗

mydict = {0: u'black', 1: u'cat', 2: u'dog',
          3: u'good', 4: u'hates', 5: u'is', 6: u'the'}


def get_val_key(val):
    return list(mydict.keys())[list(mydict.values()).index(val.lower())]


raw_text = [
    'The dog hates the black cat',
    'The black dog is good'
]
expected_output = [list(map(get_val_key, text.split())) for text in raw_text]
print(expected_output)
输出:

[[6, 2, 4, 6, 0, 1], [6, 0, 2, 5, 3]]

按如下方式查找每个单词:

from sklearn.feature_extraction.text import CountVectorizer
raw_text = [
    'The dog hates the black cat',
    'The black dog is good'
]

cv = CountVectorizer()
cv.fit_transform(raw_text)


vocab = cv.vocabulary_.copy()

def lookup_key(string):
    s = string.lower()
    return [vocab[w] for w in s.split()]

list(map(lookup_key, raw_text))
输出:


按如下方式查找每个单词:

from sklearn.feature_extraction.text import CountVectorizer
raw_text = [
    'The dog hates the black cat',
    'The black dog is good'
]

cv = CountVectorizer()
cv.fit_transform(raw_text)


vocab = cv.vocabulary_.copy()

def lookup_key(string):
    s = string.lower()
    return [vocab[w] for w in s.split()]

list(map(lookup_key, raw_text))
输出:


我认为您可以将文本调整为构建词汇表,然后使用build_analyzer使用词汇表创建所需的映射

输出:

[[6, 2, 4, 6, 0, 1], [6, 0, 2, 5, 3]]
[6,2,4,6,0,1],[6,0,2,5,3]]


我认为您可以将文本调整为构建词汇表,然后使用build_analyzer使用词汇表创建所需的映射

输出:

[[6, 2, 4, 6, 0, 1], [6, 0, 2, 5, 3]]
[6,2,4,6,0,1],[6,0,2,5,3]]


与这里另一个答案的解决方案相比,这个解决方案通常对大容量文本具有更好的运行时间。拆分不考虑停止字或其他预留,当构建vocabulary@AI_Learning说得好。对于一个OP的用例来说,对于一个被删除的单词来说,使用一个-1就足够了。与这里的另一个答案相比,这个解决方案通常对于大批量的文本来说有更好的运行时间。在构建vocabulary@AI_Learning说得好。也许对任何被删除的停止字使用-1对于OP的用例来说就足够了。虽然有效,但是由于使用了值列表,时间复杂度是二次的。这里的另一个答案提供了更好的典型运行时,通过反转dict.Works,但由于使用了值列表,因此具有二次时间复杂性。这里的另一个答案提供了更好的典型运行时,方法是反转dict。