當前位置: 首頁>>代碼示例>>Python>>正文


Python Levenshtein.editops方法代碼示例

本文整理匯總了Python中Levenshtein.editops方法的典型用法代碼示例。如果您正苦於以下問題:Python Levenshtein.editops方法的具體用法?Python Levenshtein.editops怎麽用?Python Levenshtein.editops使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Levenshtein的用法示例。


在下文中一共展示了Levenshtein.editops方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _get_operation_counts

# 需要導入模塊: import Levenshtein [as 別名]
# 或者: from Levenshtein import editops [as 別名]
def _get_operation_counts(
    source_string: str, destination_string: str
) -> Tuple[int, int, int, int]:
    """
    Check how many edit operations (delete, insert, replace) are required to
    transform the source string into the destination string. The number of hits
    can be given by subtracting the number of deletes and substitutions from the
    total length of the source string.

    :param source_string: the source string to transform into the destination string
    :param destination_string: the destination to transform the source string into
    :return: a tuple of #hits, #substitutions, #deletions, #insertions
    """

    editops = Levenshtein.editops(source_string, destination_string)

    substitutions = sum(1 if op[0] == "replace" else 0 for op in editops)
    deletions = sum(1 if op[0] == "delete" else 0 for op in editops)
    insertions = sum(1 if op[0] == "insert" else 0 for op in editops)
    hits = len(source_string) - (substitutions + deletions)

    return hits, substitutions, deletions, insertions 
開發者ID:jitsi,項目名稱:jiwer,代碼行數:24,代碼來源:measures.py

示例2: print_error_analysis

# 需要導入模塊: import Levenshtein [as 別名]
# 或者: from Levenshtein import editops [as 別名]
def print_error_analysis():
    options = config.options(read=True)
    output = get_output(options.run_dir, 'eval')
    errors = [(inst['input'], pred, inst['output'])
              for inst, pred in zip(output.data, output.predictions)
              if inst['output'] != pred]
    if 0 < options.max_examples < len(errors):
        indices = np.random.choice(np.arange(len(errors)), size=options.max_examples, replace=False)
    else:
        indices = range(len(errors))

    if options.html:
        print('<!DOCTYPE html>')
        print('<html><head><title>Error analysis</title><meta charset="utf-8" /></head><body>')
    for i in indices:
        inp, pred, gold = [unicode(s).strip() for s in errors[i]]
        editops = lev.editops(gold, pred)
        print_visualization(inp, pred, gold, editops, html=options.html)
    if options.html:
        print('</body></html>') 
開發者ID:stanfordnlp,項目名稱:stanza-old,代碼行數:22,代碼來源:error_analysis.py

示例3: print_visualization

# 需要導入模塊: import Levenshtein [as 別名]
# 或者: from Levenshtein import editops [as 別名]
def print_visualization(input_seq, pred_output_seq,
                        gold_output_seq, editops, html=False):
    gold_highlights = []
    pred_highlights = []
    for optype, gold_idx, pred_idx in editops:
        gold_highlights.append(gold_idx)
        pred_highlights.append(pred_idx)

    input_seq = highlight(input_seq, pred_highlights, 'cyan', html=html)
    pred_output_seq = highlight(pred_output_seq, pred_highlights, 'red', html=html)
    gold_output_seq = highlight(gold_output_seq, gold_highlights, 'yellow', html=html)

    if html:
        print('<p>')
        br = u' <br />'
    else:
        br = u''
    uprint(input_seq + br)
    uprint(pred_output_seq + br)
    uprint(gold_output_seq)
    if html:
        print('</p>')
    print('') 
開發者ID:stanfordnlp,項目名稱:stanza-old,代碼行數:25,代碼來源:error_analysis.py

示例4: _get_word_errors

# 需要導入模塊: import Levenshtein [as 別名]
# 或者: from Levenshtein import editops [as 別名]
def _get_word_errors(s1, s2):
        b = set(s1.split() + s2.split())
        word2char = dict(zip(b, range(len(b))))
        w1 = [chr(word2char[w]) for w in s1.split()]
        w2 = [chr(word2char[w]) for w in s2.split()]

        ops = Lev.editops(''.join(w1), ''.join(w2))
        errors = {"delete": 0, "insert": 0, "replace": 0}
        for x in ops:
            errors[x[0]] += 1
        return errors 
開發者ID:ryanleary,項目名稱:patter,代碼行數:13,代碼來源:transcription_error.py

示例5: _get_char_errors

# 需要導入模塊: import Levenshtein [as 別名]
# 或者: from Levenshtein import editops [as 別名]
def _get_char_errors(s1, s2):
        s1, s2, = s1.replace(' ', ''), s2.replace(' ', '')
        ops = Lev.editops(s1, s2)
        errors = {"delete": 0, "insert": 0, "replace": 0}
        for x in ops:
            errors[x[0]] += 1
        return errors 
開發者ID:ryanleary,項目名稱:patter,代碼行數:9,代碼來源:transcription_error.py

示例6: main

# 需要導入模塊: import Levenshtein [as 別名]
# 或者: from Levenshtein import editops [as 別名]
def main():

    # a = 'GCCTGAGTCCGAGCAGAAGAAGAAGGGCTCCCATCACATCAAC'
    # b = 'GAGTCGAGCAGAAGAAGAANGG'

    a = 'AATGTGTGTCTGCTGGAAGCTCCTATTCTTCCGCCATTTTCCAGTCCTCCAGAAGTTTCCTGATGGTCCATGTCTGAATTAGACACCCCTCTTCTTTGTTCCAGTTGCACCTGTAATTCTTCAGCATAGTACTTCTTAAACTGTTTTTAA'
    b= 'TTTNCTGATGGTCCATGTCTGTTACTC'

    print(l.distance(a, b))
    print(l.editops(a, b))
    print(l.matching_blocks(l.editops(a,b), a, b)) 
開發者ID:tsailabSJ,項目名稱:circleseq,代碼行數:13,代碼來源:test_align.py

示例7: compute_uer_confusion_matrix

# 需要導入模塊: import Levenshtein [as 別名]
# 或者: from Levenshtein import editops [as 別名]
def compute_uer_confusion_matrix(predictions_dict, labels_dict, unit_dict):

    slim_dict = {key:val for key, val in unit_dict.items() if val not in ['GO', 'EOS', 'MASK', 'END']}
    vocab_size = len(slim_dict)
    invdict = {v: k for k, v in slim_dict.items()}

    conf_matrix = np.zeros(shape=(vocab_size, vocab_size + 2))  # plus deletions, insertions
    edit_ops_indices = []
    edit_ops_at_word_boundaries = []
    edit_ops_not_at_word_boundaries = []

    for (id, label) in labels_dict.items():
        label_str = ''.join(_strip_extra_chars(label))
        prediction_str = ''.join(_strip_extra_chars(predictions_dict[id]))
        edit_ops = Levenshtein.editops(prediction_str, label_str)

        seen_positions = []
        for op in edit_ops:
            opname = op[0]
            if len(prediction_str) >= 40:
                edit_ops_indices.append(op[1] / len(prediction_str))  # store all errors in the source (prediction) string

            if opname == 'delete':
                source_unit = prediction_str[op[1]]
                mat_col = vocab_size
                seen_positions.append(op[1])

                if source_unit == ' ':
                    edit_ops_at_word_boundaries.append(source_unit)
                else:
                    edit_ops_not_at_word_boundaries.append(source_unit)

            elif opname == 'insert':
                source_unit = label_str[op[2]]  # the inserted unit does not exist in the source string
                mat_col = vocab_size + 1
            elif opname == 'replace':
                source_unit = prediction_str[op[1]]
                dest_unit = label_str[op[2]]
                mat_col = invdict[dest_unit] - 1
                seen_positions.append(op[1])

                if source_unit == ' ':
                    edit_ops_at_word_boundaries.append(source_unit)
                else:
                    edit_ops_not_at_word_boundaries.append(source_unit)

            else:
                raise Exception('unknown opname {}'.format(opname))

            mat_row = invdict[source_unit] - 1
            conf_matrix[mat_row, mat_col] += 1


        for idx, symbol in enumerate(prediction_str):
            if idx not in seen_positions:  # correct match
                mat_pos = invdict[symbol] - 1
                conf_matrix[mat_pos, mat_pos] += 1

    # plot_confusion_matrix(conf_matrix, invdict)
    plot_edit_ops_histogram(edit_ops_indices) 
開發者ID:georgesterpu,項目名稱:avsr-tf1,代碼行數:62,代碼來源:analyse.py


注:本文中的Levenshtein.editops方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。