-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.py
32 lines (32 loc) · 1.05 KB
/
merge.py
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
from decimal import Decimal
# One line of lyrics
class Line:
def __init__(self, min: str, sec: str, text: str) -> None:
self.min, self.sec = Decimal(min), Decimal(sec)
self.text = text
def parse(lyric: str) -> object:
try:
timeTag = lyric[:lyric.index("]")]
min, sec = timeTag[timeTag.index("[") + 1:].split(":", 1)
return Line(min, sec, lyric)
except:
return Line("-1", "-1", lyric)
def __str__(self) -> str:
return self.text
def __eq__(self, value: object) -> bool:
return self.text == value.text
def __lt__(self, value: object) -> bool:
return (self.min, self.sec) < (value.min, value.sec)
def merge(*lyrics: str) -> str:
ret = []
# Merge
for lyric in lyrics:
ret.extend([Line.parse(i) for i in lyric.split('\n') if i != ""])
# Sort
ret = sorted(ret)
# Deduplicate
for i in range(len(ret) - 1, -1, -1):
if ret.count(ret[i]) > 1:
ret.pop(i)
# Return
return "\n".join([str(i) for i in ret])