use difflib.get_close_matches
This commit is contained in:
parent
e69a08f1b1
commit
ba3cbbbb4c
1 changed files with 3 additions and 13 deletions
|
@ -1,19 +1,9 @@
|
||||||
from os import path
|
from difflib import get_close_matches
|
||||||
from difflib import SequenceMatcher
|
|
||||||
from jinja2 import Environment, PackageLoader
|
from jinja2 import Environment, PackageLoader
|
||||||
|
|
||||||
|
|
||||||
def template(name, prompt):
|
def template(name, prompt):
|
||||||
best_ratio = 0
|
|
||||||
best_template = ''
|
|
||||||
|
|
||||||
environment = Environment(loader=PackageLoader(__name__, 'templates'))
|
environment = Environment(loader=PackageLoader(__name__, 'templates'))
|
||||||
for template in environment.list_templates():
|
best_templates = get_close_matches(name, environment.list_templates(), n=1, cutoff=0)
|
||||||
base, _ = path.splitext(template)
|
template = environment.get_template(best_templates.pop())
|
||||||
ratio = SequenceMatcher(None, path.basename(name).lower(), base).ratio()
|
|
||||||
if ratio > best_ratio:
|
|
||||||
best_ratio = ratio
|
|
||||||
best_template = template
|
|
||||||
|
|
||||||
template = environment.get_template(best_template)
|
|
||||||
return template.render(prompt=prompt)
|
return template.render(prompt=prompt)
|
||||||
|
|
Loading…
Reference in a new issue