Documentation
¶
Overview ¶
Package subtitlematch matches loose subtitle files to the videos they belong to.
A subtitle not named after its video is invisible to a media manager, which attaches a caption only when the file sits beside the video as <stem>.srt or <stem>.<lang>.srt. Reconnecting the two is not string comparison — the names often share nothing — so the matcher combines studio/JAV codes, normalised title tokens, and the subtitle's own runtime against the scene duration. Runtime is the decisive signal.
Nothing here talks to a media manager, database or filesystem. The input is filenames, durations and subtitle contents; the output is a verdict per candidate pair. Callers supply the library listing and act on the result.
See docs/design.md for why it decides the way it does, and docs/usage.md for the API.
Index ¶
- func Codes(s string) map[string]bool
- func DetectLanguage(r io.Reader) (lang string, confidence float64, ok bool)
- func F1(a, b map[string]bool) float64
- func HMS(d time.Duration) string
- func Overlap(a, b map[string]bool) int
- func Runtime(r io.Reader) (d time.Duration, ok bool)
- func RuntimeFit(subRuntime, sceneDur time.Duration) (score float64, delta time.Duration)
- func StripNoise(stem string) string
- func Tokens(s string) map[string]bool
- type Candidate
- type Index
- type Match
- type SceneRef
- type Subtitle
- type Verdict
- type Vocab
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Codes ¶
Codes extracts studio/JAV identifiers, normalised to ABC-123 with leading zeros removed so "venu00765" and "VENU-765" compare equal. These are the strongest single signal available when present.
func DetectLanguage ¶
DetectLanguage returns an ISO-639-1 code for a subtitle's text, plus a confidence margin over the runner-up. ok is false when no language could be determined with any confidence.
Script is checked first: CJK, Hangul and Cyrillic are unambiguous and do not need word statistics.
func F1 ¶
F1 is the harmonic mean of precision and recall between two token sets. It is symmetric-ish and penalises both missing and extra words, which suits comparing a filename against a title where either may carry more detail.
func Runtime ¶
Runtime returns the timestamp of the last cue — an approximation of the video's length, and the signal that makes matching trustworthy (see docs/design.md).
The whole stream is scanned, not just the tail: cues are not always stored chronologically and a trailing credit line can carry an earlier timestamp. Returns ok=false when nothing parsable is found.
func RuntimeFit ¶
RuntimeFit scores how well a subtitle runtime matches a scene duration.
A subtitle's final cue normally ends slightly BEFORE the video does, so the expected delta (sceneDur - subRuntime) is small and positive. A negative delta means the subtitle outlives the video, which is evidence against the pairing rather than for it.
Returns a score in [0,1] and the signed delta. A zero score means the runtimes are incompatible.
func StripNoise ¶
StripNoise removes scraper and transcoder junk from a filename stem.
Types ¶
type Candidate ¶
type Candidate struct {
Scene SceneRef
Score float64
// Delta is sceneDuration - subtitleRuntime.
Delta time.Duration
// NameSim is the better of title/filename similarity, kept so a verdict
// can require real name agreement and not a runtime-inflated score.
NameSim float64
// Reasons explains the score in human terms, for the report.
Reasons []string
}
Candidate is one scored possibility for a subtitle.
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index holds a preprocessed library for repeated matching.
func NewIndex ¶
NewIndex preprocesses scenes for matching.
vocab may be nil, in which case every token counts as content — matching still works, it just loses the ability to tell "same creator" apart from "same scene".
func (*Index) Match ¶
Match scores a subtitle against the library and returns a verdict with up to maxCandidates ranked possibilities.
Scoring is additive across independent signals, so agreement between two weak signals can outweigh one strong-looking name match:
studio/JAV code +60 (near-decisive when present) exact stem match +100 (the subtitle is already named after the video) title similarity +0..70 (token F1) runtime agreement +0..45, and a contradiction subtracts 30 matching date (±2d) +25, and a disagreement beyond that subtracts 40
type SceneRef ¶
type SceneRef struct {
ID string
Title string
Date string
Studio string
Performers []string
// Path of the scene's primary file, as Stash reports it.
Path string
// Basename stem of that file, without extension.
Stem string
Duration time.Duration
// HasCaption is true when the scene already carries a caption track in
// this subtitle's language; used to avoid proposing a duplicate.
HasCaption bool
// contains filtered or unexported fields
}
SceneRef is the subset of a Stash scene the matcher needs.
type Subtitle ¶
type Subtitle struct {
// Path as reported by the backend that listed it.
Path string
// Stem is the basename without its extension.
Stem string
// Runtime is the last cue time; zero when unknown.
Runtime time.Duration
// Lang is the ISO-639-1 code detected from the file's text.
Lang string
// Size in bytes, used to break ties between duplicate transcripts.
Size int64
// Dirs are the folder names between the scan root and the file. They name
// a creator when the filename does not, and count as creator evidence
// only.
Dirs []string
// Date is a YYYY-MM-DD scene date, optional. Set it when the caller has
// one from metadata (a media manager, a fingerprint DB); when empty the
// matcher falls back to subDate(Stem), which only fires for libraries
// that put dates in filenames — rare. See docs/design.md.
Date string
}
Subtitle is one loose subtitle file to place.
type Verdict ¶
type Verdict string
Verdict expresses how much trust a match deserves.
const ( // Confirmed: name and runtime agree, and no rival comes close. Confirmed Verdict = "CONFIRMED" // Likely: good evidence, but weaker on one axis. Likely Verdict = "LIKELY" // Ambiguous: several candidates are too close to separate safely. Ambiguous Verdict = "AMBIGUOUS" // Unmatched: nothing credible in the library. Unmatched Verdict = "UNMATCHED" )
type Vocab ¶
type Vocab struct {
// contains filtered or unexported fields
}
Vocab names the creators in a library — performers and studios, plus aliases — so their handles can be told apart from title words.
It holds only squashed names (separators removed, as scrapers write them: "riverstone", "thehousenextdoor"), never individual words. Indexing every word of every alias sweeps up ordinary vocabulary and gutted title matching when measured; see docs/design.md.
func (*Vocab) IsCreator ¶
IsCreator reports whether a token names a creator. Scrapers bolt suffixes on ("riverstonexxx"), so a token also counts when it starts with a known handle; trailing digits are ignored for the same reason.