Skip to content

Commit 2598763

Browse files
Add HTML annotation example to README
Demonstrates how to preserve original text spacing while annotating tokens with lemmas and POS tags for web display. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4bc9cf3 commit 2598763

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,46 @@ P # Root
105105
['sól']
106106
````
107107

108+
### Extracting lemmas and POS tags for web display
109+
110+
Greynir preserves the original text of each token, including surrounding whitespace.
111+
This allows you to reconstruct the exact original input while annotating it
112+
with linguistic information such as lemmas and part-of-speech (POS) tags.
113+
114+
````python
115+
from reynir import Greynir
116+
117+
g = Greynir()
118+
sent = g.parse_single("Ása sá sól.")
119+
120+
# Build a map from token index to terminal node
121+
terminal_map = {node.index: node for node in sent.terminal_nodes}
122+
123+
html_parts = []
124+
for i, tok in enumerate(sent.tokens):
125+
# tok.original preserves the original text with spacing
126+
original = tok.original or ""
127+
if i in terminal_map:
128+
node = terminal_map[i]
129+
# Wrap with linguistic info (lemma and POS tag)
130+
html_parts.append(
131+
f'<span data-lemma="{node.lemma}" data-pos="{node.tcat}">'
132+
f'{original}</span>'
133+
)
134+
else:
135+
html_parts.append(original)
136+
137+
html = "".join(html_parts)
138+
print(html)
139+
````
140+
141+
The program outputs the following HTML, with the original spacing preserved
142+
and each token annotated with its lemma and POS tag (`no`=noun, `so`=verb):
143+
144+
````html
145+
<span data-lemma="Ása" data-pos="no">Ása</span><span data-lemma="sjá" data-pos="so"> sá</span><span data-lemma="sól" data-pos="no"> sól</span><span data-lemma="." data-pos="">.</span>
146+
````
147+
108148
## Prerequisites
109149

110150
This package runs on CPython 3.9 or newer, and on PyPy 3.9 or newer.

0 commit comments

Comments
 (0)