SHARE
TWEET

Untitled

a guest Aug 19th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from pymorphy2 import MorphAnalyzer
  2. from pymorphy2.units.by_analogy import KnownPrefixAnalyzer, UnknownPrefixAnalyzer
  3.  
  4. morph = MorphAnalyzer()
  5.  
  6. def cut_prefix(morph, word):
  7.     analyzers = [
  8.         KnownPrefixAnalyzer(morph),
  9.         UnknownPrefixAnalyzer(morph)]
  10.     seen = set()
  11.     word_lower = word.lower()
  12.     res = []
  13.     for analyzer in analyzers:
  14.         parsed = analyzer.parse(word, word_lower, seen)
  15.         if not parsed:
  16.             continue
  17.         tmp = parsed[0]
  18.         res.extend(tmp)
  19.         if len(tmp) >= 5:
  20.             if tmp[4] and tmp[4][0]:
  21.                 return morph.normal_forms(tmp[4][0][1])[0]
  22.     return res
  23.      
  24. In [126]: cut_prefix(morph, "приукрасила")
  25. Out[126]: 'украсить'
  26.  
  27. In [127]: cut_prefix(morph, "спортлото")
  28. Out[127]: 'лото'
  29.  
  30. In [128]: cut_prefix(morph, "мегаабракадабра")
  31. Out[128]: 'абракадабра'
  32.  
  33. In [129]: cut_prefix(morph, "подловил")
  34. Out[129]: 'ловить'
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top