假设我有一个词汇:“你好”、“你好”、“是”、“你”。我有很多文本的语料库,例如:“你好”、“如何”、“如何”。是否有任何有效的方法将此文本编码为整数列表,例如,如果我指定'hello‘= 1,'how’= 2,'are‘= 3,'you’= 4,那么上面的文本将被编码为1,2,2。
我的背景:我必须编码一个大约15万条短信的语料库。词汇量约为20万。一般来说,每一篇课文都包含大约200个单词。
我尝试了下面的代码,但它似乎没有效率。每条短信大约需要2秒,所以我要花8-9个小时才能完成。
tokens_to_index = [[vocabulary.index(word)+1 for word in text] for text in corpus]发布于 2018-12-22 19:06:55
试着用字典代替
vocabulary = dict(zip(vocabulary, range(1, len(vocabulary)+1) )) def tokens_to_index(corpus): return [[vocabulary[word] for word in text] for text in corpus]
发布于 2018-12-22 16:16:25
我不确定,但是试一下字典,你可以用键:值对
https://stackoverflow.com/questions/53897228
复制相似问题