如果想要将实验结果保存到文档中,可以使用Python的文件操作来实现。具体步骤如下:

  1. 在代码中添加文件操作相关的代码,例如:

    with open('实验结果.txt', 'w', encoding='utf-8') as file:
        file.write('TF-IDF模型结果:\n')
        tfidf_extract(filter_list, pos=True, keyword_num=10, file=file)
        file.write('\nTextRank模型结果:\n')
        textrank_extract(text, pos=True, keyword_num=10, file=file)
    
  2. tfidf_extracttextrank_extract函数中,增加一个file参数,并将打印结果的部分修改为写入文件:

    def tfidf_extract(word_list, pos=False, keyword_num=10, file=None):
        doc_list = load_data(pos)
        idf_dic, default_idf = train_idf(doc_list)
        tfidf_model = TfIdf(idf_dic, default_idf, word_list, keyword_num)
        result = tfidf_model.get_tfidf()
        if file:
            file.write('TF-IDF模型关键词提取结果:\n')
            file.write(result + '\n')
        else:
            print('TF-IDF模型关键词提取结果:')
            print(result)
    
    def textrank_extract(text, pos=False, keyword_num=10, file=None):
        textrank = analyse.textrank
        keywords = textrank(text, keyword_num)
        result = '/'.join(keywords)
        if file:
            file.write('TextRank模型关键词提取结果:\n')
            file.write(result + '\n')
        else:
            print('TextRank模型关键词提取结果:')
            print(result)
    

这样,运行代码后就可以将实验结果保存到名为"实验结果.txt"的文档中了。

# -- coding utf-8 --import mathimport numpy as npimport jiebaimport jiebaposseg as psgfrom gensim import corpora modelsfrom jieba import analyseimport functools#步骤2定义停用词表加载函数。停用词表加载方法def get_stopword_

原文地址: https://www.cveoy.top/t/topic/i3yJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录