File size: 7,038 Bytes
e14cd69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""CoNLL-U reader and writer.

Supports:
- Regular tokens (integer IDs)
- Multi-word tokens (range IDs like "1-2")
- Empty nodes (decimal IDs like "1.1")
- FEATS as dict
- Sentence-level comments
"""
from __future__ import annotations

import re
import os
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Union

from tqdm import tqdm


@dataclass
class Token:
    id: Union[int, str]       # int for regular; "1-2" for MWT; "1.1" for empty
    form: str
    lemma: str = "_"
    upos: str = "_"
    xpos: str = "_"
    feats: Union[Dict[str, str], str] = "_"  # parsed dict or "_"
    head: Optional[int] = None               # None when not set
    deprel: str = "_"
    deps: str = "_"
    misc: str = "_"
    space_after: bool = True
    is_mwt: bool = False

    # Convenience helpers
    def is_multiword(self) -> bool:
        return isinstance(self.id, str) and "-" in str(self.id)

    def is_empty(self) -> bool:
        return isinstance(self.id, str) and "." in str(self.id)

    def feats_str(self) -> str:
        if isinstance(self.feats, dict):
            if not self.feats:
                return "_"
            return "|".join(f"{k}={v}" for k, v in sorted(self.feats.items()))
        return self.feats if self.feats else "_"

    def __str__(self) -> str:
        head_str = str(self.head) if self.head is not None else "_"

        # Handle SpaceAfter in misc column: only write SpaceAfter=No
        misc_val = self.misc if self.misc != "_" else ""
        if not self.space_after:
            space_attr = "SpaceAfter=No"
            if misc_val:
                misc_final = f"{misc_val},{space_attr}" if "," not in misc_val else f"{misc_val},{space_attr}"
            else:
                misc_final = space_attr
        else:
            misc_final = misc_val

        return "\t".join([
            str(self.id),
            self.form,
            self.lemma,
            self.upos,
            self.xpos,
            self.feats_str(),
            head_str,
            self.deprel,
            self.deps,
            misc_final,
        ])


@dataclass
class Sentence:
    tokens: List[Token] = field(default_factory=list)
    comments: List[str] = field(default_factory=list)

    def regular_tokens(self) -> List[Token]:
        """Return only regular (non-MWT, non-empty) tokens."""
        return [t for t in self.tokens if not t.is_multiword() and not t.is_empty()]

    def surface_tokens(self) -> List[Token]:
        """Return surface tokens, excluding MWT components and empty nodes."""
        tokens: List[Token] = []
        multiword_end = 0
        for token in self.tokens:
            if token.is_empty():
                continue
            if token.is_multiword():
                multiword_end = int(str(token.id).split("-", 1)[1])
                tokens.append(token)
            elif int(token.id) > multiword_end:
                tokens.append(token)
        return tokens

    def __repr__(self) -> str:
        words = " ".join(t.form for t in self.regular_tokens())
        return f"<Sentence: {words!r}>"

    def __str__(self) -> str:
        lines = list(self.comments)
        lines.extend(str(t) for t in self.tokens)
        lines.append("")
        return "\n".join(lines)

    def to_conllu(self) -> str:
        return str(self)


# ---------------------------------------------------------------------------
# Parsing helpers
# ---------------------------------------------------------------------------

def _parse_feats(feats_str: str) -> Union[Dict[str, str], str]:
    if feats_str in ("_", ""):
        return "_"
    feats: Dict[str, str] = {}
    for part in feats_str.split("|"):
        if "=" in part:
            k, v = part.split("=", 1)
            feats[k] = v
        else:
            feats[part] = ""
    return feats


def _parse_id(id_str: str) -> Union[int, str]:
    """Return int for simple IDs, str for ranges and decimals."""
    if re.match(r"^\d+$", id_str):
        return int(id_str)
    return id_str


def _parse_head(head_str: str) -> Optional[int]:
    if head_str == "_":
        return None
    try:
        return int(head_str)
    except ValueError:
        return None


# ---------------------------------------------------------------------------
# Public API
# ---------------------------------------------------------------------------

def read_conllu(path: str, *, show_progress: bool = False) -> List[Sentence]:
    """Read a CoNLL-U file and return a list of Sentence objects."""
    sentences: List[Sentence] = []
    current_tokens: List[Token] = []
    current_comments: List[str] = []

    with open(path, encoding="utf-8", newline="") as fh:
        progress = tqdm(
            total=os.path.getsize(path),
            desc=f"Loading {os.path.basename(path)}",
            unit="B",
            unit_scale=True,
            disable=not show_progress,
        )
        try:
            for raw_line in fh:
                progress.update(len(raw_line.encode("utf-8")))
                line = raw_line.rstrip("\r\n")
                if line.startswith("#"):
                    current_comments.append(line)
                elif line == "":
                    if current_tokens:
                        sentences.append(Sentence(tokens=current_tokens, comments=current_comments))
                    current_tokens = []
                    current_comments = []
                else:
                    parts = line.split("\t")
                    if len(parts) != 10:
                        continue  # skip malformed lines

                    misc = parts[9]
                    space_after = True
                    if "SpaceAfter=No" in misc:
                        space_after = False
                    elif "SpaceAfter=Yes" in misc:
                        space_after = True

                    tok = Token(
                        id=_parse_id(parts[0]),
                        form=parts[1],
                        lemma=parts[2],
                        upos=parts[3],
                        xpos=parts[4],
                        feats=_parse_feats(parts[5]),
                        head=_parse_head(parts[6]),
                        deprel=parts[7],
                        deps=parts[8],
                        misc=misc,
                        space_after=space_after,
                    )
                    current_tokens.append(tok)
        finally:
            progress.close()

    # Handle file without trailing newline
    if current_tokens:
        sentences.append(Sentence(tokens=current_tokens, comments=current_comments))

    return sentences


def write_conllu(sentences: List[Sentence], path: str) -> None:
    """Write a list of Sentence objects to a CoNLL-U file."""
    with open(path, "w", encoding="utf-8") as fh:
        for sent in sentences:
            for comment in sent.comments:
                fh.write(comment + "\n")
            for token in sent.tokens:
                fh.write(str(token) + "\n")
            fh.write("\n")