haspirater

detect aspirated 'h' in French words (local mirror of https://gitlab.com/a3nm/haspirater)
git clone https://a3nm.net/git/haspirater/
Log | Files | Refs | README

haspirater.py (1219B)


      1 #!/usr/bin/python3
      2 
      3 """Determine if a French word starts by an aspirated 'h' or not, by a
      4 lookup in a precompiled trie"""
      5 
      6 import os
      7 import json
      8 import sys
      9 
     10 f = open(os.path.join(os.path.dirname(
     11   os.path.realpath(__file__)), 'haspirater.json'))
     12 trie = json.load(f)
     13 f.close()
     14 
     15 def do_lookup(trie, key):
     16   if len(key) == 0 or (key[0] not in trie[1].keys()):
     17     return trie[0]
     18   return do_lookup(trie[1][key[0]], key[1:])
     19 
     20 def lookup(key):
     21   """Return True iff key starts with an aspirated 'h'"""
     22   if key == '' or key[0] != 'h':
     23     raise ValueError
     24   return list(map((lambda x: x == "1"), do_lookup(trie, key[1:] + ' ')))
     25 
     26 def wrap_lookup(line):
     27   line = line.lower().lstrip().rstrip()
     28   try:
     29     result = lookup(line)
     30     if True in result and not False in result:
     31       print("%s: aspirated" % line)
     32     elif False in result and not True in result:
     33       print("%s: not aspirated" % line)
     34     else:
     35       print("%s: ambiguous" % line)
     36   except ValueError:
     37     print("%s: no leading 'h'" % line)
     38 
     39 if __name__ == '__main__':
     40   if len(sys.argv) > 1:
     41     for arg in sys.argv[1:]:
     42       wrap_lookup(arg)
     43   else:
     44     while True:
     45       line = sys.stdin.readline()
     46       if not line:
     47         break
     48       wrap_lookup(line)
     49