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

问题描述 投票:1回答:2

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

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

#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文件的转录准确性,并考虑不同的语音到文本服务如何转录的所有不同方式。

python data-science string-comparison speech-to-text
2个回答
0
投票

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

例如,如果four是规范形式,那么编写代码用1替换one,用213替换two hundred and thirteen,等等,并与这些进行比较。

实际上,我认为最好将4标准化为four而不是4,因为在某些语言中可以使用多种方式表达数字。通过优选from nltk.tokenize import word_tokenize from nltk.corpus import wordnet ,可以将所有等效转录标准化为单一形式。


0
投票

谢谢@Michael Veksler。我现在尝试使用NLTK库来更有效地将字符串拆分为单词列表。此外,我试图寻找每个单词的同义词,并比较同义词是否匹配。这仍然没有真正解决任务,所以我想知道我还能尝试什么。

我使用这两个库:

s1_set = word_tokenize(list1)

拆分单词就像:

for i in range(0, (len(s1_set)-1)):
    #Find synonym of word in s1_set index i
    t1 = wordnet.synsets(s1_set[i])
    #Ensure t1 isn't empty
    if t1:
        wl1.append(t1[0].lemmas()[0].name())

现在我尝试找到单词的同义词并采取第一个找到的同义词。我将它附加到名为“wl1”的空列表中。如果找到任何同义词,我之前检查,因为情况并非总是这样。

qazxswpoi

然后我再次逐字逐句比较,就像我上面的第一篇文章。这个方法对我的问题也不是一个令人满意的解决方案。谁能想到更好的方法?

© www.soinside.com 2019 - 2024. All rights reserved.