-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_lang.py
34 lines (28 loc) · 1.05 KB
/
remove_lang.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
from langdetect import detect, detect_langs
import langid
import os
import glob
# This module removes foreign languages from our corpus files
# Language detection works best when distinguishing between only 2 languages
# Run this method twice: one to remove English, then to remove Portuguese
def langDetect(string, lang):
langid.set_languages(['es', lang]) # ISO 639-1 language codes
lang, score = langid.classify(string)
return lang
def removeForeign(filename):
with open(filename, "r") as f:
lines = f.readlines()
with open(filename, "w") as f:
for line in lines:
if line == '---------------------------------------------------------------\n':
continue
if langDetect(line, 'en') == 'es' and langDetect(line, 'pt') == 'es':
f.write(line)
else:
print("FOREIGN LANGAUGE DETECTED: " + line)
def main():
path = 'lyrics'
for filename in glob.glob(os.path.join(path, '*.txt')):
removeForeign(filename)
if __name__ == "__main__":
main()