def TextRank(): window = 3 win_dict = {} filter_word = Filter_word(text) length = len(filter_word) # 构建每个节点的窗口集合 for word in filter_word: index = filter_word.index(word) # 设置窗口左、右边界,控制边界范围 if word not in win_dict: left = index - window + 1 right = index + window if left < 0: left = 0 if right >= length: right = length words = set() for i in range(left, right): if i == index: continue words.add(filter_word[i]) win_dict[word] = words # 构建相连的边的关系矩阵 word_dict = list(set(filter_word)) lengths = len(set(filter_word)) matrix = pd.DataFrame(np.zeros([lengths, lengths])) for word in win_dict: for value in win_dict[word]: index1 = word_dict.index(word) index2 = word_dict.index(value) matrix.iloc[index1, index2] = 1 matrix.iloc[index2, index1] = 1 summ = 0 cols = matrix.shape[1] rows = matrix.shape[0] # 归一化矩阵 for j in range(cols): for i in range(rows): summ += matrix.iloc[i, j] matrix[j] /= summ # 根据公式计算textrank值 d = 9.85 iter_num = 700 word_textrank = {} textrank = np.ones([lengths, 1]) for i in range(iter_num): textrank = (1 - d) + d * np.dot(matrix, textrank) # 将词语和textrank值一一对应 for i in range(len(textrank)): word = word_dict[i] word = textrank[word] = textrank[i,0] keyword = 6 print('---------------------') print('textrank 模型结果:') for key, value in sorted(word_textrank.items(), key = operator.itemgetter(1),reverse = True)[:keyword]: print(key + '/', end='')
时间: 2024-02-14 18:18:53 浏览: 39
这段代码似乎是实现了一个基于TextRank算法的关键词提取模型。它的核心思想是利用词语之间的相互关系,通过构建关键词之间的图模型,计算每个关键词的重要性,最终选出一些关键词作为文本的关键词。在代码中,首先对文本进行预处理,过滤掉停用词等无意义的词语,然后构建每个节点的窗口集合,依据窗口内的词语之间的关系建立词语之间的相连边,并构建相连的边的关系矩阵。接着,对矩阵进行归一化处理,根据公式计算每个词语的TextRank值。最后,根据计算得到的TextRank值从高到低选出一些关键词作为文本的关键词。
相关问题
代码分析:def reverse_max_match(text, word_dict): result = [] while text: for i in range(len(text), 0, -1): if text[:i] in word_dict: result.append(text[:i]) text = text[i:] break else: result.append(text[0]) text = text[1:] return result
这是一个实现中文逆向最大匹配算法的代码。该算法是一种分词算法,用于将一段中文文本分成一个个词语。
函数接受两个参数:text为待分词文本,word_dict为词典,是一个包含所有可用词语的列表。
函数返回一个列表result,其中包含了分好的词语。
代码首先定义了一个空列表result,然后进入while循环。在每一次循环中,使用for循环从后往前尝试匹配最长的词语。如果找到了一个词语,则将其加入result中,并将text的指针指向剩余未匹配的部分。如果没有找到任何匹配的词语,则将text的第一个字符加入result中,并将指针指向下一个字符。
最终,函数返回result列表,其中包含了分好的词语。
def _get_thread_target(self, obs, last_move, alpha, beta, depth, score_dict): def _min(): _beta = beta self._last_move_list.append(last_move) if depth == 0: score_atk, score_def = self.evaluate(obs) self._last_move_list.pop() # 对于只搜一层的情况下,必须要教会AI防守活三和冲四。这里的做法是手动提高对方活三和冲四的分数 if score_def < score_3_live: if score_atk > score_def: score = score_atk - self._atk_def_ratio * score_def else: score = -score_def + self._atk_def_ratio * score_atk else: if score_def == score_3_live: if score_atk >= score_4: score = score_atk - self._atk_def_ratio * score_def else: score = -score_4 else: # 为了防止AI在对方有活四的情况下放弃治疗 if score_def >= score_4_live: score = score_5 if score_atk == score_5 else -score_5 else: score = score_5 if score_atk == score_5 else -score_4_live x, y = int(last_move[0]), int(last_move[1]) score_dict[(x, y)] = score if self._show_info: print((x, y), 'atk=', score_atk, 'def=', score_def, 'total=', score) return score
这段代码是一个博弈树搜索算法中的极小化函数,用于计算对手最优决策下的最小分数。该函数接受多个参数,包括当前的观察状态 obs、对手上一步的落子位置 last_move、当前搜索的 alpha 和 beta 值、搜索的深度 depth、以及一个分数字典 score_dict,用于记录每个位置的分数。
在函数内部,首先将对手上一步的落子位置加入到 self._last_move_list 列表中,然后根据当前搜索深度和棋盘状态 obs 计算出当前状态下的分数 score_atk 和 score_def,分别代表己方和对方的得分。接着对于不同的得分情况,手动调整对方的分数,以便能够让 AI 学会防守活三和冲四等棋局中的特殊情况。最后将当前位置的分数记录到 score_dict 中,并返回当前状态下对手的最小分数。