-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_episode.go
576 lines (483 loc) · 17.6 KB
/
parser_episode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package habari
import (
"slices"
"strconv"
"strings"
)
func (p *parser) parseEpisode() {
// Check alt episode number or range
// e.g. 01 (12)
if found := p.parseKnownEpisodeAltNumber(); found {
return // Stop if an episode number is found (even if the alt number is not found)
}
// Search by alt episode number
// We check if any unknown number token is followed by "({number})", e.g. {tkn} (14)
if found := p.parseEpisodeBySearchingForAltNumber(); found {
return // Stop if an episode number with alt number is found
}
// Check combined or separated keywords other than season prefixes
// e.g. Ep1, ED1, ED 1, OVAs 1-3, OVAs 1 ~ 3, OVA1, OVA 1v2
if found := p.parseKeywordsWithEpisodes(); found {
return // Stop if an episode number (e.g. Ep1) is found, not an OVA, ED, OP, ...
}
// e.g. 01 of 24
if found := p.parseEpisodeByRangeSeparator("OF"); found {
return // Stop if an episode number is found
}
// Check last number before the first opening bracket (if there is one at the beginning [subgroup], then, before the second opening bracket)
// e.g. Title - 01
if found := p.parseEpisodeBySearching(false); found {
return // Stop if an episode number is found
}
// e.g. [12]
if found := p.parseEpisodeByEnclosedNumber(); found {
return // Stop if an episode number is found
}
// e.g Title 01
if found := p.parseEpisodeBySearching(true); found {
return // Stop if an episode number is found
}
}
// +---------------------+
// | Alt number |
// +---------------------+
// e.g. 01 (12)
func (p *parser) parseEpisodeBySearchingForAltNumber() bool {
for _, numTkn := range *p.tokenManager.tokens {
if !numTkn.isNumberOrLikeKind() || !numTkn.isUnknown() {
continue // Check next token
}
// Check if token is followed by "({number})", e.g. {tkn} (14)
nextTkns, found, _ := p.tokenManager.tokens.getCategorySequenceAfter(p.tokenManager.tokens.getIndexOf(numTkn), []tokenCategory{
tokenCatOpeningBracket, // (
tokenCatUnknown, // 12
tokenCatClosingBracket, // )
}, true)
if !found {
continue // Check next token
}
// Verify that the sequence is correct
if nextTkns[0].getValue() != "(" || !nextTkns[1].isNumberKind() || nextTkns[2].getValue() != ")" {
continue
}
// Update tokens
numTkn.setMetadataCategory(metadataEpisodeNumber)
nextTkns[1].setMetadataCategory(metadataEpisodeNumberAlt)
return true
}
return false
}
// +---------------------+
// | Alt number |
// +---------------------+
// e.g. {epTkn} (12)
// parseKnownEpisodeAltNumber parses the alt episode number if an episode number already exists.
func (p *parser) parseKnownEpisodeAltNumber() (foundEpisode bool) {
found, tkns := p.tokenManager.tokens.findWithMetadataCategory(metadataEpisodeNumber)
if !found {
return false
}
// We found an episode number anyway
foundEpisode = true
last := tkns[len(tkns)-1]
// Check if token is followed by "({number})", e.g. {tkn} (14)
nextTkns, found, _ := p.tokenManager.tokens.getCategorySequenceAfter(p.tokenManager.tokens.getIndexOf(last), []tokenCategory{
tokenCatOpeningBracket, // (
tokenCatUnknown, // 12
tokenCatClosingBracket, // )
}, true)
if !found {
// Check range after found episode
{
if len(tkns) != 1 {
return
}
// Check range, e.g. 01-03
// making sure that the range has no delimiters
rangeTkns, foundRange := p.tokenManager.tokens.checkNumberRangeAfter(last, false)
if !foundRange {
return
}
rangeTkns[1].setMetadataCategory(metadataEpisodeNumber)
}
return
}
if nextTkns[0].getValue() != "(" || !nextTkns[1].isNumberKind() || nextTkns[2].getValue() != ")" {
return
}
// Update token
nextTkns[1].setMetadataCategory(metadataEpisodeNumberAlt)
return
}
// ---------------------------------------------------------------------------------------------------------------------
// Searching by patterns
// ---------------------------------------------------------------------------------------------------------------------
// parseEpisodeBySearching parses the episode number by searching for different patterns.
func (p *parser) parseEpisodeBySearching(aggressive bool) bool {
// +---------------------+
// | Case 1 |
// +---------------------+
// Check "- 01 [...]" or "- 01 480p"
for {
var openingBracketTkn *token
for _, tkn := range *p.tokenManager.tokens {
// Find the first opening bracket or metadata keyword, whatever comes first
if tkn.isOpeningBracket() || tkn.isKeyword() {
if p.tokenManager.tokens.getIndexOf(tkn) == 0 { // Skip first token if it's an opening bracket
continue
}
openingBracketTkn = tkn
break
}
}
if openingBracketTkn == nil {
break
}
// Get previous token
numTkn, found, _ := p.tokenManager.tokens.getTokenBeforeSD(openingBracketTkn)
if !found {
break
}
// Check if previous token is an unknown number or number-like
if !numTkn.isNumberOrLikeKind() || !numTkn.isUnknown() {
break
}
// Check we find a range before
// e.g. "01-{numTkn} [...]"
// making sure that the range has no delimiters
if rangeTkns, found := p.tokenManager.tokens.checkEpisodeRangeBefore(numTkn); found && isReasonableEpisodeNumber(rangeTkns[1].getValue()) {
// Make sure that there is no number range before the range
// e.g. Avoid this "009-1-02 [...]", where "1-02" is considered as a range
if _, found := p.tokenManager.tokens.checkNumberRangeBefore(rangeTkns[1], false); !found {
rangeTkns[1].setMetadataCategory(metadataEpisodeNumber)
numTkn.setMetadataCategory(metadataEpisodeNumber)
return true // Found episode number, end
}
}
// If we are not searching aggressively, check if there is a dash separator before the number
// e.g. "- 01"
if !aggressive && !p.tokenManager.tokens.foundDashSeparatorBefore(numTkn) {
break
}
// +---------------------+
// | Aggressive |
// +---------------------+
// When searching aggressively
// Check that the number might really be an episode number
// e.g. if {lastNumTkn} < 10, lastNumTkn should be zero padded to avoid false positives like "Title 2"
if aggressive {
if numTkn.isNumberKind() {
intVal, err := strconv.Atoi(numTkn.getValue())
if err != nil {
break
}
if intVal < 10 && !isNumberZeroPadded(numTkn.getValue()) {
break
}
// should be isolated
// avoids Evangelion 1.11 You Can (Not) Redo -> 11 being identified as episode number
if !p.tokenManager.tokens.isIsolated(numTkn) {
break
}
}
// in the case of "Title 2v2", we can safely identify "2v2" as an episode number
}
numTkn.setMetadataCategory(metadataEpisodeNumber)
return true // Found episode number, end
}
// +---------------------+
// | Case 2 |
// +---------------------+
// Check for first occurrence of unknown number preceded and followed by a dash separator
// e.g. "- 01 -"
for _, numTkn := range *p.tokenManager.tokens {
if !numTkn.isUnknown() || !numTkn.isNumberOrLikeKind() {
continue // Check next token
}
// Check dash separator before
if !p.tokenManager.tokens.foundDashSeparatorBefore(numTkn) {
continue // Check next token
}
// Check dash separator after
if !p.tokenManager.tokens.foundDashSeparatorAfter(numTkn) {
continue // Check next token
}
// Check that it is not a range
// e.g. "03-{numTkn} -"
if _, found := p.tokenManager.tokens.checkNumberRangeBefore(numTkn, false); found {
continue // Check next token
}
// Check that it is not a range
// e.g. "- {numTkn}-03"
if _, found := p.tokenManager.tokens.checkNumberRangeAfter(numTkn, false); found {
continue // Check next token
}
numTkn.setMetadataCategory(metadataEpisodeNumber)
return true // Found episode number, end
}
// +---------------------+
// | Case 3 |
// +---------------------+
// Check for last unknown number
for {
var lastNumTkn *token
var count int
// Get the last unknown number token
for _, tkn := range *p.tokenManager.tokens {
if tkn.isYear() {
continue
}
if tkn.isNumberOrLikeKind() && tkn.isUnknown() {
lastNumTkn = tkn
count++
}
}
if lastNumTkn == nil {
break
}
// Check we find a range before
// e.g. "1 - {lastNumTkn} [...]"
if rangeTkns, found := p.tokenManager.tokens.checkNumberRangeBefore(lastNumTkn, true); found && isReasonableEpisodeNumber(rangeTkns[1].getValue()) {
if isNumberZeroPadded(lastNumTkn.getValue()) && !isNumberZeroPadded(rangeTkns[1].getValue()) {
// Do not consider "01-1" as a range
} else {
rangeTkns[1].setMetadataCategory(metadataEpisodeNumber)
}
lastNumTkn.setMetadataCategory(metadataEpisodeNumber)
return true // Found episode number, end
}
// If we are not searching aggressively, check if there is a dash separator before the number
// e.g. "- 01"
if !aggressive && !p.tokenManager.tokens.foundDashSeparatorBefore(lastNumTkn) {
break
}
// When searching aggressively
// Check that the number might really be an episode number
// e.g. if {lastNumTkn} < 10, lastNumTkn should be zero padded to avoid false positives like "Title 2"
if aggressive {
// If we have more than one number, we can't be sure that it's an episode number
if count > 1 {
break
}
if p.tokenManager.tokens.foundDashSeparatorAfter(lastNumTkn) && !p.tokenManager.tokens.isFirstToken(lastNumTkn) {
break
}
if lastNumTkn.isNumberKind() {
intVal, err := strconv.Atoi(lastNumTkn.getValue())
if err != nil {
break
}
if intVal < 10 && !isNumberZeroPadded(lastNumTkn.getValue()) {
break
}
// should be isolated
if !p.tokenManager.tokens.isIsolated(lastNumTkn) {
break
}
} else if lastNumTkn.isNumberLikeKind() {
if !isReasonableEpisodeNumber(lastNumTkn.getValue()) {
break
}
}
// in the case of "Title 2v2", we can safely identify "2v2" as an episode number
}
lastNumTkn.setMetadataCategory(metadataEpisodeNumber)
return true // Found episode number, end
}
return false
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ---------------------------------------------------------------------------------------------------------------------
// Keywords
// ---------------------------------------------------------------------------------------------------------------------
// parseKeywordsWithEpisodes parses keywords that are combined or separated with a number.
// It does not handle season/volume/part prefixes (keywordCatSeasonPrefix, ...) as those are handled by parseSeason.
//
// It handles the following cases:
// keywordKindCombinedWithNumber
// keywordKindSeparatedWithNumber
//
// e.g. Ep1, ED1, ED 1, OVAs 1-3, OVAs 1 ~ 3, OVA1, OVA 1v2
//
// "foundEpisode" is set to true if an actual episode number is found. (not an OVA, ED, OP, ...)
func (p *parser) parseKeywordsWithEpisodes() (foundEpisode bool) {
for _, tkn := range *p.tokenManager.tokens {
if tkn.isKeyword() || !tkn.isUnknown() { // Don't bother if token is already a keyword
continue // Skip to next token
}
keywords, found := p.tokenManager.keywordManager.findKeywordsBy(func(kw *keyword) bool {
// Get keywords that are combined or separated with a number AND not a season prefix or episode prefix
// e.g. ED1, ED 1
return (kw.isCombinedWithNumber() || kw.isSeparatedWithNumber()) &&
!kw.isSeasonPrefix() && !kw.isVolumePrefix() && !kw.isPartPrefix() && // Skip all these because they are handled in parseSeason()
strings.HasPrefix(tkn.getNormalizedValue(), kw.value) // Token value starts with keyword value
})
if !found {
continue // Skip to next token
}
keywordLoop:
for _, keyword := range keywords {
// e.g. ED1
if keyword.isCombinedWithNumber() {
// Check if prefix is followed by a number or number-like (e.g. 01, 01v2)
remaining := strings.TrimPrefix(tkn.getNormalizedValue(), keyword.value)
if len(remaining) > 0 && isNumberOrLike(remaining) {
// e.g. ED
prefixTkn := newToken(keyword.value)
prefixTkn.setIdentifiedKeywordCategory(keyword.category, keyword.kind)
prefixTkn.setKind(tokenKindWord)
// e.g. 01, 1, 3
numberTkn := newToken(tkn.getValue()[len(keyword.value):])
numberTkn.setMetadataCategory(metadataOtherEpisodeNumber)
if keyword.isEpisodePrefix() {
numberTkn.setMetadataCategory(metadataEpisodeNumber)
}
numberTkn.setKind(tokenKindNumberLike)
if isNumber(remaining) {
numberTkn.setKind(tokenKindNumber)
}
//firstNumberIsZeroPadded := isNumberZeroPadded(remaining)
// Overwrite token and insert new tokens
// "ED1" -> "ED", "1"
p.tokenManager.tokens.overwriteAndInsertManyAt(p.tokenManager.tokens.getIndexOf(tkn), []*token{prefixTkn, numberTkn})
foundEpisode = true
if isNumber(remaining) { // e.g. ED1.5, don't bother if ED1v2
p.tokenManager.tokens.checkNumberWithDecimal(numberTkn) // Check if number is decimal
}
// Check range
// e.g. ED1-3, ED01-03, ED1 ~ 3, ED01 ~ 03
if rangeTkns, found := p.tokenManager.tokens.checkNumberRangeAfter(numberTkn, false); found {
// e.g. ED1-3, ED01-03
rangeTkns[1].setMetadataCategory(metadataOtherEpisodeNumber)
if keyword.isEpisodePrefix() {
rangeTkns[1].setMetadataCategory(metadataEpisodeNumber)
}
break keywordLoop // Skip to next token
}
//if nextNumTkn, found, kind := checkNumberRangeAfterToken(p, numberTkn, firstNumberIsZeroPadded); found {
// // e.g. ED1-3, ED01-03
// if kind == 0 {
// nextNumTkn.setMetadataCategory(metadataOtherEpisodeNumber)
// p.tokenManager.tokens.checkNumberWithDecimal(nextNumTkn) // Check if number is decimal
// break keywordLoop // Skip to next token
// }
//}
break keywordLoop // Skip to next token
}
}
// e.g. ED 1
if keyword.isSeparatedWithNumber() {
// Get next token, by skipping delimiters
// Check if next token is a number or number-like
if nextTkn, found, _ := p.tokenManager.tokens.getTokenAfterSD(tkn); found &&
(nextTkn.isNumberOrLikeKind() && nextTkn.isUnknown()) {
tkn.setIdentifiedKeywordCategory(keyword.category, keyword.kind)
nextTkn.setMetadataCategory(metadataOtherEpisodeNumber)
if keyword.isEpisodePrefix() {
foundEpisode = true
nextTkn.setMetadataCategory(metadataEpisodeNumber)
}
// Check range
firstSeasonIsZeroPadded := isNumberZeroPadded(nextTkn.getValue())
// e.g. ED 1-3, ED 01-03, ED 1 ~ 3, ED 01 ~ 03
if nextNumTkn, found, kind := checkNumberRangeAfterToken(p, nextTkn, firstSeasonIsZeroPadded); found {
// e.g. ED 1-3, ED 01-03
if kind == 0 {
nextNumTkn.setMetadataCategory(metadataOtherEpisodeNumber)
if keyword.isEpisodePrefix() {
nextNumTkn.setMetadataCategory(metadataEpisodeNumber)
}
break keywordLoop // Skip to next token
}
}
break keywordLoop // Skip to next token
}
}
}
}
return
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ---------------------------------------------------------------------------------------------------------------------
// Enclosed episode number
// ---------------------------------------------------------------------------------------------------------------------
func (p *parser) parseEpisodeByEnclosedNumber() bool {
//if !p.tokenManager.tokens.allUnknownTokensAreEnclosed() {
// return false
//}
for _, numTkn := range *p.tokenManager.tokens {
if !numTkn.isUnknown() || !numTkn.isNumberOrLikeKind() || !numTkn.isEnclosed() {
continue // Check next token
}
// e.g. [
prevTkn, found := p.tokenManager.tokens.getTokenBefore(numTkn)
if !found || !prevTkn.isOpeningBracket() {
continue
}
// e.g. ]
nextTkn, found := p.tokenManager.tokens.getTokenAfter(numTkn)
if !found || !nextTkn.isClosingBracket() {
continue
}
// e.g. We found [12] or (12)
numTkn.setMetadataCategory(metadataEpisodeNumber)
return true // Found
}
return false
}
// ---------------------------------------------------------------------------------------------------------------------
// Searching by range separator
// ---------------------------------------------------------------------------------------------------------------------
func (p *parser) parseEpisodeByRangeSeparator(value string) bool {
for _, numTkn := range *p.tokenManager.tokens {
if !numTkn.isUnknown() || !numTkn.isNumberOrLikeKind() {
continue // Check next token
}
// e.g. of
ofTkn, found, _ := p.tokenManager.tokens.getTokenAfterSD(numTkn)
if !found || ofTkn.getNormalizedValue() != value {
continue
}
// e.g. [
secondNumTkn, found, _ := p.tokenManager.tokens.getTokenAfterSD(ofTkn)
if !found || !secondNumTkn.isNumberOrLikeKind() {
continue
}
// e.g. We found "01 of 24"
numTkn.setMetadataCategory(metadataEpisodeNumber)
ofTkn.setCategory(tokenCatKnown) // Set category to known to avoid incorrect episode title parsing
secondNumTkn.setMetadataCategory(metadataOtherEpisodeNumber)
return true
}
return false
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// e.g.
// - 01 -> true
// - 1 -> true
// - 1v2 -> true
// - 1.1 -> true
// - 1.11 -> false
func isReasonableEpisodeNumber(s string) bool {
if slices.Contains([]string{"3.0", "1.11"}, s) {
return false
}
if isDigitsOnly(s) {
return true
}
if isNumber(s) {
return true
}
index := strings.IndexByte(s, '.')
if index == -1 {
return true
}
decimalIntVal, err := strconv.Atoi(s[index+1:])
if err == nil {
return false
}
if decimalIntVal > 5 {
return false
}
return true
}