novy slovnik substantiv
[krypto.git] / ocesavac.py
1 """Modul pro odstranovani diakritiky a zvlastnich znaku."""
2
3 import unicodedata
4 from spolecne import ABECEDA
5
6 def deaccent(unistr):
7     """Vrati text bez akcentu. Pochybna metoda pouzivajici unicodedata."""
8     return ''.join(aChar 
9                    for aChar in unicodedata.normalize('NFD', unistr) 
10                    if not unicodedata.combining(aChar))
11
12 def ocesat(text, mezery=True):
13     """Odstrani z textu akcenty, zvlastni znaky nahradi mezerami, posloupnost
14     mezer jednou mezerou a prevede vsechna pismena na velka."""
15     text = deaccent(unicode(text)).upper()
16     if mezery:
17         pole = [' ']
18         last_white = True
19         for char in text:
20             if char in ABECEDA:
21                 pole.append(char)
22                 last_white = False
23             elif not last_white:
24                 pole.append(' ')
25                 last_white = True
26         if not last_white:
27             pole.append(' ')
28         return ''.join(pole)
29     else: # bez mezer
30         return ''.join([c for c in text if c in ABECEDA])
31
32 def interactive():
33     import sys
34     import codecs
35     sys.stdin = codecs.getreader('utf-8')(sys.stdin)
36     sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
37     for line in sys.stdin.readlines():
38         print " ".join(deaccent(line).lower().split())
39
40 if __name__ == '__main__':
41     interactive()