gpt4 book ai didi

python - 如何比较不完全匹配的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 21:02:57 25 4
gpt4 key购买 nike

我需要比较两个输出字符串,即原始转录和语音转文本服务的转录。数字通常以数字格式或单词形式书写,例如“四”或“4”。如何考虑这些不同的转录方法来比较字符串?

到目前为止,我只是将两个字符串转换为小写字母,并用空格作为分隔符分隔每个单词。

#Read the two files and store them in s1_raw and s2_raw
with open('original.txt', 'r') as f:
s1_raw = f.read()
with open('comparison.txt', 'r') as f:
s2_raw = f.read()

#Transform all letters to minuscule letter
s1 = s1_raw.lower()
s2 = s2_raw.lower()

#Split texts with space as seperator to have a list of words
s1_set = s1.split(' ')
s2_set = s2.split(' ')

#Used later for confidence calculation
count1 = len(s1_set)
count2 = 0
x = 0

#Check which string is longer to prevent running out of indices
if len(s1_set) < len(s2_set):
#Loop through whole list and compare word by word
for x in range (0, len(s1_set)):
if s1_set[x] == s2_set[x]:
count2 += 1
x += 1
else:
#Loop through whole list and compare word by word
for x in range (0, len(s2_set)):
if s1_set[x] == s2_set[x]:
count2 += 1
x += 1

#Confidence level= correct words divided by total words
confidence = count2/count1

#Print out result
print('The confidence level of this service is {:.2f}%'.format(confidence*100))

我想测量多个 *.txt 文件的转录准确性,并考虑不同语音转文本服务转录的所有不同方式。

最佳答案

在比较文本之前,您必须对其进行标准化。首先确定 four 还是 4 是您的规范形式,并将所有字符串转换为该形式。

例如,如果 four 是规范形式,则编写代码将 1 替换为 one213213,依此类推,并与这些进行比较。

实际上,我认为规范化为 4 而不是 4 更好,因为在某些语言中可能有多种表达数字的方法。通过选择4,可以将所有等效转录标准化为一种单一形式。

关于python - 如何比较不完全匹配的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55612073/

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