-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyoficator.py
More file actions
executable file
·114 lines (100 loc) · 3.95 KB
/
yoficator.py
File metadata and controls
executable file
·114 lines (100 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import codecs
import os, sys
import pprint
import regex as re
#-------------------------------------------------------------------------#
#
# ▗▀▖▗ ▐
# ▌ ▌▞▀▖▐ ▄ ▞▀▖▝▀▖▜▀ ▞▀▖▙▀▖ ▛▀▖▌ ▌
# ▚▄▌▌ ▌▜▀ ▐ ▌ ▖▞▀▌▐ ▖▌ ▌▌ ▗▖▙▄▘▚▄▌
# ▗▄▘▝▀ ▐ ▀▘▝▀ ▝▀▘ ▀ ▝▀ ▘ ▝▘▌ ▗▄▘
#
# Description:
# This is a Russian text yoficator (ёфикатор).
#
# It conservatively replaces every "е" to "ё" when it's unambiguously
# a case of the latter. No context is used; it relies entirely on a lack
# of dictionary entries for a correspondent "truly е" homograph.
#
# Yoficating Russian texts remove some unnecessary ambiguity.
# https://en.wikipedia.org/wiki/Yoficator
# https://ru.wikipedia.org/wiki/Ёфикатор
#
# Syntax: yoficator.py [text-file-in-Russian | string-in-Russian]
#
# Depends on yoficator.dic, which is used for the lookup.
#
# Limitations:
# * The code being conservative and not looking for context, it won't correct
# when a "truly е" homograph exists. Thus a "все" will never be corrected,
# because both все and всё exist as different words.
# * Prone to wrongly yoficate other Cyrillic-based languages, such as
# Bulgarian, Ukrainian, Belarussian.
# * It's not the fastest thing in the world, mind you. But does the job.
#
#-------------------------------------------------------------------------
#
# Found this useful? Appalling? Appealing? Please let me know.
# The Unabashed welcomes your impressions.
#
# You will find the
# unabashed
# at the location opposite to
# moc • thgimliam
#
#-------------------------------------------------------------------------
#
# License:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
#--------------------------------------------------------------------------#
# TODO Better handle lowercase, uppercase
pp = pprint.PrettyPrinter(4)
# Variables initialization; tests a file if no argument is supplied.
# Save the yoficator as a subfolder of your Desktop
# TODO: Make it compatible with other OSs.
workingDir = os.getenv('HOME') + "/Desktop/yoficator/"
textFile = workingDir + "tests/yoficator.txt"
dictionaryFile = workingDir + "yoficator.dic"
if len(sys.argv) > 1:
# Is the input a filename?
if os.path.isfile(sys.argv[1]):
text = codecs.open(sys.argv[1].decode("utf-8"), "r", "utf-8").read()
# Else we will assume it's a string
else:
text = sys.argv[1].decode("utf-8")
else:
# We will assume using textFile as input filename above
text = codecs.open(textFile, "r", "utf-8").read()
dictionary = {}
# Splitter / tokenizer
splitter = re.compile(r'(\s+|\w+|\W+|\S+)', re.UNICODE)
tokens = splitter.findall(text)
with codecs.open(dictionaryFile, "r", "utf-8") as f:
for line in f:
if ":" in line:
key,value = line.split(":")
dictionary[key] = value.rstrip('\n')
else:
pass
for token in tokens:
if token in dictionary:
print(dictionary[token], end='')
else:
print(token, end='')
sys.exit(0)
# -------------------- END -----------------------