Datasets:
Tasks:
Token Classification
Modalities:
Text
Formats:
parquet
Sub-tasks:
named-entity-recognition
Languages:
Tagalog
Size:
1K - 10K
ArXiv:
DOI:
License:
Delete loading script auxiliary file
Browse files- spacy_to_iob.py +0 -50
spacy_to_iob.py
DELETED
|
@@ -1,50 +0,0 @@
|
|
| 1 |
-
from pathlib import Path
|
| 2 |
-
|
| 3 |
-
import spacy
|
| 4 |
-
import typer
|
| 5 |
-
from spacy.tokens import DocBin
|
| 6 |
-
from wasabi import msg
|
| 7 |
-
|
| 8 |
-
DELIMITER = "-DOCSTART- -X- O O"
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def spacy_to_iob(
|
| 12 |
-
# fmt: off
|
| 13 |
-
spacy_indir: Path = typer.Argument(..., help="Path to the directory containing the spaCy files."),
|
| 14 |
-
iob_outdir: Path = typer.Argument(..., help="Path to the directory to save the IOB files."),
|
| 15 |
-
lang: str = typer.Option("tl", "-l", "--lang", help="Language code for the spaCy vocab."),
|
| 16 |
-
verbose: bool = typer.Option(False, "-v", "--verbose", help="Print additional information."),
|
| 17 |
-
delimiter: str = typer.Option(DELIMITER, "-d", "--delimiter", help="Delimiter between examples.")
|
| 18 |
-
# fmt: on
|
| 19 |
-
):
|
| 20 |
-
"""Convert spaCy files into IOB-formatted files."""
|
| 21 |
-
nlp = spacy.blank(lang)
|
| 22 |
-
for spacy_file in spacy_indir.glob(f"*.spacy"):
|
| 23 |
-
msg.text(f"Converting {str(spacy_file)}", show=verbose)
|
| 24 |
-
doc_bin = DocBin().from_disk(spacy_file)
|
| 25 |
-
docs = doc_bin.get_docs(nlp.vocab)
|
| 26 |
-
|
| 27 |
-
lines = [] # container for the IOB lines later on
|
| 28 |
-
for doc in docs:
|
| 29 |
-
lines.append(delimiter)
|
| 30 |
-
lines.append("\n\n")
|
| 31 |
-
for token in doc:
|
| 32 |
-
label = (
|
| 33 |
-
f"{token.ent_iob_}-{token.ent_type_}"
|
| 34 |
-
if token.ent_iob_ != "O"
|
| 35 |
-
else "O"
|
| 36 |
-
)
|
| 37 |
-
line = f"{token.text}\t{label}"
|
| 38 |
-
lines.append(line)
|
| 39 |
-
lines.append("\n")
|
| 40 |
-
lines.append("\n")
|
| 41 |
-
|
| 42 |
-
iob_file = iob_outdir / f"{spacy_file.stem}.iob"
|
| 43 |
-
with open(iob_file, "w", encoding="utf-8") as f:
|
| 44 |
-
f.writelines(lines)
|
| 45 |
-
|
| 46 |
-
msg.good(f"Saved to {iob_file}")
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
if __name__ == "__main__":
|
| 50 |
-
typer.run(spacy_to_iob)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|