【发布时间】:2017-12-10 06:44:05
【问题描述】:
参考我的this 问题,我意识到语料库太大,需要在进行 levenshtein 计算之前分成多个迷你列表。以下代码是我的简单尝试,但我想知道是否有更优雅的方法:
import csv#, StringIO
import itertools, Levenshtein
# open the newline-separated list of words
path = '/Users/path/'
file = path + 'wordlist.txt'
output1 = path + 'ortho1.csv'
output2 = path + 'ortho2.csv'
output3 = path + 'ortho3.csv'
output4 = path + 'ortho4.csv'
output5 = path + 'ortho5.csv'
output6 = path + 'ortho6.csv'
words = sorted(set(s.strip() for s in open(file)))
# words is a list with 16349, so I split it in to 6 mini lists
verbs1 = words[:3269]
verbs2 = words[3269:13080]
verbs3 = words[13081:9811]
verbs4 = words[9812:6542]
verbs5 = words[6543:3273]
verbs6 = words[3274:len(words)]
对于上面的每个列表,我计算以下循环:
with open(output1, 'wb') as f:
writer = csv.writer(f, delimiter=",", lineterminator="\n")
for a, b in itertools.product(verbs1, words):
if (a < b and Levenshtein.distance(a,b) <= 5):
writer.writerow([a, b, Levenshtein.distance(a,b)])
再次,一切正常,但我想知道有一种更优雅的方法可以为每个迷你列表编写一个循环。
【问题讨论】:
-
words[13081:9811]你试过这个吗?这不只是一个空列表,因为“to”索引低于“from”索引吗?此外,您可能应该使用列表而不是 6 个单独的变量。 -
您可以将动词拆分为列表字典,您的第一个代码部分很有趣,其余部分很好。
-
另外,如果您为所有人制作产品,我看不出拆分列表对您有什么帮助。
a*x + b*x + c*x与(a+b+c) * x相同。 -
@tobias_k:你是对的!我弄乱了索引。我的问题并不清楚完全清楚 - excel 无法打开单个文件,所以我试图将单个 csv 拆分为多个 csv。但我似乎根本没有成功——有什么提示吗?
-
如果您只对距离