38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
#
|
|
# Copyright 2016-2099 Ailemon.net
|
|
#
|
|
# This file is part of ASRT Speech Recognition Tool.
|
|
#
|
|
# ASRT is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
# ASRT is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with ASRT. If not, see <https://www.gnu.org/licenses/>.
|
|
# ============================================================================
|
|
|
|
'''
|
|
一些通用函数
|
|
'''
|
|
|
|
import difflib
|
|
|
|
def GetEditDistance(str1, str2):
|
|
leven_cost = 0
|
|
s = difflib.SequenceMatcher(None, str1, str2)
|
|
for tag, i1, i2, j1, j2 in s.get_opcodes():
|
|
#print('{:7} a[{}: {}] --> b[{}: {}] {} --> {}'.format(tag, i1, i2, j1, j2, str1[i1: i2], str2[j1: j2]))
|
|
if tag == 'replace':
|
|
leven_cost += max(i2-i1, j2-j1)
|
|
elif tag == 'insert':
|
|
leven_cost += (j2-j1)
|
|
elif tag == 'delete':
|
|
leven_cost += (i2-i1)
|
|
return leven_cost |