-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecode_language.py
executable file
·126 lines (93 loc) · 3.32 KB
/
recode_language.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
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
#!/usr/bin/env python3
import argparse
import re
import sys
import traceback
from icu import LocaleData
from langdetect import detect
from _utils import find_decodings, get_artisttitle, get_lyrics
HELP = """
Try to find the correct encoding for a given ultrastar text file. Tries to
determine which language a song is written in, get the alphabet for that
language and find the encoding, that produces the fewest non-alphabet
characters. Does not work well for multi-language songs.
Changes the file in place.
"""
non_ascii = re.compile("[^a-zA-Z0-9\"',. !?~\n\r*: #&_()\\[\\]-]")
def guess_lyric_language(text, remove_non_ascii=True):
lyrics = get_lyrics(text)
if remove_non_ascii:
ascii_lyrics = ""
for line in lyrics:
if line in ascii_lyrics:
continue
if not non_ascii.search(line):
ascii_lyrics += line + " "
if not ascii_lyrics:
raise Exception("no ascii-only lyrics found")
ascii_lyrics = ascii_lyrics.replace("~", " ")
lyrics = ascii_lyrics
else:
lyrics = " ".join(lyrics)
return detect(lyrics)
def get_anti_alphabet(language):
data = LocaleData(language)
alphabet = data.getExemplarSet()
return re.compile(
"[^" + "".join(alphabet) + "0-9\"',. !?&~\n\r*: #_()\\[\\]…-]", re.IGNORECASE
)
def guess_encoding(content, anti_alphabet, verbose=False):
best = None
best_count = len(content) * 2
for encoding, text in find_decodings(content):
lyrics = " ".join(get_lyrics(text))
metadata = get_artisttitle(text)
non_alphabet_chars = set(
anti_alphabet.findall(lyrics) + anti_alphabet.findall(metadata)
)
non_alphabet_count = len(non_alphabet_chars)
if verbose:
print(encoding, non_alphabet_chars, lyrics)
if non_alphabet_count < best_count:
best = encoding
best_count = non_alphabet_count
if not best:
raise Exception("could not find encoding")
return best
def fix_encoding(path, dry_run=False, verbose=False):
with open(path, "rb") as f:
content = f.read()
try:
text = next(find_decodings(content))[1]
except StopIteration:
print(f"ERROR\tcoult not find encoding\t{path}")
return
try:
language = guess_lyric_language(text)
anti_alphabet = get_anti_alphabet(language)
encoding = guess_encoding(content, anti_alphabet, verbose)
content = content.decode(encoding)
if encoding not in ("ascii", "utf_8") and not dry_run:
with open(path, "w") as f:
f.write(content)
except Exception as ex:
print(f"ERROR\t{ex}\t{path}")
raise
print(f"SUCCESS\t{language}/{encoding}\t{path}")
def main(argv):
parser = argparse.ArgumentParser(description=HELP)
parser.add_argument("files", nargs="+")
parser.add_argument(
"--dry-run",
action="store_true",
help="just find the encoding, do not change the file.",
)
parser.add_argument("--verbose", action="store_true")
args = parser.parse_args(argv)
for path in args.files:
try:
fix_encoding(path, args.dry_run, args.verbose)
except Exception as ex:
traceback.print_exc()
if __name__ == "__main__":
main(sys.argv[1:])