Some features for recommend, a browse feature
This commit is contained in:
@@ -103,6 +103,11 @@ def recommend(ctx, text, paths, interactive, skip_ucs):
|
|||||||
catlist = [x.catid for x in inference_ctx.catlist]
|
catlist = [x.catid for x in inference_ctx.catlist]
|
||||||
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
|
_, ext = os.path.splitext(path)
|
||||||
|
|
||||||
|
if ext not in (".wav", ".flac"):
|
||||||
|
continue
|
||||||
|
|
||||||
basename = os.path.basename(path)
|
basename = os.path.basename(path)
|
||||||
if skip_ucs and parse_ucs(basename, catlist):
|
if skip_ucs and parse_ucs(basename, catlist):
|
||||||
continue
|
continue
|
||||||
|
@@ -1,10 +1,13 @@
|
|||||||
# recommend.py
|
# recommend.py
|
||||||
|
|
||||||
from re import match
|
from re import match
|
||||||
|
|
||||||
from .inference import InferenceContext
|
from .inference import InferenceContext
|
||||||
|
|
||||||
|
from tabulate import tabulate
|
||||||
|
|
||||||
def print_recommendation(path: str | None, text: str, ctx: InferenceContext,
|
def print_recommendation(path: str | None, text: str, ctx: InferenceContext,
|
||||||
interactive_rename: bool):
|
interactive_rename: bool, recommend_limit=10):
|
||||||
"""
|
"""
|
||||||
Print recommendations interactively.
|
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.
|
`print_recommendation` should be called again with this argument.
|
||||||
- if retval[2] is a str, this is the catid the user has selected.
|
- 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("----------")
|
print("----------")
|
||||||
if path:
|
if path:
|
||||||
print(f"Path: {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})")
|
print(f"- {i}: {r} ({cat}-{subcat})")
|
||||||
|
|
||||||
if interactive_rename and path is not None:
|
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):
|
if m := match(r'^([0-9]+)', response):
|
||||||
selection = int(m.group(1))
|
selection = int(m.group(1))
|
||||||
@@ -43,16 +46,27 @@ def print_recommendation(path: str | None, text: str, ctx: InferenceContext,
|
|||||||
text = m.group(1)
|
text = m.group(1)
|
||||||
return True, text, None
|
return True, text, None
|
||||||
|
|
||||||
elif m := match(r'^c (.*)', response):
|
elif m := match(r'^c (.+)', response):
|
||||||
return True, None, m.group(1)
|
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("?"):
|
elif response.startswith("?"):
|
||||||
print("""
|
print("""
|
||||||
Choices:
|
Choices:
|
||||||
- Enter recommendation number to rename file,
|
- 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
|
- "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
|
- "?" for this message
|
||||||
- "q" to quit
|
- "q" to quit
|
||||||
- or any other key to skip this file and continue to next file
|
- or any other key to skip this file and continue to next file
|
||||||
@@ -60,6 +74,8 @@ Choices:
|
|||||||
return True, text, None
|
return True, text, None
|
||||||
elif response.startswith('q'):
|
elif response.startswith('q'):
|
||||||
return (False, None, None)
|
return (False, None, None)
|
||||||
|
else:
|
||||||
|
print()
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user