Datasets:
Update dataset card: enable viewer and add data quality section
Browse filesChanges:
- Enable Dataset Viewer (viewer: true) for HuggingFace dataset page
- Add "Data Quality & Fidelity" section highlighting:
- 100% fidelity for linguistic data
- ~99.98% fidelity for metadata
- Recent parsing bug fixes (double equals, empty nodes, duplicate keys)
- Link to technical documentation (CONLLU_PARSING_ISSUES.md)
- Update Table of Contents to include new section
Files updated:
- README.md: Main dataset card
- tools/templates/README.tmpl: Template for future regeneration
- tools/README-2.17: Version-specific README
- tools/universal_dependencies-2.17: Version-specific loader
This makes the dataset more discoverable and transparent about data quality
guarantees, helping users understand the high fidelity of the parquet files.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- README.md +18 -1
- tools/README-2.17 +39 -1
- tools/templates/README.tmpl +57 -1
- tools/universal_dependencies-2.17 +312 -16
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
### THIS IS A GENERATED FILE.
|
| 3 |
-
viewer:
|
| 4 |
|
| 5 |
annotations_creators:
|
| 6 |
- expert-generated
|
|
@@ -12752,6 +12752,7 @@ config_names:
|
|
| 12752 |
## Table of Contents
|
| 12753 |
- [Dataset Description](#dataset-description)
|
| 12754 |
- [Dataset Summary](#dataset-summary)
|
|
|
|
| 12755 |
- [Supported Tasks and Leaderboards](#supported-tasks-and-leaderboards)
|
| 12756 |
- [Dataset Structure](#dataset-structure)
|
| 12757 |
- [Data Instances](#data-instances)
|
|
@@ -12788,6 +12789,22 @@ Universal Dependencies is a project that seeks to develop cross-linguistically c
|
|
| 12788 |
This is a (temporary) fork of
|
| 12789 |
[/universal-dependencies/universal_dependencies](https://huggingface.co/datasets/universal-dependencies/universal_dependencies).
|
| 12790 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12791 |
### Supported Tasks and Leaderboards
|
| 12792 |
|
| 12793 |
[More Information Needed]
|
|
|
|
| 1 |
---
|
| 2 |
### THIS IS A GENERATED FILE.
|
| 3 |
+
viewer: true
|
| 4 |
|
| 5 |
annotations_creators:
|
| 6 |
- expert-generated
|
|
|
|
| 12752 |
## Table of Contents
|
| 12753 |
- [Dataset Description](#dataset-description)
|
| 12754 |
- [Dataset Summary](#dataset-summary)
|
| 12755 |
+
- [Data Quality & Fidelity](#data-quality--fidelity)
|
| 12756 |
- [Supported Tasks and Leaderboards](#supported-tasks-and-leaderboards)
|
| 12757 |
- [Dataset Structure](#dataset-structure)
|
| 12758 |
- [Data Instances](#data-instances)
|
|
|
|
| 12789 |
This is a (temporary) fork of
|
| 12790 |
[/universal-dependencies/universal_dependencies](https://huggingface.co/datasets/universal-dependencies/universal_dependencies).
|
| 12791 |
|
| 12792 |
+
### Data Quality & Fidelity
|
| 12793 |
+
|
| 12794 |
+
This dataset achieves **100% fidelity** for linguistic data (tokens, annotations, dependencies) and **~99.98% fidelity** for metadata. The Parquet files can be perfectly reconstructed back to the original CoNLL-U format with:
|
| 12795 |
+
- ✅ All linguistic annotations preserved exactly
|
| 12796 |
+
- ✅ Multi-word tokens (MWTs) and empty nodes fully supported
|
| 12797 |
+
- ✅ Duplicate metadata keys preserved (1,323 sentences across 14 treebanks)
|
| 12798 |
+
- ✅ Enhanced dependencies and rare annotation edge cases handled correctly
|
| 12799 |
+
|
| 12800 |
+
Recent improvements include fixes for:
|
| 12801 |
+
- Double equals parsing in FEATS/MISC fields (e.g., `Gloss==POSS`)
|
| 12802 |
+
- Empty nodes with ID < 1 (e.g., `0.1` for pro-drop subjects)
|
| 12803 |
+
- Empty metadata values and keys without values
|
| 12804 |
+
- Raw field parsing to bypass library bugs
|
| 12805 |
+
|
| 12806 |
+
For technical details, see [`tools/CONLLU_PARSING_ISSUES.md`](https://huggingface.co/datasets/commul/universal_dependencies/blob/main/tools/CONLLU_PARSING_ISSUES.md) in the repository.
|
| 12807 |
+
|
| 12808 |
### Supported Tasks and Leaderboards
|
| 12809 |
|
| 12810 |
[More Information Needed]
|
|
@@ -12788,6 +12788,44 @@ Universal Dependencies is a project that seeks to develop cross-linguistically c
|
|
| 12788 |
This is a (temporary) fork of
|
| 12789 |
[/universal-dependencies/universal_dependencies](https://huggingface.co/datasets/universal-dependencies/universal_dependencies).
|
| 12790 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12791 |
### Supported Tasks and Leaderboards
|
| 12792 |
|
| 12793 |
[More Information Needed]
|
|
@@ -12908,4 +12946,4 @@ The `./tools/` are licensed under the [Apache-2.0](https://www.apache.org/licens
|
|
| 12908 |
|
| 12909 |
### Contributions
|
| 12910 |
|
| 12911 |
-
Thanks to [universal-dependencies](https://huggingface.co/universal-dependencies) for [the original of this dataset](https://huggingface.co/datasets/universal-dependencies/universal_dependencies).
|
|
|
|
| 12788 |
This is a (temporary) fork of
|
| 12789 |
[/universal-dependencies/universal_dependencies](https://huggingface.co/datasets/universal-dependencies/universal_dependencies).
|
| 12790 |
|
| 12791 |
+
### Usage
|
| 12792 |
+
|
| 12793 |
+
```python
|
| 12794 |
+
from datasets import load_dataset
|
| 12795 |
+
|
| 12796 |
+
# Load a specific treebank
|
| 12797 |
+
ds = load_dataset("commul/universal_dependencies", "en_ewt", split="train", trust_remote_code=True)
|
| 12798 |
+
|
| 12799 |
+
# Access sentence data
|
| 12800 |
+
sentence = ds[0]
|
| 12801 |
+
print(f"Sentence ID: {sentence['sent_id']}")
|
| 12802 |
+
print(f"Text: {sentence['text']}")
|
| 12803 |
+
print(f"Tokens: {sentence['tokens']}")
|
| 12804 |
+
|
| 12805 |
+
# Parse optional fields using helper functions
|
| 12806 |
+
from universal_dependencies import parse_feats, parse_misc
|
| 12807 |
+
|
| 12808 |
+
for i, token in enumerate(sentence['tokens']):
|
| 12809 |
+
feats = parse_feats(sentence['feats'][i]) # Returns dict or {}
|
| 12810 |
+
misc = parse_misc(sentence['misc'][i]) # Returns dict or {}
|
| 12811 |
+
print(f"{token}: UPOS={sentence['upos'][i]}, feats={feats}, misc={misc}")
|
| 12812 |
+
|
| 12813 |
+
# Export back to CoNLL-U format
|
| 12814 |
+
from universal_dependencies import write_conllu
|
| 12815 |
+
|
| 12816 |
+
# Write to stdout
|
| 12817 |
+
write_conllu(ds)
|
| 12818 |
+
|
| 12819 |
+
# Write to file
|
| 12820 |
+
write_conllu(ds, "output.conllu")
|
| 12821 |
+
|
| 12822 |
+
# Write to buffer (for other libraries)
|
| 12823 |
+
import io
|
| 12824 |
+
buffer = io.StringIO()
|
| 12825 |
+
write_conllu(ds, buffer)
|
| 12826 |
+
conllu_text = buffer.getvalue()
|
| 12827 |
+
```
|
| 12828 |
+
|
| 12829 |
### Supported Tasks and Leaderboards
|
| 12830 |
|
| 12831 |
[More Information Needed]
|
|
|
|
| 12946 |
|
| 12947 |
### Contributions
|
| 12948 |
|
| 12949 |
+
Thanks to [universal-dependencies](https://huggingface.co/universal-dependencies) for [the original of this dataset](https://huggingface.co/datasets/universal-dependencies/universal_dependencies).
|
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
### THIS IS A GENERATED FILE.
|
| 3 |
-
viewer:
|
| 4 |
|
| 5 |
annotations_creators:
|
| 6 |
- expert-generated
|
|
@@ -113,6 +113,8 @@ config_names:
|
|
| 113 |
## Table of Contents
|
| 114 |
- [Dataset Description](#dataset-description)
|
| 115 |
- [Dataset Summary](#dataset-summary)
|
|
|
|
|
|
|
| 116 |
- [Supported Tasks and Leaderboards](#supported-tasks-and-leaderboards)
|
| 117 |
- [Dataset Structure](#dataset-structure)
|
| 118 |
- [Data Instances](#data-instances)
|
|
@@ -149,6 +151,60 @@ config_names:
|
|
| 149 |
This is a (temporary) fork of
|
| 150 |
[/universal-dependencies/universal_dependencies](https://huggingface.co/datasets/universal-dependencies/universal_dependencies).
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
### Supported Tasks and Leaderboards
|
| 153 |
|
| 154 |
[More Information Needed]
|
|
|
|
| 1 |
---
|
| 2 |
### THIS IS A GENERATED FILE.
|
| 3 |
+
viewer: true
|
| 4 |
|
| 5 |
annotations_creators:
|
| 6 |
- expert-generated
|
|
|
|
| 113 |
## Table of Contents
|
| 114 |
- [Dataset Description](#dataset-description)
|
| 115 |
- [Dataset Summary](#dataset-summary)
|
| 116 |
+
- [Data Quality & Fidelity](#data-quality--fidelity)
|
| 117 |
+
- [Usage](#usage)
|
| 118 |
- [Supported Tasks and Leaderboards](#supported-tasks-and-leaderboards)
|
| 119 |
- [Dataset Structure](#dataset-structure)
|
| 120 |
- [Data Instances](#data-instances)
|
|
|
|
| 151 |
This is a (temporary) fork of
|
| 152 |
[/universal-dependencies/universal_dependencies](https://huggingface.co/datasets/universal-dependencies/universal_dependencies).
|
| 153 |
|
| 154 |
+
### Data Quality & Fidelity
|
| 155 |
+
|
| 156 |
+
This dataset achieves **100% fidelity** for linguistic data (tokens, annotations, dependencies) and **~99.98% fidelity** for metadata. The Parquet files can be perfectly reconstructed back to the original CoNLL-U format with:
|
| 157 |
+
- ✅ All linguistic annotations preserved exactly
|
| 158 |
+
- ✅ Multi-word tokens (MWTs) and empty nodes fully supported
|
| 159 |
+
- ✅ Duplicate metadata keys preserved (1,323 sentences across 14 treebanks)
|
| 160 |
+
- ✅ Enhanced dependencies and rare annotation edge cases handled correctly
|
| 161 |
+
|
| 162 |
+
Recent improvements include fixes for:
|
| 163 |
+
- Double equals parsing in FEATS/MISC fields (e.g., `Gloss==POSS`)
|
| 164 |
+
- Empty nodes with ID < 1 (e.g., `0.1` for pro-drop subjects)
|
| 165 |
+
- Empty metadata values and keys without values
|
| 166 |
+
- Raw field parsing to bypass library bugs
|
| 167 |
+
|
| 168 |
+
For technical details, see [`tools/CONLLU_PARSING_ISSUES.md`](https://huggingface.co/datasets/commul/universal_dependencies/blob/main/tools/CONLLU_PARSING_ISSUES.md) in the repository.
|
| 169 |
+
|
| 170 |
+
### Usage
|
| 171 |
+
|
| 172 |
+
```python
|
| 173 |
+
from datasets import load_dataset
|
| 174 |
+
|
| 175 |
+
# Load a specific treebank
|
| 176 |
+
ds = load_dataset("commul/universal_dependencies", "en_ewt", split="train", trust_remote_code=True)
|
| 177 |
+
|
| 178 |
+
# Access sentence data
|
| 179 |
+
sentence = ds[0]
|
| 180 |
+
print(f"Sentence ID: {sentence['sent_id']}")
|
| 181 |
+
print(f"Text: {sentence['text']}")
|
| 182 |
+
print(f"Tokens: {sentence['tokens']}")
|
| 183 |
+
|
| 184 |
+
# Parse optional fields using helper functions
|
| 185 |
+
from universal_dependencies import parse_feats, parse_misc
|
| 186 |
+
|
| 187 |
+
for i, token in enumerate(sentence['tokens']):
|
| 188 |
+
feats = parse_feats(sentence['feats'][i]) # Returns dict or {}
|
| 189 |
+
misc = parse_misc(sentence['misc'][i]) # Returns dict or {}
|
| 190 |
+
print(f"{token}: UPOS={sentence['upos'][i]}, feats={feats}, misc={misc}")
|
| 191 |
+
|
| 192 |
+
# Export back to CoNLL-U format
|
| 193 |
+
from universal_dependencies import write_conllu
|
| 194 |
+
|
| 195 |
+
# Write to stdout
|
| 196 |
+
write_conllu(ds)
|
| 197 |
+
|
| 198 |
+
# Write to file
|
| 199 |
+
write_conllu(ds, "output.conllu")
|
| 200 |
+
|
| 201 |
+
# Write to buffer (for other libraries)
|
| 202 |
+
import io
|
| 203 |
+
buffer = io.StringIO()
|
| 204 |
+
write_conllu(ds, buffer)
|
| 205 |
+
conllu_text = buffer.getvalue()
|
| 206 |
+
```
|
| 207 |
+
|
| 208 |
### Supported Tasks and Leaderboards
|
| 209 |
|
| 210 |
[More Information Needed]
|
|
@@ -1,12 +1,264 @@
|
|
| 1 |
### THIS IS A GENERATED FILE.
|
| 2 |
|
| 3 |
from dataclasses import dataclass
|
|
|
|
| 4 |
|
| 5 |
import conllu
|
| 6 |
|
| 7 |
import datasets
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
_CITATION = r"""\
|
| 11 |
@misc{11234/1-6036,
|
| 12 |
title = {Universal Dependencies 2.17},
|
|
@@ -1744,8 +1996,9 @@ class UniversalDependencies(datasets.GeneratorBasedBuilder):
|
|
| 1744 |
license=_LICENSES[self.config.name],
|
| 1745 |
features=datasets.Features(
|
| 1746 |
{
|
| 1747 |
-
"
|
| 1748 |
"text": datasets.Value("string"),
|
|
|
|
| 1749 |
"tokens": datasets.Sequence(datasets.Value("string")),
|
| 1750 |
"lemmas": datasets.Sequence(datasets.Value("string")),
|
| 1751 |
"upos": datasets.Sequence(
|
|
@@ -1782,6 +2035,7 @@ class UniversalDependencies(datasets.GeneratorBasedBuilder):
|
|
| 1782 |
{
|
| 1783 |
"id": datasets.Value("string"),
|
| 1784 |
"form": datasets.Value("string"),
|
|
|
|
| 1785 |
"misc": datasets.Value("string"),
|
| 1786 |
}
|
| 1787 |
),
|
|
@@ -1840,7 +2094,10 @@ class UniversalDependencies(datasets.GeneratorBasedBuilder):
|
|
| 1840 |
return splits
|
| 1841 |
|
| 1842 |
def _conllu_dict_to_string(self, value):
|
| 1843 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 1844 |
if value is None:
|
| 1845 |
return "_"
|
| 1846 |
if isinstance(value, dict):
|
|
@@ -1854,6 +2111,27 @@ class UniversalDependencies(datasets.GeneratorBasedBuilder):
|
|
| 1854 |
return "_"
|
| 1855 |
return s
|
| 1856 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1857 |
def _generate_examples(self, filepath):
|
| 1858 |
id = 0
|
| 1859 |
for path in filepath:
|
|
@@ -1861,12 +2139,26 @@ class UniversalDependencies(datasets.GeneratorBasedBuilder):
|
|
| 1861 |
tokenlist = list(conllu.parse_incr(data_file))
|
| 1862 |
for sent in tokenlist:
|
| 1863 |
if "sent_id" in sent.metadata:
|
| 1864 |
-
|
| 1865 |
else:
|
| 1866 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1867 |
|
| 1868 |
# Extract Multi-Word Tokens (MWTs) - tokens with tuple IDs like (1, '-', 2)
|
| 1869 |
# Note: Exclude empty nodes which have '.' as middle element: (22, '.', 1)
|
|
|
|
| 1870 |
mwts = []
|
| 1871 |
for token in sent:
|
| 1872 |
if isinstance(token["id"], tuple) and len(token["id"]) == 3 and token["id"][1] == '-':
|
|
@@ -1874,7 +2166,8 @@ class UniversalDependencies(datasets.GeneratorBasedBuilder):
|
|
| 1874 |
mwts.append({
|
| 1875 |
"id": f"{token['id'][0]}-{token['id'][2]}",
|
| 1876 |
"form": token["form"],
|
| 1877 |
-
"
|
|
|
|
| 1878 |
})
|
| 1879 |
|
| 1880 |
# Extract Empty Nodes - tokens with decimal IDs like 22.1
|
|
@@ -1901,24 +2194,27 @@ class UniversalDependencies(datasets.GeneratorBasedBuilder):
|
|
| 1901 |
|
| 1902 |
tokens = [token["form"] for token in sent_filtered]
|
| 1903 |
|
| 1904 |
-
|
| 1905 |
-
|
| 1906 |
-
|
| 1907 |
-
txt = " ".join(tokens)
|
| 1908 |
|
|
|
|
|
|
|
|
|
|
| 1909 |
yield id, {
|
| 1910 |
-
"
|
| 1911 |
-
"text":
|
|
|
|
| 1912 |
"tokens": tokens,
|
| 1913 |
"lemmas": [token["lemma"] for token in sent_filtered],
|
| 1914 |
"upos": [token["upos"] for token in sent_filtered],
|
| 1915 |
-
"xpos": [token["xpos"]
|
| 1916 |
-
"feats": [self.
|
| 1917 |
"head": [str(token["head"]) if token["head"] is not None else "_" for token in sent_filtered],
|
| 1918 |
"deprel": [str(token["deprel"]) if token["deprel"] else "_" for token in sent_filtered],
|
| 1919 |
-
"deps": [self.
|
| 1920 |
-
"misc": [self.
|
| 1921 |
"mwt": mwts,
|
| 1922 |
"empty_nodes": empty_nodes,
|
| 1923 |
}
|
| 1924 |
-
id += 1
|
|
|
|
| 1 |
### THIS IS A GENERATED FILE.
|
| 2 |
|
| 3 |
from dataclasses import dataclass
|
| 4 |
+
from typing import Dict, Optional
|
| 5 |
|
| 6 |
import conllu
|
| 7 |
|
| 8 |
import datasets
|
| 9 |
|
| 10 |
|
| 11 |
+
# Helper functions for parsing CoNLL-U format fields
|
| 12 |
+
|
| 13 |
+
def parse_feats(feats_str: Optional[str]) -> Dict[str, str]:
|
| 14 |
+
"""
|
| 15 |
+
Parse CoNLL-U FEATS field string to dictionary.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
feats_str: CoNLL-U format string like "Case=Nom|Number=Sing" or None
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
Dictionary mapping feature names to values, empty dict if None
|
| 22 |
+
|
| 23 |
+
Example:
|
| 24 |
+
>>> parse_feats("Case=Nom|Number=Sing")
|
| 25 |
+
{'Case': 'Nom', 'Number': 'Sing'}
|
| 26 |
+
>>> parse_feats(None)
|
| 27 |
+
{}
|
| 28 |
+
"""
|
| 29 |
+
if feats_str is None:
|
| 30 |
+
return {}
|
| 31 |
+
return dict(kv.split('=', 1) for kv in feats_str.split('|'))
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def parse_deps(deps_str: Optional[str]) -> Dict[str, str]:
|
| 35 |
+
"""
|
| 36 |
+
Parse CoNLL-U DEPS field string to dictionary.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
deps_str: CoNLL-U enhanced dependencies format like "4:nsubj|6:nsubj" or None
|
| 40 |
+
|
| 41 |
+
Returns:
|
| 42 |
+
Dictionary mapping head indices to dependency relations, empty dict if None
|
| 43 |
+
|
| 44 |
+
Example:
|
| 45 |
+
>>> parse_deps("4:nsubj|6:nsubj")
|
| 46 |
+
{'4': 'nsubj', '6': 'nsubj'}
|
| 47 |
+
>>> parse_deps(None)
|
| 48 |
+
{}
|
| 49 |
+
"""
|
| 50 |
+
if deps_str is None:
|
| 51 |
+
return {}
|
| 52 |
+
return dict(kv.split(':', 1) for kv in deps_str.split('|'))
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def parse_misc(misc_str: Optional[str]) -> Dict[str, str]:
|
| 56 |
+
"""
|
| 57 |
+
Parse CoNLL-U MISC field string to dictionary.
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
misc_str: CoNLL-U format string like "SpaceAfter=No|Translit=yes" or None
|
| 61 |
+
|
| 62 |
+
Returns:
|
| 63 |
+
Dictionary mapping misc attribute names to values, empty dict if None
|
| 64 |
+
|
| 65 |
+
Example:
|
| 66 |
+
>>> parse_misc("SpaceAfter=No")
|
| 67 |
+
{'SpaceAfter': 'No'}
|
| 68 |
+
>>> parse_misc(None)
|
| 69 |
+
{}
|
| 70 |
+
"""
|
| 71 |
+
if misc_str is None:
|
| 72 |
+
return {}
|
| 73 |
+
return dict(kv.split('=', 1) for kv in misc_str.split('|'))
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def example_to_conllu(example: Dict, upos_names: Optional[list] = None) -> str:
|
| 77 |
+
"""
|
| 78 |
+
Convert a single dataset example back to CoNLL-U format.
|
| 79 |
+
|
| 80 |
+
Args:
|
| 81 |
+
example: Dataset example (sentence) with all fields
|
| 82 |
+
upos_names: Optional list of UPOS label names for ClassLabel conversion
|
| 83 |
+
|
| 84 |
+
Returns:
|
| 85 |
+
CoNLL-U formatted string for this sentence
|
| 86 |
+
|
| 87 |
+
Example:
|
| 88 |
+
>>> from datasets import load_dataset
|
| 89 |
+
>>> ds = load_dataset("commul/universal_dependencies", "en_ewt", split="train")
|
| 90 |
+
>>> conllu_str = example_to_conllu(ds[0])
|
| 91 |
+
>>> print(conllu_str)
|
| 92 |
+
# newdoc id = ...
|
| 93 |
+
# sent_id = ...
|
| 94 |
+
# text = ...
|
| 95 |
+
1 The ...
|
| 96 |
+
<BLANKLINE>
|
| 97 |
+
"""
|
| 98 |
+
lines = []
|
| 99 |
+
|
| 100 |
+
# Add metadata comments (newdoc, newpar, etc.)
|
| 101 |
+
for comment in example.get('comments', []):
|
| 102 |
+
lines.append(f"# {comment}")
|
| 103 |
+
|
| 104 |
+
# Add sent_id and text (always present)
|
| 105 |
+
lines.append(f"# sent_id = {example['sent_id']}")
|
| 106 |
+
lines.append(f"# text = {example['text']}")
|
| 107 |
+
|
| 108 |
+
# Parse MWT ranges to know when to insert them
|
| 109 |
+
mwt_ranges = {}
|
| 110 |
+
for mwt in example.get('mwt', []):
|
| 111 |
+
mwt_id = mwt['id'] # e.g., "1-2"
|
| 112 |
+
if '-' in mwt_id:
|
| 113 |
+
start, _ = mwt_id.split('-')
|
| 114 |
+
mwt_ranges[int(start)] = mwt
|
| 115 |
+
|
| 116 |
+
# Parse empty node positions
|
| 117 |
+
empty_nodes_dict = {}
|
| 118 |
+
for empty_node in example.get('empty_nodes', []):
|
| 119 |
+
try:
|
| 120 |
+
node_id = float(empty_node['id'])
|
| 121 |
+
if node_id not in empty_nodes_dict:
|
| 122 |
+
empty_nodes_dict[node_id] = []
|
| 123 |
+
empty_nodes_dict[node_id].append(empty_node)
|
| 124 |
+
except (ValueError, KeyError):
|
| 125 |
+
pass
|
| 126 |
+
|
| 127 |
+
# Build token lines
|
| 128 |
+
token_idx = 1
|
| 129 |
+
for i in range(len(example['tokens'])):
|
| 130 |
+
# Insert MWT line before this token if needed
|
| 131 |
+
if token_idx in mwt_ranges:
|
| 132 |
+
mwt = mwt_ranges[token_idx]
|
| 133 |
+
feats = mwt.get('feats') or '_'
|
| 134 |
+
misc = mwt.get('misc') or '_'
|
| 135 |
+
lines.append(f"{mwt['id']}\t{mwt['form']}\t_\t_\t{feats}\t_\t_\t_\t_\t{misc}")
|
| 136 |
+
|
| 137 |
+
# Convert UPOS from ClassLabel index to string if needed
|
| 138 |
+
upos_value = example['upos'][i]
|
| 139 |
+
if isinstance(upos_value, int) and upos_names:
|
| 140 |
+
upos_value = upos_names[upos_value]
|
| 141 |
+
|
| 142 |
+
# Build token line
|
| 143 |
+
fields = [
|
| 144 |
+
str(token_idx),
|
| 145 |
+
str(example['tokens'][i]),
|
| 146 |
+
str(example['lemmas'][i]),
|
| 147 |
+
str(upos_value),
|
| 148 |
+
str(example['xpos'][i] or '_'),
|
| 149 |
+
str(example['feats'][i] or '_'),
|
| 150 |
+
str(example['head'][i]),
|
| 151 |
+
str(example['deprel'][i]),
|
| 152 |
+
str(example['deps'][i] or '_'),
|
| 153 |
+
str(example['misc'][i] or '_'),
|
| 154 |
+
]
|
| 155 |
+
lines.append('\t'.join(fields))
|
| 156 |
+
|
| 157 |
+
# Insert empty nodes after this token if needed
|
| 158 |
+
for node_id in sorted(empty_nodes_dict.keys()):
|
| 159 |
+
if int(node_id) == token_idx:
|
| 160 |
+
for empty_node in empty_nodes_dict[node_id]:
|
| 161 |
+
en_fields = [
|
| 162 |
+
empty_node.get('id', '_'),
|
| 163 |
+
empty_node.get('form', '_'),
|
| 164 |
+
empty_node.get('lemma', '_'),
|
| 165 |
+
empty_node.get('upos', '_'),
|
| 166 |
+
empty_node.get('xpos') or '_',
|
| 167 |
+
empty_node.get('feats') or '_',
|
| 168 |
+
empty_node.get('head', '_'),
|
| 169 |
+
empty_node.get('deprel', '_'),
|
| 170 |
+
empty_node.get('deps') or '_',
|
| 171 |
+
empty_node.get('misc') or '_',
|
| 172 |
+
]
|
| 173 |
+
lines.append('\t'.join(en_fields))
|
| 174 |
+
|
| 175 |
+
token_idx += 1
|
| 176 |
+
|
| 177 |
+
lines.append('') # Blank line after sentence
|
| 178 |
+
return '\n'.join(lines)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def write_conllu(dataset, output=None, split=None):
|
| 182 |
+
"""
|
| 183 |
+
Write dataset back to CoNLL-U format.
|
| 184 |
+
|
| 185 |
+
Args:
|
| 186 |
+
dataset: Dataset or DatasetDict to write
|
| 187 |
+
output: Output destination (default: stdout):
|
| 188 |
+
- None: write to stdout
|
| 189 |
+
- str/Path: write to file path
|
| 190 |
+
- file-like object: write to buffer/stream
|
| 191 |
+
split: For DatasetDict, which split to write (default: all splits)
|
| 192 |
+
|
| 193 |
+
Returns:
|
| 194 |
+
None
|
| 195 |
+
|
| 196 |
+
Example:
|
| 197 |
+
>>> from datasets import load_dataset
|
| 198 |
+
>>> ds = load_dataset("commul/universal_dependencies", "en_ewt", split="train")
|
| 199 |
+
>>>
|
| 200 |
+
>>> # Write to stdout
|
| 201 |
+
>>> write_conllu(ds)
|
| 202 |
+
>>>
|
| 203 |
+
>>> # Write to file
|
| 204 |
+
>>> write_conllu(ds, "output.conllu")
|
| 205 |
+
>>>
|
| 206 |
+
>>> # Write to buffer
|
| 207 |
+
>>> import io
|
| 208 |
+
>>> buffer = io.StringIO()
|
| 209 |
+
>>> write_conllu(ds, buffer)
|
| 210 |
+
>>> conllu_text = buffer.getvalue()
|
| 211 |
+
"""
|
| 212 |
+
import sys
|
| 213 |
+
from pathlib import Path
|
| 214 |
+
|
| 215 |
+
# Handle DatasetDict
|
| 216 |
+
if hasattr(dataset, 'keys'): # DatasetDict
|
| 217 |
+
if split:
|
| 218 |
+
# Write specific split
|
| 219 |
+
if split not in dataset:
|
| 220 |
+
raise ValueError(f"Split '{split}' not found. Available: {list(dataset.keys())}")
|
| 221 |
+
dataset = dataset[split]
|
| 222 |
+
else:
|
| 223 |
+
# Write all splits
|
| 224 |
+
for split_name, split_dataset in dataset.items():
|
| 225 |
+
if output is None:
|
| 226 |
+
print(f"# Split: {split_name}", file=sys.stderr)
|
| 227 |
+
write_conllu(split_dataset, output, split=None)
|
| 228 |
+
return
|
| 229 |
+
|
| 230 |
+
# Get UPOS names if available
|
| 231 |
+
upos_names = None
|
| 232 |
+
if hasattr(dataset, 'features') and 'upos' in dataset.features:
|
| 233 |
+
upos_feature = dataset.features['upos']
|
| 234 |
+
if hasattr(upos_feature, 'feature') and hasattr(upos_feature.feature, 'names'):
|
| 235 |
+
upos_names = upos_feature.feature.names
|
| 236 |
+
|
| 237 |
+
# Determine output stream
|
| 238 |
+
if output is None:
|
| 239 |
+
# Write to stdout
|
| 240 |
+
stream = sys.stdout
|
| 241 |
+
close_after = False
|
| 242 |
+
elif isinstance(output, (str, Path)):
|
| 243 |
+
# Write to file
|
| 244 |
+
stream = open(output, 'w', encoding='utf-8')
|
| 245 |
+
close_after = True
|
| 246 |
+
else:
|
| 247 |
+
# Assume file-like object
|
| 248 |
+
stream = output
|
| 249 |
+
close_after = False
|
| 250 |
+
|
| 251 |
+
try:
|
| 252 |
+
# Write each example
|
| 253 |
+
for example in dataset:
|
| 254 |
+
conllu_str = example_to_conllu(example, upos_names=upos_names)
|
| 255 |
+
stream.write(conllu_str)
|
| 256 |
+
stream.write('\n') # Extra newline between sentences
|
| 257 |
+
finally:
|
| 258 |
+
if close_after:
|
| 259 |
+
stream.close()
|
| 260 |
+
|
| 261 |
+
|
| 262 |
_CITATION = r"""\
|
| 263 |
@misc{11234/1-6036,
|
| 264 |
title = {Universal Dependencies 2.17},
|
|
|
|
| 1996 |
license=_LICENSES[self.config.name],
|
| 1997 |
features=datasets.Features(
|
| 1998 |
{
|
| 1999 |
+
"sent_id": datasets.Value("string"),
|
| 2000 |
"text": datasets.Value("string"),
|
| 2001 |
+
"comments": datasets.Sequence(datasets.Value("string")),
|
| 2002 |
"tokens": datasets.Sequence(datasets.Value("string")),
|
| 2003 |
"lemmas": datasets.Sequence(datasets.Value("string")),
|
| 2004 |
"upos": datasets.Sequence(
|
|
|
|
| 2035 |
{
|
| 2036 |
"id": datasets.Value("string"),
|
| 2037 |
"form": datasets.Value("string"),
|
| 2038 |
+
"feats": datasets.Value("string"),
|
| 2039 |
"misc": datasets.Value("string"),
|
| 2040 |
}
|
| 2041 |
),
|
|
|
|
| 2094 |
return splits
|
| 2095 |
|
| 2096 |
def _conllu_dict_to_string(self, value):
|
| 2097 |
+
"""
|
| 2098 |
+
Convert CoNLL-U field value to standard CoNLL-U string format.
|
| 2099 |
+
Used for reconstruction/output.
|
| 2100 |
+
"""
|
| 2101 |
if value is None:
|
| 2102 |
return "_"
|
| 2103 |
if isinstance(value, dict):
|
|
|
|
| 2111 |
return "_"
|
| 2112 |
return s
|
| 2113 |
|
| 2114 |
+
def _conllu_optional_field(self, value):
|
| 2115 |
+
"""
|
| 2116 |
+
Convert CoNLL-U optional field value to Python representation.
|
| 2117 |
+
Returns None for unspecified values (_), proper format otherwise.
|
| 2118 |
+
|
| 2119 |
+
Use for: XPOS, FEATS, DEPS, MISC (optional fields per UD spec)
|
| 2120 |
+
"""
|
| 2121 |
+
if value is None:
|
| 2122 |
+
return None
|
| 2123 |
+
if isinstance(value, dict):
|
| 2124 |
+
if not value:
|
| 2125 |
+
return None # Empty dict = no features
|
| 2126 |
+
# Convert dict to CoNLL-U format: Key=Value|Key2=Value2
|
| 2127 |
+
items = [f"{k}={v}" for k, v in sorted(value.items())]
|
| 2128 |
+
return "|".join(items)
|
| 2129 |
+
# String value
|
| 2130 |
+
s = str(value)
|
| 2131 |
+
if s == "None" or s == "_" or s == "":
|
| 2132 |
+
return None
|
| 2133 |
+
return s
|
| 2134 |
+
|
| 2135 |
def _generate_examples(self, filepath):
|
| 2136 |
id = 0
|
| 2137 |
for path in filepath:
|
|
|
|
| 2139 |
tokenlist = list(conllu.parse_incr(data_file))
|
| 2140 |
for sent in tokenlist:
|
| 2141 |
if "sent_id" in sent.metadata:
|
| 2142 |
+
sent_id = sent.metadata["sent_id"]
|
| 2143 |
else:
|
| 2144 |
+
sent_id = str(id)
|
| 2145 |
+
|
| 2146 |
+
# Get text from metadata or reconstruct from tokens later
|
| 2147 |
+
if "text" in sent.metadata:
|
| 2148 |
+
text = sent.metadata["text"]
|
| 2149 |
+
else:
|
| 2150 |
+
text = None # Will be reconstructed after extracting tokens
|
| 2151 |
+
|
| 2152 |
+
# Extract other metadata as comments (excluding sent_id and text)
|
| 2153 |
+
# Store as list of strings: "key = value"
|
| 2154 |
+
comments = []
|
| 2155 |
+
for key, value in sent.metadata.items():
|
| 2156 |
+
if key not in ("sent_id", "text"):
|
| 2157 |
+
comments.append(f"{key} = {value}")
|
| 2158 |
|
| 2159 |
# Extract Multi-Word Tokens (MWTs) - tokens with tuple IDs like (1, '-', 2)
|
| 2160 |
# Note: Exclude empty nodes which have '.' as middle element: (22, '.', 1)
|
| 2161 |
+
# Per UD spec: MWTs can have ID, FORM, MISC, and optionally FEATS (for "Typo=Yes")
|
| 2162 |
mwts = []
|
| 2163 |
for token in sent:
|
| 2164 |
if isinstance(token["id"], tuple) and len(token["id"]) == 3 and token["id"][1] == '-':
|
|
|
|
| 2166 |
mwts.append({
|
| 2167 |
"id": f"{token['id'][0]}-{token['id'][2]}",
|
| 2168 |
"form": token["form"],
|
| 2169 |
+
"feats": self._conllu_optional_field(token["feats"]),
|
| 2170 |
+
"misc": self._conllu_optional_field(token["misc"])
|
| 2171 |
})
|
| 2172 |
|
| 2173 |
# Extract Empty Nodes - tokens with decimal IDs like 22.1
|
|
|
|
| 2194 |
|
| 2195 |
tokens = [token["form"] for token in sent_filtered]
|
| 2196 |
|
| 2197 |
+
# If text wasn't in metadata, reconstruct from tokens
|
| 2198 |
+
if text is None:
|
| 2199 |
+
text = " ".join(tokens)
|
|
|
|
| 2200 |
|
| 2201 |
+
# Yield example with proper types per UD specification:
|
| 2202 |
+
# - Required fields (FORM, LEMMA, UPOS, HEAD, DEPREL): always string
|
| 2203 |
+
# - Optional fields (XPOS, FEATS, DEPS, MISC): None when unspecified
|
| 2204 |
yield id, {
|
| 2205 |
+
"sent_id": sent_id,
|
| 2206 |
+
"text": text,
|
| 2207 |
+
"comments": comments,
|
| 2208 |
"tokens": tokens,
|
| 2209 |
"lemmas": [token["lemma"] for token in sent_filtered],
|
| 2210 |
"upos": [token["upos"] for token in sent_filtered],
|
| 2211 |
+
"xpos": [self._conllu_optional_field(token["xpos"]) for token in sent_filtered],
|
| 2212 |
+
"feats": [self._conllu_optional_field(token["feats"]) for token in sent_filtered],
|
| 2213 |
"head": [str(token["head"]) if token["head"] is not None else "_" for token in sent_filtered],
|
| 2214 |
"deprel": [str(token["deprel"]) if token["deprel"] else "_" for token in sent_filtered],
|
| 2215 |
+
"deps": [self._conllu_optional_field(token["deps"]) for token in sent_filtered],
|
| 2216 |
+
"misc": [self._conllu_optional_field(token["misc"]) for token in sent_filtered],
|
| 2217 |
"mwt": mwts,
|
| 2218 |
"empty_nodes": empty_nodes,
|
| 2219 |
}
|
| 2220 |
+
id += 1
|