使用pke模块提取python关键词

2024-10-16 22:35:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用https://github.com/boudinfl/pke模块提取关键短语。 当我运行它一次,它是完美的工作。但当我运行它几次时,它会发出以下错误。 ZeroDivisionError:浮点除以零

我的代码如下

extractor = TopicRank()
def key_phrase_extract(path_to_json):
   //get_temp_text.txt from json
   extractor.load_document(input='temp_text.txt', language="en", max_length=10000000,
                        normalization='stemming')
   extractor.candidate_selection(pos={'NOUN', 'PROPN', 'ADJ'},stoplist=stoplist)
   extractor.candidate_weighting(threshold=0.74,
                                  method='average')
   kpe_results = []
   for (keyphrase, score) in extractor.get_n_best(n=10, stemming=True):
      kpe_results.append([keyphrase, score])
   print(kpe_results)

for each_json in json_list()
    key_phrase_extract('each_json')

它完美地运行在第一个json文件中,但在启动第二个json文件时,它给了我 ZeroDivisionError: float division by zero


Tags: keytexttxtjsongetextracttempresults
1条回答
网友
1楼 · 发布于 2024-10-16 22:35:52

我能够解决这个问题。问题是在函数外部初始化提取器

def key_phrase_extract(path_to_json):
   extractor = TopicRank()
   //get_temp_text.txt from json
   extractor.load_document(input='temp_text.txt', language="en", max_length=10000000,
                        normalization='stemming')
   extractor.candidate_selection(pos={'NOUN', 'PROPN', 'ADJ'},stoplist=stoplist)
   extractor.candidate_weighting(threshold=0.74,
                                  method='average')
   kpe_results = []
   for (keyphrase, score) in extractor.get_n_best(n=10, stemming=True):
      kpe_results.append([keyphrase, score])
   print(kpe_results)

for each_json in json_list()
    key_phrase_extract('each_json')

相关问题 更多 >