为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

侃侃无极 2022-01-05 20:14:57
我需要比较两个输出字符串,即原始转录和 Speech-to-Text 服务的转录。数字通常以数字格式或作为单词书写,例如“四”或“4”。考虑到这些不同的转录方法,如何比较字符串?到目前为止,我只是将两个字符串都转换为小写字母,并用空格分隔每个单词。#Read the two files and store them in s1_raw and s2_rawwith 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 letters1 = s1_raw.lower()s2 = s2_raw.lower()#Split texts with space as seperator to have a list of wordss1_set = s1.split(' ')s2_set = s2.split(' ')#Used later for confidence calculationcount1 = len(s1_set)count2 = 0x = 0#Check which string is longer to prevent running out of indicesif 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 += 1else:    #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 wordsconfidence = count2/count1#Print out resultprint('The confidence level of this service is {:.2f}%'.format(confidence*100))我想测量几个 *.txt 文件的转录准确性,并考虑不同 Speech-to-Text 服务转录的所有不同方式。
查看完整描述

2 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞

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

例如,如果four是标准形式,然后写入代码来替换1one213two hundred and thirteen,等等,并且不与这些进行比较。

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


查看完整回答
反对 回复 2022-01-05
?
桃花长相依

TA贡献1860条经验 获得超8个赞

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


我使用这两个库:


from nltk.tokenize import word_tokenize

from nltk.corpus import wordnet

拆分单词很简单:


s1_set = word_tokenize(list1)

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


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())

然后我再次像上面的第一篇文章一样逐字比较。这种方法也不能令人满意地解决我的问题。谁能想到更好的方法?


查看完整回答
反对 回复 2022-01-05
  • 2 回答
  • 0 关注
  • 228 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号