Skip to content

Commit 9dde242

Browse files
committed
wordnet: change grammar info from Italic to colored, defaulting to green
just like FreeDict and Wiktextract
1 parent 5f72b32 commit 9dde242

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

doc/p/wordnet.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@
1414
| Kind | 📁 directory |
1515
| Wiki | [WordNet](https://en.wikipedia.org/wiki/WordNet) |
1616
| Website | [WordNet - A Lexical Database for English](https://wordnet.princeton.edu/) |
17+
18+
### Read options
19+
20+
| Name | Default | Type | Comment |
21+
| ---------- | ------- | ---- | ------------- |
22+
| gram_color | `green` | str | Grammar color |

plugins-meta/index.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,10 +1872,19 @@
18721872
"description": "WordNet",
18731873
"extensions": [],
18741874
"singleFile": false,
1875-
"optionsProp": {},
1875+
"optionsProp": {
1876+
"gram_color": {
1877+
"class": "StrOption",
1878+
"type": "str",
1879+
"customValue": true,
1880+
"comment": "Grammar color"
1881+
}
1882+
},
18761883
"canRead": true,
18771884
"canWrite": false,
1878-
"readOptions": {}
1885+
"readOptions": {
1886+
"gram_color": "green"
1887+
}
18791888
},
18801889
{
18811890
"module": "wordset",

pyglossary/plugins/wordnet/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
from typing import TYPE_CHECKING
55

6+
from pyglossary.option import StrOption
7+
68
from .reader import Reader
79

810
if TYPE_CHECKING:
@@ -38,4 +40,8 @@
3840
)
3941

4042
# key is option/argument name, value is instance of Option
41-
optionsProp: dict[str, Option] = {}
43+
optionsProp: dict[str, Option] = {
44+
"gram_color": StrOption(
45+
comment="Grammar color",
46+
),
47+
}

pyglossary/plugins/wordnet/reader.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,9 @@ class WordNet:
184184
"data.verb": ["v"],
185185
}
186186

187-
def __init__(self, wordnetdir: str) -> None:
187+
def __init__(self, wordnetdir: str, gram_color: str) -> None:
188188
self.wordnetdir = wordnetdir
189+
self.gram_color = gram_color
189190
self.collector: dict[str, list[str]] = defaultdict(list)
190191

191192
@staticmethod
@@ -204,6 +205,7 @@ def prepare(self) -> None: # noqa: PLR0912
204205
file2pos = self.file2pos
205206

206207
dict_dir = self.wordnetdir
208+
gram_color = self.gram_color
207209

208210
files: dict[str, io.TextIOWrapper] = {}
209211
for name in os.listdir(dict_dir):
@@ -232,6 +234,11 @@ def href(word: str) -> str:
232234
)
233235

234236
words = synset.words
237+
238+
gram = synSetTypes[synset.ss_type]
239+
gram_html = f'<font class="pos grammar" color="{gram_color}">{gram}</font>'
240+
gloss_with_examples = gram_html + gloss_with_examples
241+
235242
for wordIndex, word in enumerate(words):
236243
# TODO: move this block to a func
237244
synonyms_links = ", ".join(href(w) for w in words if w != word)
@@ -277,10 +284,10 @@ def href(word: str) -> str:
277284
for symbol_desc, referenced_words in pointers.items()
278285
if referenced_words
279286
)
280-
self.collector[word].append(
281-
f'<i class="pos grammar">{synSetTypes[synset.ss_type]}</i>'
282-
f" {gloss_with_examples}{synonyms_str}{pointers_str}",
283-
)
287+
288+
text = gloss_with_examples + synonyms_str + pointers_str
289+
self.collector[word].append(text)
290+
284291
sys.stdout.write("\n")
285292
sys.stdout.flush()
286293

@@ -308,6 +315,7 @@ def process(self) -> Iterator[tuple[str, str]]:
308315

309316
class Reader:
310317
useByteProgress = False
318+
_gram_color: str = "green"
311319

312320
def __init__(self, glos: ReaderGlossaryType) -> None:
313321
self._glos = glos
@@ -319,7 +327,7 @@ def __len__(self) -> int:
319327
return self._entryCount
320328

321329
def open(self, filename: str) -> None:
322-
self.wordnet = WordNet(filename)
330+
self.wordnet = WordNet(filename, gram_color=self._gram_color)
323331
log.info("Running wordnet.prepare()")
324332
self.wordnet.prepare()
325333

0 commit comments

Comments
 (0)