stream language detector

This commit is contained in:
Jonas Weinz 2018-05-22 20:17:24 +02:00
parent d5d1c34aa4
commit 8c4b167057
2 changed files with 20 additions and 1 deletions

View File

@ -0,0 +1,19 @@
#!/usr/bin/env python3
from langdetect import detect
import fileinput as fi
import sys
# just a little script to detect languages of lines starting
# with the json keyword 'text' and writing the language as json value to stdout.
# other lines are just passing through so that this script can be used in a shell pipeline
for line in fi.input():
s = str.split(line.lstrip(' '), ":")
if s[0] == '"text"':
try:
sys.stdout.write(' "lang": "' + detect(s[1]) + '"\n')
except Exception:
sys.stdout.write(' "lang": "NaN"\n')
sys.stdout.write(line)