Some features for recommend, a browse feature

This commit is contained in:
2025-09-06 14:10:53 -07:00
parent 04332b73ee
commit b4758dd138
2 changed files with 27 additions and 6 deletions

View File

@@ -103,6 +103,11 @@ def recommend(ctx, text, paths, interactive, skip_ucs):
catlist = [x.catid for x in inference_ctx.catlist]
for path in paths:
_, ext = os.path.splitext(path)
if ext not in (".wav", ".flac"):
continue
basename = os.path.basename(path)
if skip_ucs and parse_ucs(basename, catlist):
continue

View File

@@ -1,10 +1,13 @@
# recommend.py
from re import match
from .inference import InferenceContext
from tabulate import tabulate
def print_recommendation(path: str | None, text: str, ctx: InferenceContext,
interactive_rename: bool):
interactive_rename: bool, recommend_limit=10):
"""
Print recommendations interactively.
@@ -16,7 +19,7 @@ def print_recommendation(path: str | None, text: str, ctx: InferenceContext,
`print_recommendation` should be called again with this argument.
- if retval[2] is a str, this is the catid the user has selected.
"""
recs = ctx.classify_text_ranked(text)
recs = ctx.classify_text_ranked(text, limit=recommend_limit)
print("----------")
if path:
print(f"Path: {path}")
@@ -28,7 +31,7 @@ def print_recommendation(path: str | None, text: str, ctx: InferenceContext,
print(f"- {i}: {r} ({cat}-{subcat})")
if interactive_rename and path is not None:
response = input("#, t [text], ?, q > ")
response = input("(n#), t [text], c [cat], ?, q > ")
if m := match(r'^([0-9]+)', response):
selection = int(m.group(1))
@@ -43,16 +46,27 @@ def print_recommendation(path: str | None, text: str, ctx: InferenceContext,
text = m.group(1)
return True, text, None
elif m := match(r'^c (.*)', response):
elif m := match(r'^c (.+)', response):
return True, None, m.group(1)
elif m := match(r'^b (.+)', response):
expt = []
for cat in ctx.catlist:
if cat.catid.startswith(m.group(1)):
expt.append([f"{cat.catid}: ({cat.category}-{cat.subcategory})",
cat.explanations])
print(tabulate(expt, maxcolwidths=80))
return True, text, None
elif response.startswith("?"):
print("""
Choices:
- Enter recommendation number to rename file,
- "t [text]" to search for new recommendations based on [text]
- "t <text>" to search for new recommendations based on <text>
- "p" re-use the last selected cat-id
- "c [cat]" to type in a category by hand
- "c <cat>" to type in a category by hand
- "b <cat>" browse category list for categories starting with <cat>
- "?" for this message
- "q" to quit
- or any other key to skip this file and continue to next file
@@ -60,6 +74,8 @@ Choices:
return True, text, None
elif response.startswith('q'):
return (False, None, None)
else:
print()
else:
return None