Skip to content

Instantly share code, notes, and snippets.

@maximtrp
Last active February 10, 2021 18:44
Show Gist options
  • Save maximtrp/def4a054a6a6bf37d6afd878721d9c4b to your computer and use it in GitHub Desktop.
Save maximtrp/def4a054a6a6bf37d6afd878721d9c4b to your computer and use it in GitHub Desktop.
MyStem Simple Wrapper Functions
import subprocess as sp
import json
def get_lemma(x):
lemma = x['analysis'][0].get('lex') if x['analysis'] else x.get('text')
return lemma
def mylem(x):
p = sp.Popen(['mystem', '-l', '--format', 'json'], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT)
stdout = p.communicate(input=bytes(x, encoding='utf-8'))
stdout = json.loads(stdout[0].decode('utf-8'))
return list(filter(None, map(get_lemma, stdout)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment