| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Fenrir TTY screen reader |
| 5 |
# By Chrys, Storm Dragon, and contributors. |
| 6 |
|
| 7 |
import inspect |
| 8 |
import os |
| 9 |
import re |
| 10 |
import string |
| 11 |
|
| 12 |
from fenrirscreenreader.core import debug |
| 13 |
|
| 14 |
currentdir = os.path.dirname( |
| 15 |
os.path.realpath(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
| 16 |
) |
| 17 |
fenrir_path = os.path.dirname(currentdir) |
| 18 |
|
| 19 |
|
| 20 |
class PunctuationManager: |
| 21 |
def __init__(self): |
| 22 |
pass |
| 23 |
|
| 24 |
def initialize(self, environment): |
| 25 |
self.env = environment |
| 26 |
self.allPunctNone = dict.fromkeys( |
| 27 |
map(ord, string.punctuation + "§ "), " " |
| 28 |
) |
| 29 |
# replace with None: |
| 30 |
# dot, comma, grave, apostrophe |
| 31 |
# for char in [ord('`'),ord("'")]: |
| 32 |
# self.allPunctNone[char] = None |
| 33 |
# dont restore the following (for announce correct pause) |
| 34 |
for char in [ |
| 35 |
ord("'"), |
| 36 |
ord("."), |
| 37 |
ord(","), |
| 38 |
ord(";"), |
| 39 |
ord(":"), |
| 40 |
ord("?"), |
| 41 |
ord("!"), |
| 42 |
ord("-"), |
| 43 |
]: |
| 44 |
self.allPunctNone[char] = chr(char) |
| 45 |
|
| 46 |
def shutdown(self): |
| 47 |
pass |
| 48 |
|
| 49 |
def remove_unused(self, text, curr_level=""): |
| 50 |
# dont translate dot and comma because they create a pause |
| 51 |
curr_all_punct_none = self.allPunctNone.copy() |
| 52 |
|
| 53 |
# Check if we should replace undefined punctuation with spaces |
| 54 |
replace_with_space = self.env["runtime"]["SettingsManager"].get_setting_as_bool( |
| 55 |
"general", "replace_undefined_punctuation_with_space" |
| 56 |
) |
| 57 |
|
| 58 |
# If the setting is disabled, use the old behavior (remove completely) |
| 59 |
if not replace_with_space: |
| 60 |
# Create a map that removes undefined punctuation instead of replacing with spaces |
| 61 |
curr_all_punct_none = dict.fromkeys( |
| 62 |
map(ord, string.punctuation + "§ "), None |
| 63 |
) |
| 64 |
# Restore the pause-important characters |
| 65 |
for char in [ |
| 66 |
ord("'"), |
| 67 |
ord("."), |
| 68 |
ord(","), |
| 69 |
ord(";"), |
| 70 |
ord(":"), |
| 71 |
ord("?"), |
| 72 |
ord("!"), |
| 73 |
ord("-"), |
| 74 |
]: |
| 75 |
curr_all_punct_none[char] = chr(char) |
| 76 |
|
| 77 |
# Remove characters that are defined in the current punctuation level |
| 78 |
for char in curr_level: |
| 79 |
try: |
| 80 |
del curr_all_punct_none[ord(char)] |
| 81 |
except Exception as e: |
| 82 |
pass |
| 83 |
|
| 84 |
return text.translate(curr_all_punct_none) |
| 85 |
|
| 86 |
def use_custom_dict(self, text, customDict, seperator=""): |
| 87 |
result_text = str(text) |
| 88 |
if customDict: |
| 89 |
for key, item in customDict.items(): |
| 90 |
try: |
| 91 |
regex_lbl = "REGEX;" |
| 92 |
if key.upper().startswith(regex_lbl) and ( |
| 93 |
len(key) > len(regex_lbl) |
| 94 |
): |
| 95 |
result_text = re.sub( |
| 96 |
str(key[len(regex_lbl) :]), |
| 97 |
seperator + str(item) + seperator, |
| 98 |
result_text, |
| 99 |
) |
| 100 |
else: |
| 101 |
result_text = result_text.replace( |
| 102 |
str(key), seperator + str(item) + seperator |
| 103 |
) |
| 104 |
except Exception as e: |
| 105 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 106 |
"use_custom_dict replace:'" |
| 107 |
+ key |
| 108 |
+ "' with '" |
| 109 |
+ item |
| 110 |
+ "' failed:" |
| 111 |
+ str(e), |
| 112 |
debug.DebugLevel.ERROR, |
| 113 |
on_any_level=False, |
| 114 |
) |
| 115 |
return result_text |
| 116 |
|
| 117 |
def use_punctuation_dict(self, text, punctuationDict, punctuation): |
| 118 |
result_text = str(text) |
| 119 |
|
| 120 |
if punctuationDict and punctuation and punctuation != "": |
| 121 |
if " " in punctuation: |
| 122 |
result_text = result_text.replace( |
| 123 |
" ", " " + punctuationDict[" "] + " " |
| 124 |
) |
| 125 |
for key, item in punctuationDict.items(): |
| 126 |
if ( |
| 127 |
punctuation != "" and key in punctuation |
| 128 |
) and key not in " ": |
| 129 |
if ( |
| 130 |
self.env["runtime"]["SettingsManager"].get_setting( |
| 131 |
"general", "respect_punctuation_pause" |
| 132 |
) |
| 133 |
and len(key) == 1 |
| 134 |
and key in "',.;:?!" |
| 135 |
): |
| 136 |
result_text = result_text.replace( |
| 137 |
str(key), " " + str(item) + str(key) + " " |
| 138 |
) |
| 139 |
else: |
| 140 |
result_text = result_text.replace( |
| 141 |
str(key), " " + str(item) + " " |
| 142 |
) |
| 143 |
return result_text |
| 144 |
|
| 145 |
def is_puctuation(self, char): |
| 146 |
return char in self.env["punctuation"]["PUNCTDICT"] |
| 147 |
|
| 148 |
def proceed_punctuation(self, text, ignore_punctuation=False): |
| 149 |
if ignore_punctuation: |
| 150 |
return text |
| 151 |
result_text = text |
| 152 |
result_text = self.use_custom_dict( |
| 153 |
result_text, self.env["punctuation"]["CUSTOMDICT"] |
| 154 |
) |
| 155 |
if self.env["runtime"]["SettingsManager"].get_setting_as_bool( |
| 156 |
"general", "emoticons" |
| 157 |
): |
| 158 |
result_text = self.use_custom_dict( |
| 159 |
result_text, self.env["punctuation"]["EMOTICONDICT"], " " |
| 160 |
) |
| 161 |
curr_punct_level = "" |
| 162 |
if ( |
| 163 |
self.env["runtime"]["SettingsManager"] |
| 164 |
.get_setting("general", "punctuation_level") |
| 165 |
.lower() |
| 166 |
in self.env["punctuation"]["LEVELDICT"] |
| 167 |
): |
| 168 |
curr_punct_level = self.env["punctuation"]["LEVELDICT"][ |
| 169 |
self.env["runtime"]["SettingsManager"] |
| 170 |
.get_setting("general", "punctuation_level") |
| 171 |
.lower() |
| 172 |
] |
| 173 |
else: |
| 174 |
curr_punct_level = string.punctuation + " §" |
| 175 |
result_text = self.use_punctuation_dict( |
| 176 |
result_text, self.env["punctuation"]["PUNCTDICT"], curr_punct_level |
| 177 |
) |
| 178 |
result_text = self.remove_unused(result_text, curr_punct_level) |
| 179 |
return result_text |
| 180 |
|
| 181 |
def cycle_punctuation(self): |
| 182 |
punct_list = list(self.env["punctuation"]["LEVELDICT"].keys()) |
| 183 |
try: |
| 184 |
curr_index = punct_list.index( |
| 185 |
self.env["runtime"]["SettingsManager"] |
| 186 |
.get_setting("general", "punctuation_level") |
| 187 |
.lower() |
| 188 |
) # curr punctuation |
| 189 |
except Exception as e: |
| 190 |
return False |
| 191 |
curr_index += 1 |
| 192 |
if curr_index >= len(punct_list): |
| 193 |
curr_index = 0 |
| 194 |
curr_level = punct_list[curr_index] |
| 195 |
self.env["runtime"]["SettingsManager"].set_setting( |
| 196 |
"general", "punctuation_level", curr_level.lower() |
| 197 |
) |
| 198 |
return True |
| 199 |
|
| 200 |
def load_dicts( |
| 201 |
self, |
| 202 |
dictConfigPath=fenrir_path + "/../../config/punctuation/default.conf", |
| 203 |
): |
| 204 |
dict_config = open(dictConfigPath, "r") |
| 205 |
curr_dict_name = "" |
| 206 |
while True: |
| 207 |
line = dict_config.readline() |
| 208 |
if not line: |
| 209 |
break |
| 210 |
line = line.replace("\n", "") |
| 211 |
if line.replace(" ", "") == "": |
| 212 |
continue |
| 213 |
if line.replace(" ", "").startswith("#"): |
| 214 |
if not line.replace(" ", "").startswith("#:===:"): |
| 215 |
continue |
| 216 |
if line.replace(" ", "").upper().startswith("[") and line.replace( |
| 217 |
" ", "" |
| 218 |
).upper().endswith("DICT]"): |
| 219 |
curr_dict_name = line[ |
| 220 |
line.find("[") + 1 : line.upper().find("DICT]") + 4 |
| 221 |
].upper() |
| 222 |
else: |
| 223 |
if curr_dict_name == "": |
| 224 |
continue |
| 225 |
if ":===:" not in line: |
| 226 |
continue |
| 227 |
sep_line = line.split(":===:") |
| 228 |
if len(sep_line) == 1: |
| 229 |
sep_line.append("") |
| 230 |
elif len(sep_line) < 1: |
| 231 |
continue |
| 232 |
elif len(sep_line) > 2: |
| 233 |
sep_line[1] = ":===:" |
| 234 |
self.env["punctuation"][curr_dict_name][sep_line[0]] = ( |
| 235 |
sep_line[1] |
| 236 |
) |
| 237 |
self.env["runtime"]["DebugManager"].write_debug_out( |
| 238 |
"Punctuation: " |
| 239 |
+ curr_dict_name |
| 240 |
+ "." |
| 241 |
+ str(sep_line[0]) |
| 242 |
+ " :" |
| 243 |
+ sep_line[1], |
| 244 |
debug.DebugLevel.INFO, |
| 245 |
on_any_level=True, |
| 246 |
) |
| 247 |
dict_config.close() |