首页 > 解决方案 > 二元组和三元组

问题描述

我目前正在尝试创建二元组和三元组,以将我的语料库从单词重新制作为单词和短语,并使用此笔记本作为我的参考。但是,我认为应该从代码中产生的短语没有被编译。

这是我正在使用的代码:

unigram_sentences = LineSentence("*.csv")                        

for unigram_sentence in it.islice(unigram_sentences, 1, 5):
print (u' '.join(unigram_sentence))
print (u'')  

intermediate_directory = os.path.join('.../2015/TEMP') 
bigram_model_filepath = os.path.join(intermediate_directory,'bigram_model_all')               


%%time

bigram_model = Phrases(unigram_sentences)
bigram_model.save(bigram_model_filepath)

# load the finished model from disk  

bigram_model = Phrases.load(bigram_model_filepath) 
bigram_sentences_filepath = os.path.join(intermediate_directory,
                                     'bigram_sentences_all.txt')      
%%time                                                              

with codecs.open(bigram_sentences_filepath, 'w', encoding='utf_8') as f:

    for unigram_sentence in unigram_sentences:

        bigram_sentence = u' '.join(bigram_model[unigram_sentence])

        f.write(bigram_sentence + '\n')                 

bigram_sentences = LineSentence(bigram_sentences_filepath)           

for bigram_sentence in it.islice(bigram_sentences, 1, 5):
print (u' '.join(bigram_sentence))
print (u'')

实际上我的 Ins(一元句子)是:

虽然我的出局(二元句)是:

虽然代码确实结合了 bbc_news 和 the_rise 之类的短语,但我在这里真正期望的是看到 mental_health 被组合在一起。

问题:我做错了什么?:/

感谢您的帮助,并为凌乱的第一个计时器帖子道歉!

阿丽娜

标签: pythontopic-modeling

解决方案


推荐阅读