plint

French poetry validator (local mirror of https://gitlab.com/a3nm/plint)
git clone https://a3nm.net/git/plint/
Log | Files | Refs | README

diaeresis.py (1699B)


      1 #!/usr/bin/python3
      2 
      3 """Get the number of syllables of a vowel cluster with context"""
      4 
      5 import os
      6 import json
      7 import sys
      8 
      9 
     10 class DiaeresisFinder(object):
     11 
     12     def __init__(self, diaeresis_file="data/diaeresis.json"):
     13         self._trie = None
     14         self._diaeresis_file = diaeresis_file
     15         try:
     16             self._load_diaeresis()
     17         except json.JSONDecodeError:
     18             pass  # cannot read the file, we assume that another file will be loaded later
     19 
     20     def _load_diaeresis(self):
     21         with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), self._diaeresis_file)) as f:
     22             self._trie = json.load(f)
     23 
     24     def do_lookup_sub(self, trie, key):
     25         if len(key) == 0 or (key[0] not in trie[1].keys()):
     26             return trie[0]
     27         return self.do_lookup_sub(trie[1][key[0]], key[1:])
     28 
     29     def lookup(self, key):
     30         return self.do_lookup(key + ['-', '-'])
     31 
     32     def wrap_lookup(self, line_read):
     33         result = self.lookup(line_read)
     34         print("%s: %s" % (line_read, result))
     35 
     36     def do_lookup(self, key):
     37         return self.do_lookup_sub(self._trie, key)
     38 
     39 
     40 diaeresis_finder = DiaeresisFinder()
     41 
     42 
     43 def set_diaeresis(diaeresis_file):
     44     global diaeresis_finder
     45     diaeresis_finder = DiaeresisFinder(diaeresis_file=diaeresis_file)
     46 
     47 
     48 if __name__ == '__main__':
     49     if len(sys.argv) > 1:
     50         diaeresis_finder = DiaeresisFinder(sys.argv[1])
     51 
     52     if len(sys.argv) > 2:
     53         for arg in sys.argv[2:]:
     54             diaeresis_finder.wrap_lookup(arg)
     55     else:
     56         while True:
     57             line = sys.stdin.readline()
     58             if not line:
     59                 break
     60             diaeresis_finder.wrap_lookup(line.lower().lstrip().rstrip().split())