lou*_*alu 2 gensim word2vec python-3.7
我正在尝试使用 Python 3 中的 word2vec-gensim 从文本文件中的 wiki 标题转储中提取印度尼西亚标题。 wiki 转储还包含其他语言的标题和一些符号。下面是我的代码:
if len(sys.argv) != 3:
namaFileInput = "idwiki-latest-pages-articles.xml.bz2"
namaFileOutput = "wiki.id.case.text"
sys.exit(1)
inp, outp = sys.argv[1:3]
space = " "
i = 0
output = open(namaFileOutput, 'w')
# lower=False: huruf kecil dan besar dibedakan
wiki = WikiCorpus(namaFileInput, lemmatize=False, dictionary={}, lower=False)
for text in wiki.get_texts():
if six.PY3:
output.write(b' '.join(text).encode('utf-8') + '\n')
else:
output.write(space.join(text) + "\n")
i = i + 1
if i % 10000 == 0:
logger.info("Saved " + str(i) + " articles")
output.close()
logger.info("Finished Saved " + str(i) + " articles")
Run Code Online (Sandbox Code Playgroud)
但我得到和错误:
TypeError Traceback (most recent call last)
<ipython-input-17-d4c686a9093a> in <module>
29 for text in wiki.get_texts():
30 if six.PY3:
---> 31 output.write(b' '.join(text).encode('utf-8') + '\n')
32 else:
33 output.write(space.join(text) + "\n")
TypeError: sequence item 0: expected a bytes-like object, str found
Run Code Online (Sandbox Code Playgroud)
我在网上搜索过,但没有成功。任何帮助将不胜感激。
问题出在这行代码中: b' '.join(text)
该变量text
是一个字符串列表,但您试图将它们与一个字节字符串(即b' '
)连接起来,这是行不通的。您可以通过简单地删除来解决这个问题,b
因为您之后无论如何都要使用该.encode('utf-8')
方法将字符串转换为字节:
output.write(' '.join(text).encode('utf-8') + '\n')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8357 次 |
最近记录: |