使用计算值创建字典
Creating a dictionary with calculated values
我有一个大文本字符串,我想创建一个字典,其中键 = 字符串中的一对单词(必须经过所有可能的组合),值 = 给定单词对的频率.因此,它是一个二维矩阵,每个矩阵元素都是一个数字(来自一列和一行的对的频率相互交叉。词在对中的位置无关紧要:例如,如果 ridebike = 4(频率)然后骑自行车= 4
最终结果是填充矩阵,然后 select N 个顶对。
我刚开始使用文本字符串和 Python 一般情况下,我绝望地迷路了(我的 "code" 中也有太多循环)
这是我的(删除停用词和标点符号后):
textNP = 'stopped traffic bklyn bqe 278 wb manhattan brtillary stx29 wb cadman pla hope oufootball makes safe manhattan kansas tomorrow boomersooner beatwildcats theyhateuscuztheyaintus hatersgonnahate rt bringonthecats bring cats exclusive live footage oklahoma trying get manhattan http colktsoyzvvz rt jonfmorse bring cats exclusive live footage oklahoma trying get manhattan'
一些代码(不完整和错误):
txtU = set(textNP)
lntxt = len(textNP)
lntxtS = len(txtU)
matrixNP = {}
for b1, i1 in txtU:
for b2, i2 in txtU:
if i1< i2:
bb1 = b1+b2
bb2 = b2+b1
freq = 0
for k in textNP:
for j in textNP:
if k < j:
kj = k+j
if kj == bb1 | kj == bb2:
freq +=1
matrixNP[i1][i2] = freq
matrixNP[i2][i1] = freq
elif i1 == i2: matrixNP[i1][i1] = 1
我确定循环很多是错误的问题之一。另外,我不确定如何将计算出的键(单词连接)分配给字典(我想我得到的值是正确的)
文本字符串不是完整的产品:它将使用各种正则表达式清除数字和其他一些内容
非常感谢您的帮助!
您是否正在寻找 2 个单词的所有组合,如果是这样,您可以使用 itertools.combinations
和 collections.Counter
来完成您想要的操作:
>>> from itertools import combinations
>>> from collections import Counter
>>> N = 5
>>> c = Counter(tuple(sorted(a)) for a in combinations(textNP.split(), 2))
>>> c.most_common(N)
[(('manhattan', 'rt'), 8),
(('exclusive', 'manhattan'), 8),
(('footage', 'manhattan'), 8),
(('manhattan', 'oklahoma'), 8),
(('bring', 'manhattan'), 8)]
或者您是在寻找所有成对的连续单词,然后您可以创建成对函数:
>>> from itertools import tee
>>> from collections import Counter
>>> def pairwise(iterable):
... a, b = tee(iterable)
... next(b, None)
... return zip(a, b) # itertools.izip() in python2
>>> N = 5
>>> c = Counter(tuple(sorted(a)) for a in pairwise(textNP.split()))
>>> c.most_common(N)
[(('get', 'manhattan'), 2),
(('footage', 'live'), 2),
(('get', 'trying'), 2),
(('bring', 'cats'), 2),
(('exclusive', 'live'), 2)]
无论哪种方式,我都没有在列表中看到骑自行车。
我有一个大文本字符串,我想创建一个字典,其中键 = 字符串中的一对单词(必须经过所有可能的组合),值 = 给定单词对的频率.因此,它是一个二维矩阵,每个矩阵元素都是一个数字(来自一列和一行的对的频率相互交叉。词在对中的位置无关紧要:例如,如果 ridebike = 4(频率)然后骑自行车= 4
最终结果是填充矩阵,然后 select N 个顶对。
我刚开始使用文本字符串和 Python 一般情况下,我绝望地迷路了(我的 "code" 中也有太多循环)
这是我的(删除停用词和标点符号后):
textNP = 'stopped traffic bklyn bqe 278 wb manhattan brtillary stx29 wb cadman pla hope oufootball makes safe manhattan kansas tomorrow boomersooner beatwildcats theyhateuscuztheyaintus hatersgonnahate rt bringonthecats bring cats exclusive live footage oklahoma trying get manhattan http colktsoyzvvz rt jonfmorse bring cats exclusive live footage oklahoma trying get manhattan'
一些代码(不完整和错误):
txtU = set(textNP)
lntxt = len(textNP)
lntxtS = len(txtU)
matrixNP = {}
for b1, i1 in txtU:
for b2, i2 in txtU:
if i1< i2:
bb1 = b1+b2
bb2 = b2+b1
freq = 0
for k in textNP:
for j in textNP:
if k < j:
kj = k+j
if kj == bb1 | kj == bb2:
freq +=1
matrixNP[i1][i2] = freq
matrixNP[i2][i1] = freq
elif i1 == i2: matrixNP[i1][i1] = 1
我确定循环很多是错误的问题之一。另外,我不确定如何将计算出的键(单词连接)分配给字典(我想我得到的值是正确的)
文本字符串不是完整的产品:它将使用各种正则表达式清除数字和其他一些内容
非常感谢您的帮助!
您是否正在寻找 2 个单词的所有组合,如果是这样,您可以使用 itertools.combinations
和 collections.Counter
来完成您想要的操作:
>>> from itertools import combinations
>>> from collections import Counter
>>> N = 5
>>> c = Counter(tuple(sorted(a)) for a in combinations(textNP.split(), 2))
>>> c.most_common(N)
[(('manhattan', 'rt'), 8),
(('exclusive', 'manhattan'), 8),
(('footage', 'manhattan'), 8),
(('manhattan', 'oklahoma'), 8),
(('bring', 'manhattan'), 8)]
或者您是在寻找所有成对的连续单词,然后您可以创建成对函数:
>>> from itertools import tee
>>> from collections import Counter
>>> def pairwise(iterable):
... a, b = tee(iterable)
... next(b, None)
... return zip(a, b) # itertools.izip() in python2
>>> N = 5
>>> c = Counter(tuple(sorted(a)) for a in pairwise(textNP.split()))
>>> c.most_common(N)
[(('get', 'manhattan'), 2),
(('footage', 'live'), 2),
(('get', 'trying'), 2),
(('bring', 'cats'), 2),
(('exclusive', 'live'), 2)]
无论哪种方式,我都没有在列表中看到骑自行车。