python - 文本排名在 0 和 1 之间归一化
问题描述
我想调整此 Text Rank 代码以在我的文本中提取关键字,其值在 0 和 1 之间进行标准化。我展示了一个简短的片段:
# Pare text by spaCy
doc = nlp(text)
# Filter sentences
sentences = self.sentence_segment(doc, candidate_pos, lower) # list of list of words
# Build vocabulary
vocab = self.get_vocab(sentences)
# Get token_pairs from windows
token_pairs = self.get_token_pairs(window_size, sentences)
# Get normalized matrix
g = self.get_matrix(vocab, token_pairs)
# Initionlization for weight(pagerank value)
pr = np.array([1] * len(vocab))
# Iteration
previous_pr = 0
for epoch in range(self.steps):
pr = (1-self.d) + self.d * np.dot(g, pr)
if abs(previous_pr - sum(pr)) < self.min_diff:
break
else:
previous_pr = sum(pr)
# Get weight for each node
node_weight = dict()
for word, index in vocab.items():
node_weight[word] = pr[index]
self.node_weight = node_weight
我看到输出是类似的:
# Output
# science - 1.717603106506989
# fiction - 1.6952610926181002
# filmmaking - 1.4388798751402918
# China - 1.4259793786986021
# Earth - 1.3088154732297723
# tone - 1.1145002295684114
# Chinese - 1.0996896235078055
# Wandering - 1.0071059904601571
# weekend - 1.002449354657688
# America - 0.9976329264870932
# budget - 0.9857269586649321
# North - 0.9711240881032547
我想将Text Rank值标准化为 0 到 1 之间,以获得最大值。
在维基百科上,我在这里 找到了这两个公式
但是如果我添加(1-self.d)/g.shape[0]
到前面的公式中,那么:
pr = (1-self.d)/g.shape[0] + self.d * np.dot(g, pr)
我仍然有一些高于 1 的值。有什么错误?
解决方案
推荐阅读
- java - 在 Android 项目中从 Java 调用 Kotlin 的函数时不能省略默认参数
- python - 为什么这个动态更新的散点图没有做任何事情?
- python - 使用 str.split 函数拆分数据框中的列
- python - Django:如何在模板中使用修改后的查询集而不保存修改
- laravel - Laravel - 如何创建一个覆盖 json()->all() 的新请求
- vue.js - 基于 Vue-Router 语言的路由前缀
- python-3.x - 我如何在多处理中同步列表和 int?
- react-native - 如何在不使用accessibilityLabel的情况下为反应本机应用程序添加唯一ID
- python - Python:使用 datetime.time() 模块时,时间打印为 00:00:00
- css - 如何修复 CSS 设计错误