Ошибка типа при извлечении биграмм с помощью Gensim (Python) - PullRequest
0 голосов
/ 19 февраля 2020

Я хочу извлечь и распечатать биграммы с помощью Gensim. Для этого я использовал этот код в GoogleColab:

import gensim.downloader as api
from gensim.models import Word2Vec
from gensim.corpora import WikiCorpus, Dictionary
from gensim.models import Phrases
from gensim.models.phrases import Phraser
from collections import Counter

data = api.load("text8") # wikipedia corpus
bigram = Phrases(data, min_count=3, threshold=10)


cntr = Counter()
for key in bigram.vocab.keys():
  if len(key.split('_')) > 1:
    cntr[key] += bigram.vocab[key]

for key, counts in cntr.most_common(50):
  print(key, " - ", counts)

Но есть ошибка:

TypeError

Затем я попробовал это:

cntr = Counter()
for key in bigram.vocab.keys():
  if len(key.split(b'_')) > 1:
    cntr[key] += bigram.vocab[key]

for key, counts in cntr.most_common(50):
  print(key, " - ", counts)

А потом:

again

Что не так?

1 Ответ

0 голосов
/ 19 февраля 2020
 bigram_token  = list(bigram.vocab.keys())
 type(bigram_token[0])

 #op
 bytes

конвертируйте это в строку, и это решит проблему, в вашем коде, только при разделении, сделайте

cntr = Counter()
for key in bigram.vocab.keys():
    if len(key.decode('utf-8').split(b'_')) > 1: # here added .decode('utf-8')
       cntr[key] += bigram.vocab[key]
...