-
Notifications
You must be signed in to change notification settings - Fork 17
/
utility.py
executable file
·167 lines (131 loc) · 4.85 KB
/
utility.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
Greynir: Natural language processing for Icelandic
Copyright (C) 2023 Miðeind ehf.
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 http://www.gnu.org/licenses/.
Utility functions used in various places in the codebase.
"""
from typing import Any, Dict, List, Optional
import json
import string
import logging
from functools import lru_cache
from pathlib import Path
# Path which points to the root folder of Greynir
GREYNIR_ROOT_DIR: Path = Path(__file__).parent.resolve()
# Other useful paths
CONFIG_DIR = GREYNIR_ROOT_DIR / "config"
RESOURCES_DIR = GREYNIR_ROOT_DIR / "resources"
STATIC_DIR = GREYNIR_ROOT_DIR / "static"
TTS_AUDIO_DIR = STATIC_DIR / "audio" / "tmp"
QUERIES_DIR = GREYNIR_ROOT_DIR / "queries"
QUERIES_GRAMMAR_DIR = QUERIES_DIR / "grammars"
QUERIES_JS_DIR = QUERIES_DIR / "js"
QUERIES_RESOURCES_DIR = QUERIES_DIR / "resources"
QUERIES_UTIL_DIR = QUERIES_DIR / "util"
QUERIES_UTIL_GRAMMAR_DIR = QUERIES_UTIL_DIR / "grammars"
QUERIES_DIALOGUE_DIR = QUERIES_DIR / "dialogues"
@lru_cache(maxsize=32)
def read_txt_api_key(key_name: str, *, folder: Optional[Path] = None) -> str:
"""
Read the given key from a text file in resources directory. Cached.
Optionally provide a different path to the folder containing the key file.
"""
folder = folder or RESOURCES_DIR
p = folder / f"{key_name}.txt"
try:
return p.read_text().strip()
except FileNotFoundError:
logging.warning(f"API key file {p} not found in {folder}")
return ""
@lru_cache(maxsize=32)
def read_json_api_key(
key_name: str, *, folder: Optional[Path] = None
) -> Dict[str, Any]:
"""
Read the given key from a JSON file in resources directory. Cached.
Optionally provide a different path to the folder containing the key file.
"""
folder = folder or RESOURCES_DIR
p = folder / f"{key_name}.json"
try:
return json.loads(p.read_text())
except FileNotFoundError:
logging.warning(f"API key file {p} not found in {folder}")
return {}
def modules_in_dir(p: Path) -> List[str]:
"""
Find the import names of all python modules
in a given directory given a path to a folder
(can be relative to GREYNIR_ROOT_PATH or absolute).
"""
p = p.resolve() # Fully resolve path before working with it
assert (
p.exists() and p.is_dir()
), f"Directory {str(p)} not found when searching for modules"
# Return list of python files in
# import-like format ('.' instead of '/', no '.py')
return [
".".join(pyfile.relative_to(GREYNIR_ROOT_DIR).with_suffix("").parts)
for pyfile in p.glob("*.py")
if not pyfile.name.startswith("_")
]
def sanitize_filename(fn: str, maxlen: int = 60) -> str:
"""Sanitize a potential filename string by limiting allowed characters."""
assert maxlen >= 1, "maxlen must be positive"
ALLOWED_FILE_CHARS = string.ascii_letters + string.digits + "._-"
# Replace whitespace with underscore
fn = "_".join([t for t in fn.lower().split()])
# Rm non-ASCII chars, non-filename chars and limit length
fn = "".join(c for c in icelandic_asciify(fn) if c in ALLOWED_FILE_CHARS)[
:maxlen
].rstrip("._")
return fn
def icelandic_asciify(text: str) -> str:
"""Convert Icelandic characters to their ASCII equivalent
and then remove all non-ASCII characters."""
if not text:
return text
ICECHARS_TO_ASCII = {
"ð": "d",
"Ð": "D",
"á": "a",
"Á": "A",
"ú": "u",
"Ú": "U",
"í": "i",
"Í": "I",
"é": "e",
"É": "E",
"þ": "th",
"Þ": "TH",
"ó": "o",
"Ó": "O",
"ý": "y",
"Ý": "Y",
"ö": "o",
"Ö": "O",
"æ": "ae",
"Æ": "AE",
}
# Substitute all Icelandic chars for their ASCII equivalents
t = text
for k, v in ICECHARS_TO_ASCII.items():
t = t.replace(k, v)
# Remove any remaining non-ASCII chars
t = t.encode("ascii", "ignore").decode()
return t
def icequote(s: str) -> str:
"""Return string surrounded by Icelandic-style quotation marks."""
return f"„{s.strip()}“"
def cap_first(s: str) -> str:
"""Capitalize first character in a string."""
return s[0].upper() + s[1:] if s else s