gpt4 book ai didi

python - For 循环更新字典值但不影响输出

转载 作者:行者123 更新时间:2023-11-30 21:58:46 25 4
gpt4 key购买 nike

我试图迭代文件中的句子,选择“最佳”句子(这是具有最多稀有双音素(声音)数量的句子),并在选择句子后,更改 的字典值句子中的每个双音素都为 0,这样就不会再次选择双音素(因为我想确保每个可能的双音素都已被选择)。

我已经为此编写了代码,但不明白为什么它不影响输出,因为当我检查在 for 循环开始时选取的字典键之一的值时,它已设置为0.我的代码是:

diphone_frequencies = {...}
diphone_frequencies_original = copy.deepcopy(diphone_frequencies)

line_score = {}
best_utts = []

for i in range(650):
# Open the file and put all its lines in a list. Once.
with open('recipe_diphone_utts.txt') as file:

# Read the file lines one by one.
for line in file:
line = line.rstrip('\r\n')
if line in best_utts:
continue # Skip previously picked sentences.
score = 0.0
# Add a score to the line depending on its content.
for word in line.split():
score += float(diphone_frequencies[word])
line_score[line] = score/len(line.split())

# Sort each lines based on their score and get the best.
best_sentence = max(line_score.keys(), key=(lambda k: line_score[k]))
best_utts.append(best_sentence)
print(best_sentence)

# Each unique word of this iteration's best sentence has its score set to 0.
for item in set(best_sentence.split()):
diphone_frequencies[item] = 0

if all(value == 0 for value in diphone_frequencies.values()):
diphone_frequencies = diphone_frequencies_original

编辑:这个问题已经解决了,但是我现在不能接受我自己的答案;问题是打开文档后出现 for 循环;当我放置时代码有效

for i in range(600):

之前

with open('recipe_diphone_utts.txt') as file:

编辑2:

面临的主要问题已经解决,我已经更改了代码,但是行:

if line in best_utts:
continue

应该确保在重置字典值后不会再次选择同一行的多个实例,但是它会导致同一个句子一遍又一遍地被选为最佳句子,所以我需要其他方法来防止同一句话被多次选择。

最佳答案

由于外部循环,目前 best_utts == [best_sentence] * 600 ,与所有其他句子(行)相比,best_sentence 是得分最高的句子)的文件。

要获得 600 个最佳句子,我会这样做:

diphone_frequencies = {...}
diphone_frequencies_original = copy.deepcopy(diphone_frequencies)

line_score = {}
best_utts = []

# Open the file and put all its lines in a list. Once.
with open('recipe_diphone_utts.txt') as file:
all_lines = file.readlines()

for i in range(600):
print(diphone_frequencies['f_@@r'])

# Read the file lines one by one.
for line in all_lines:
line = line.rstrip()
if line in best_utts:
line_score[line] = 0
continue # Skip previously picked sentences.
score = 0.0
# Add a score to the line depending on its content.
for word in line.split():
score += float(diphone_frequencies[word])
line_score[line] = score/len(line.split())

# Sort each lines based on their score and get the best.
best_sentence = max(line_score.keys(), key=(lambda k: line_score[k]))
best_utts.append(best_sentence)

# Each unique word of this iteration's best sentence has its score set to 0.
for item in set(best_sentence.split()):
diphone_frequencies[item] = 0

if all(value == 0 for value in diphone_frequencies.values()):
diphone_frequencies = diphone_frequencies_original

print(best_utts)

最后也不需要 file.close() 因为您使用的是 with open ... as file 而不是 file = open(. ..)

关于python - For 循环更新字典值但不影响输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54806638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com