-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFF_manualoverride.py
137 lines (117 loc) · 5.63 KB
/
FF_manualoverride.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
import json
from Z_module import CompactJSONEncoder
def manual_override(dictionary_entry):
"""Manually override entries in phrases.json that do not follow expected patterns
note: if manual_override() is going to retun more than one entry per input,
you should include both dictionary_entry["range"] and dictionary_entry["phrase"] in the if
statement to avoid memory leackage (infinite loop)
"""
if dictionary_entry["range"] == [41035, 41037]:
new_entries = [
{
"range": dictionary_entry["range"],
"phrase": "It’s a jungle out there.",
"phrase_html": "<p><strong>It’s a jungle out there.</strong></p>",
"definition": "The real world is severe.; It’s hard to get by in everyday life. _ A: Gee, people are so rude in this town. B: Yup, it’s a jungle out there.",
"definition_html": "<p>The real world is severe.; It’s hard to get by in everyday life. <br>_ <em>A: Gee, people are so rude in this town. B: Yup, it’s a jungle out there.</em></p>",
"alt": ["constant"],
"runs": ["it’s a jungle out there"],
"multiple": dictionary_entry["multiple"],
"duplicate": dictionary_entry["duplicate"],
}
]
return new_entries
if (
dictionary_entry["range"] == [26246, 26255]
and dictionary_entry["phrase"] == "and for want of a horse the man was lost."
):
new_entries = [
{
"range": dictionary_entry["range"],
"phrase": "for want of a horse the man was lost.",
"phrase_html": "<p><strong>for want of a horse the man was lost.</strong></p>",
"definition": dictionary_entry["definition"],
"definition_html": dictionary_entry["definition_html"],
"alt": dictionary_entry["alt"],
"runs": ["for want of a", "horse the man was lost."],
"multiple": dictionary_entry["multiple"],
"duplicate": dictionary_entry["duplicate"],
}
]
return new_entries
if (
dictionary_entry["range"] == [82671, 82681]
and dictionary_entry["phrase"] == "*a go at someone *a stab at someone"
):
new_entries = [
{
"range": dictionary_entry["range"],
"phrase": "*a go at someone",
"phrase_html": "<p><strong>*</strong>a <strong>go at </strong>someone</p>",
"definition": dictionary_entry["definition"],
"definition_html": dictionary_entry["definition_html"],
"alt": ["asterisk", "article", "constant", "variable"],
"runs": ["*", "a", "go at", "someone"],
"multiple": dictionary_entry["multiple"],
"duplicate": dictionary_entry["duplicate"],
},
{
"range": dictionary_entry["range"],
"phrase": "*a stab at someone",
"phrase_html": "<p><strong>*</strong>a <strong>stab at </strong>someone </p>",
"definition": dictionary_entry["definition"],
"definition_html": dictionary_entry["definition_html"],
"alt": ["asterisk", "article", "constant", "variable"],
"runs": ["*", "a", "stab at", "someone"],
"multiple": dictionary_entry["multiple"],
"duplicate": dictionary_entry["duplicate"],
},
]
return new_entries
if (
dictionary_entry["range"] == [90790, 90794]
and dictionary_entry["phrase"] == "*out of someone’s way; *out of the road"
):
new_entries = [
{
"range": dictionary_entry["range"],
"phrase": "*out of someone’s way",
"phrase_html": "<p><strong>*out of </strong>someone’s <strong>way</p>",
"definition": dictionary_entry["definition"],
"definition_html": dictionary_entry["definition_html"],
"alt": ["constant", "variable", "constant"],
"runs": ["*out of", "someone’s", "way"],
"multiple": dictionary_entry["multiple"],
"duplicate": dictionary_entry["duplicate"],
},
{
"range": dictionary_entry["range"],
"phrase": "*out of the road",
"phrase_html": "<p>*out</strong> <strong>of the road</strong></p>",
"definition": dictionary_entry["definition"],
"definition_html": dictionary_entry["definition_html"],
"alt": ["constant", "constant"],
"runs": ["*out", "of the road"],
"multiple": dictionary_entry["multiple"],
"duplicate": dictionary_entry["duplicate"],
},
]
return new_entries
return []
# load the json file
with open("englishidioms/phrases.json", encoding="UTF-8") as f:
data = json.load(f)
for index, entry in enumerate(data["dictionary"]):
new_dicts = manual_override(entry)
if new_dicts:
del data["dictionary"][index]
# Insert one or more dictionaries at the same index
index_adjustment = (
0 # To keep track of how much the index is adjusted after insertion
)
for new_dict in new_dicts:
data["dictionary"].insert(index + index_adjustment, new_dict)
index_adjustment += 1 # Increment index adjustment
# overwrite the file
with open("englishidioms/phrases.json", "w", encoding="UTF-8") as f:
json.dump(data, f, indent=2, cls=CompactJSONEncoder, ensure_ascii=False)