Provides Jaccard n-gram similarity and Levenshtein distance between strings.
# Source: https://github.com/crazyyanchao/llmcompiler | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
# Provided via justclaw.it.com; keep this notice with the code.
# -*- coding: utf-8 -*-
"""
@Author : Yc-Ma
@Desc : LLMCompiler
@Time : 2024-08-02 09:30:49
"""
def jaccard_sim_ngram(answer, ref, ngram=2):
if answer in ref: return 1.0
if len(answer) - ngram > 0:
answer_ngrams = [answer[i: i + ngram] for i in range(0, len(answer) - ngram + 1)]
else:
answer_ngrams = answer # 退化到unigram
if len(ref) - ngram > 0:
ref_ngrams = [ref[i: i + ngram] for i in range(0, len(ref) - ngram + 1)]
else:
ref_ngrams = ref # 退化到unigram
intersection = len(set(answer_ngrams).intersection(set(ref_ngrams
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key