lasid / lasid.py
jimregan's picture
add script
90cbeb0
raw
history blame
8.85 kB
# coding=utf-8
# Copyright 2021 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
# Copyright 2021 Phonetics and Speech Laboratory, Trinity College, Dublin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
import sys
from pathlib import Path
import datasets
try:
import icu
except ImportError:
sys.exit("ICU not found (hint: pip install pyicu)")
_DESCRIPTION = """\
Linguistic Atlas and Survey of Irish Dialects, volume 1
"""
_CITATION = """\
@book{wagner1958linguistic,
title={Linguistic Atlas and Survey of Irish Dialects: Introduction, 300 maps.},
author={Wagner, H.},
number={v. 1},
year={1958},
publisher={Dublin Institute for Advanced Studies}
}
@phdthesis{mckendry1982computer,
title={Computer-aided contributions to the study of Irish dialects},
author={McKendry, Eugene},
year={1982},
school={Queen's University Belfast}
}
@article{mckendry1998linguistic,
title={The Linguistic Atlas and Survey of Irish Dialects (LASID) and the Computer},
author={McKendry, Eugene},
journal={Studia Celtica Upsaliensia},
volume={2},
pages={345--354},
year={1998}
}
"""
_DATA_URL = "https://www3.smo.uhi.ac.uk/oduibhin/oideasra/lasid/lasid.zip"
LASID_ICU = """
\x07 β†’ ᡏ ;
\\\t β†’ ᡉ ; # \x09
\x0e β†’ α΄΅ ;
\x11 β†’ Κ° ;
\x12 β†’ ⁱ ;
\x13 β†’ α΅’ ;
\x14 β†’ α΅’Μ€ ;
\x15 β†’ Κ³ ;
\x16 β†’ Λ’ ;
\x17 β†’ αΆ΄ ;
\x18 β†’ α΅— ;
\x19 β†’ ᡘ ;
\x1a β†’ ᡘ̯ ;
\x1c β†’ α΅› ;
\x1d β†’ Κ· ;
\x1e β†’ αΆΎ ;
\x1f β†’ ᡊ ;
\# β†’ αΆ  ; # \x23
\$ β†’ α΅  ; # \x24
\% β†’ ᡍ ; # \x25
\& β†’ ᡞ ; # \x26 Λ  for IPA
\' β†’ ’ ; # \x27
\: β†’ ː ; # \x3a
\< β†’ ⁱ̈ ; # \x3c
\= β†’ ⁱ̯ ; # \x3d
\? β†’ Κ” ; # \x3f
\@ β†’ Κ² ; # \x40
E β†’ ᴇ ; # \x45
I β†’ Ιͺ ; # \x49
L β†’ ʟ ;
N β†’ Ι΄ ;
R β†’ Κ€ ;
\^ β†’ ᡐ ; # \x5e
\_ β†’ Η° ; # crane, 021 # \x5f
\` β†’ Ι›Μ€Μƒ ; # limekiln, 078: \x60
\| β†’ ⁿ ; # lamb, 055: \x7c
\~ β†’ α΅‘ ; # dreaming, 078; maybe ⁿ̠ ? # \x7e
\x7f β†’ ᴇ̃ ;
\x80 β†’ Ο† ; # ΙΈ
\x81 β†’ ΓΌ ;
\x83 β†’ Ι› \u0300 ;
\x84 β†’ eΜ€ \u0323 ; # FIXME
\\\x85 β†’ eΜ€Μƒ ; # this is οΏ½, so it needs to be escaped
\x86 β†’ uΜœΜƒ ; # lamb, 038
\x87 β†’ u̜ ; # finger-nails, 043
\x88 β†’ ʈ ; # looks like t̜ : toothache, 033
\x89 β†’ ᡃ ; # eggs, 066
\x8a β†’ Γ¨ ;
\x8b β†’ Γ― ;
\x8c β†’ Ι”ΜœΜƒ ; # grandmother, 007
\x8d β†’ Ι”Μœ ;
\x8e β†’ Ι”Μ† ; # before i go, 078
\x8f β†’ oΜœΜƒ ; # as cute, 062
\x91 β†’ Γ¦ ;
\x92 β†’ o̜ ;
\x93 β†’ Ι– ;
\x94 β†’ ΓΆ ;
\x95 β†’ Ι‘ΜœΜƒ ;
\x96 β†’ Γ» ; # milking, 067
\x97 β†’ Ι‘ \u0323 ; # FIXME (maybe Ξ±Μ© or Ι‘Μœ ?)
\x98 β†’ vΜ  ;
\x99 β†’ tΜ  ; # toothache, 021
\x9a β†’ rΜ  ;
\x9b β†’ ΓΈ ;
\x9c β†’ Ι΄Μ  ; # sick, 034
\x9d β†’ Ε‹Μ  ; # grazing, 002
\x9e β†’ nΜ  ;
\x9f β†’ lΜ  ; # plumage, 068
\xa4 β†’ kΜ  ; # plumage, 068
\xa5 β†’ gΜ  ;
\xa6 β†’ dΜ  ; # wedge, 021
\xa7 → ŭ ;
\xa8 β†’ oΜˆΜ† ;
\xa9 → ŏ ;
\xaa → ĭ ;
\xab β†’ Ι›Μ† ;
\xac → ĕ ;
\xad β†’ oΜ€ ;
\xae β†’ Ξ» ;
\xaf β†’ Ι‘ ; # Ξ± in the software
\xb0 β†’ Ι” ;
\xb1 β†’ Ι‘Μ† \u0323 ; # FIXME
\xb2 β†’ Ι™ ;
\xb4 β†’ ᡈ ; # tail, 007
\xb6 β†’ Ι‘Μ† ; # Ξ±Μ† in the software
\xb7 → ă ;
\xb8 β†’ Ξ» \u0323 ; # FIXME
\xb9 β†’ Ι› ;
\xba β†’ Κƒ \u030c ; # calling, 067
\xbb β†’ Ε‘ ;
\xbc β†’ Ε™ ;
\xbd β†’ Ι‘Μƒ ;
\xbe → ẽ ; # tied, 88N
\xc1 β†’ β€² ; # superscript prime
\xc5 β†’ ᴍ̠ ; # fart, 071
\xc6 → ã ; # calf, 046
\xc7 β†’ t \u0323 ; # probably t̞
\xc8 β†’ Ξ»Μ― ; # mane, 067
\xc9 β†’ oΜ― ; # hare, 088
\xca β†’ β±’ ; # loaf, 001
\xcb β†’ Ι« ; # loaf, 003
\xcc β†’ mΜ₯ ; # awake, 001
\xcd β†’ Κ€Μ₯ ; # thieving, 003
\xce β†’ ˈ ;
\xcf β†’ ˌ ; # cattle, 040
\xd0 β†’ Γ° ; # boar, 88N
\xd1 β†’ s \u0323 ; # FIXME # slime 008
\xd2 β†’ r \u0323 ; # FIXME # bulls 067
\xd3 β†’ ΙͺΜ† ; # suit of clothes 039
\xd4 β†’ ᴇ̀ ;
\xd5 β†’ p \u0323 ; # FIXME # castrating 053
\xd7 β†’ ΙͺΜƒ ; # slime, 007
\xd8 β†’ Ιͺ̈ ; # calf 027
\xdb β†’ o \u0323 ; # FIXME # cow 028
\xdc β†’ Ε‹ \u0323 ; # FIXME # tied 078
\xdd β†’ ö̀ ;
\xde β†’ k \u0323 ; # FIXME
\xdf β†’ i \u0323 ; # FIXME # sick 069
\xe1 β†’ g \u0323 ; # FIXME
\xe2 β†’ e \u0323 ; # FIXME
\xe3 β†’ d \u0323 ; # FIXME # agut 052
\xe4 → õ ; # I shall tie 062
\xe5 β†’ b \u0323 ; # FIXME # castrating 071
\xe6 β†’ Ι‘Μƒ \u0323 ; #FIXME # barking 049
\xe7 β†’ Ι‘ \u0323 ; # FIXME # slime 008
\xe8 → ỹ ;
\xea β†’ Ξ»Μƒ ;
\xeb β†’ uΜˆΜƒ ; # churn-dash, 011
\xec → ũ ;
\xed β†’ Ι”Μƒ ; # cow 074
\xee β†’ oΜ€Μƒ ; # barking 055
\xef β†’ β€² ;
\xf0 β†’ β€³ ;
\xf1 β†’ oΜˆΜ€Μƒ ; # dreaming, 078
\xf2 β†’ oΜˆΜƒ ; # sheep shears 074
\xf3 β†’ iΜˆΜƒ ; # churn-dash, 034
\xf4 → ĩ ; # sick 001
\xf5 β†’ Ι£Μƒ ; # tied 075
\xf6 β†’ Ι›Μƒ ; # tied 067
\xf7 β†’ nΜ₯ ; # awake, 059
\xf8 β†’ rΜ₯ ; # slime 002
\xf9 β†’ Κƒ ;
\xfb β†’ Β· ; # slime 058
\xfa β†’ Ι£ ;
\xfc β†’ Ο‡ ; # limekiln, 080
\xfd β†’ Κ’ ; # sheep shears 054
\xfe β†’ Ε‹ ;
"""
LASID_TITLES_ICU = """
\xb5 β†’ Á ;
\xd6 β†’ Í ;
\x90 β†’ Γ‰ ;
\xe0 β†’ Γ“ ;
\xe9 β†’ Ú ;
"""
def transliterator_from_rules(name, rules):
fromrules = icu.Transliterator.createFromRules(name, rules)
icu.Transliterator.registerInstance(fromrules)
return icu.Transliterator.createInstance(name)
LASID = transliterator_from_rules('lasid_icu', LASID_ICU)
TITLES = transliterator_from_rules('lasid_titles', LASID_TITLES_ICU)
def translit_phon(text):
# could have been any 8-bit encoding
return LASID.transliterate(text.decode('ISO-8859-1').rstrip())
def translit_irish(text):
return TITLES.transliterate(text.decode('ISO-8859-1').rstrip())
class LasidDataset(datasets.GeneratorBasedBuilder):
"""Scraper dataset for LASID."""
VERSION = datasets.Version("1.1.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="lasid"),
]
def _info(self):
features = datasets.Features(
{
"english": datasets.Value("string"),
"irish": datasets.Value("string"),
"map_id": datasets.Value("string"),
"place_ids": datasets.Sequence(datasets.Value("string")),
"transcripts": datasets.Sequence(datasets.Value("string")),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
supervised_keys=None,
citation=_CITATION
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
dl_path = dl_manager.download_and_extract(_DATA_URL)
infile = f"{dl_path}/mapdata.dat"
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"split": "train",
"data_file": infile
},
),
]
def _generate_examples(
self, split, data_file
):
""" Yields examples as (key, example) tuples. """
data = process_lasid(data_file)
_id = 1
for map in data.keys():
item = data[map]
place_ids = list(item["data"])
transcripts = [item["data"][a] for a in place_ids]
yield _id, {
"english": item.get("en", ""),
"irish": item.get("ga", ""),
"map_id": item.get("id", ""),
"place_ids": place_ids,
"transcripts": transcripts
}
_id += 1
def process_lasid(filename):
data = {}
cur = {}
en = ''
ga = ''
id = ''
with open(filename, "rb") as file:
for line in file.readlines():
if b'{M' in line:
if en and id:
tmp = {}
tmp['en'] = en
tmp['id'] = id
tmp['ga'] = ga
tmp['data'] = cur
data[id] = tmp
text = line.decode('ISO-8859-1').rstrip()
id = text[3:7].strip()
en = text[7:-1].strip()
cur = {}
elif b'{F' in line:
raw = translit_irish(line)
ga = raw[3:-1].strip()
elif line.decode('ISO-8859-1')[0:1].isnumeric():
pid = line.decode('ISO-8859-1')[0:3]
ptext = translit_phon(line[3:-1])
if ptext[-1] == '*':
ptext = ptext[0:-1]
cur[pid] = ptext.strip()
return data