Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions epitran/_epitran.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def trans_list(self, word: str, normpunc: bool=False, ligatures: bool=False) ->
"""
return self.ft.segs_safe(self.epi.transliterate(word, normpunc, ligatures))

def trans_delimiter(self, text: str, delimiter: str=str(' '), normpunc: bool=False, ligatures: bool=False):
def trans_delimiter(self, text: str, delimiter: str=str(' '), normpunc: bool=False, ligatures: bool=False) -> str:
"""Return IPA transliteration with a delimiter between segments

:param text str: An orthographic text
Expand All @@ -98,7 +98,7 @@ def trans_delimiter(self, text: str, delimiter: str=str(' '), normpunc: bool=Fal
return delimiter.join(self.trans_list(text, normpunc=normpunc,
ligatures=ligatures))

def xsampa_list(self, word: str, normpunc: bool=False, ligaturize: bool=False):
def xsampa_list(self, word: str, normpunc: bool=False, ligaturize: bool=False) -> "list[str]":
"""Transliterates/transcribes a word as X-SAMPA

:param word str: An orthographic word
Expand All @@ -111,7 +111,7 @@ def xsampa_list(self, word: str, normpunc: bool=False, ligaturize: bool=False):
ligaturize))
return list(map(self.xsampa.ipa2xs, ipa_segs))

def word_to_tuples(self, word: str, normpunc: bool=False, _ligaturize: bool=False):
def word_to_tuples(self, word: str, normpunc: bool=False, _ligaturize: bool=False) -> "list[tuple[str, str, str, str, list[int]]]":
"""Given a word, returns a list of tuples corresponding to IPA segments. The "feature
vectors" form a list consisting of (segment, vector) pairs.
For IPA segments, segment is a substring of phonetic_form such that the
Expand Down
9 changes: 5 additions & 4 deletions epitran/backoff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

from typing import List, Optional, Union
import regex as re
from . import _epitran
import panphon.featuretable
Expand All @@ -11,7 +12,7 @@
class Backoff(object):
"""Implements rudimentary language ID and backoff."""

def __init__(self, lang_script_codes, cedict_file=None):
def __init__(self, lang_script_codes: List[str], cedict_file: Optional[str] = None) -> None:
"""Construct a Backoff object.

Args:
Expand All @@ -28,7 +29,7 @@ def __init__(self, lang_script_codes, cedict_file=None):
self.puncnorm = PuncNorm()
self.dias = [StripDiacritics(c) for c in lang_script_codes]

def transliterate(self, token):
def transliterate(self, token: str) -> str:
"""Return IPA transliteration given by first acceptable mode.
Args:
token (unicode): orthographic text
Expand Down Expand Up @@ -60,7 +61,7 @@ def transliterate(self, token):
token = token[1:]
return ''.join(tr_list)

def trans_list(self, token):
def trans_list(self, token: str) -> List[str]:
"""Transliterate/transcribe a word into list of IPA phonemes.

Args:
Expand All @@ -71,7 +72,7 @@ def trans_list(self, token):
"""
return self.ft.segs_safe(self.transliterate(token))

def xsampa_list(self, token):
def xsampa_list(self, token: str) -> Union[str, List[str]]:
"""Transcribe a word into a list of X-SAMPA phonemes.

Args:
Expand Down
13 changes: 7 additions & 6 deletions epitran/bin/connl2engipaspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import codecs
import logging
from collections import Counter
from typing import List

import csv

Expand All @@ -14,16 +15,16 @@
logger = logging.getLogger('epitran')


def normpunc(flite, s):
def norm(c):
def normpunc(flite: epitran.flite.Flite, s: str) -> str:
def norm(c: str) -> str:
if c in flite.puncnorm:
return flite.puncnorm[c]
else:
return c
return ''.join(map(norm, s))


def add_record(flite, ft, orth):
def add_record(flite: epitran.flite.Flite, ft: panphon.FeatureTable, orth: str) -> Counter[str]:
space = Counter()
orth = normpunc(flite, orth)
trans = flite.transliterate(orth)
Expand All @@ -41,7 +42,7 @@ def add_record(flite, ft, orth):
return space


def add_file(flite, ft, fn):
def add_file(flite: epitran.flite.Flite, ft: panphon.FeatureTable, fn: str) -> Counter[str]:
space = Counter()
with codecs.open(fn, 'r', 'utf-8') as f:
for line in f:
Expand All @@ -53,15 +54,15 @@ def add_file(flite, ft, fn):
return space


def print_space(output, space):
def print_space(output: str, space: Counter[str]) -> None:
pairs = enumerate(sorted(filter(lambda x: x, space.keys())))
with open(output, 'wb') as f:
writer = csv.writer(f)
for i, char in pairs:
writer.writerow((i, char))


def main(infiles, output):
def main(infiles: List[str], output: str) -> None:
flite = epitran.flite.Flite()
ft = panphon.FeatureTable()
space = Counter()
Expand Down
15 changes: 8 additions & 7 deletions epitran/bin/connl2ipaspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import codecs
import logging
from collections import Counter
from typing import List

import epitran
import panphon
Expand All @@ -12,16 +13,16 @@
logger = logging.getLogger('epitran')


def normpunc(epi, s):
def norm(c):
def normpunc(epi: epitran.Epitran, s: str) -> str:
def norm(c: str) -> str:
if c in epi.puncnorm:
return epi.puncnorm[c]
else:
return c
return ''.join(map(norm, s))


def add_record_gen(epi, ft, orth):
def add_record_gen(epi: epitran.Epitran, ft: panphon.FeatureTable, orth: str) -> Counter[str]:
space = Counter()
orth = normpunc(epi, orth)
trans = epi.transliterate(orth)
Expand All @@ -36,7 +37,7 @@ def add_record_gen(epi, ft, orth):
return space


def add_file_gen(epi, ft, fn):
def add_file_gen(epi: epitran.Epitran, ft: panphon.FeatureTable, fn: str) -> Counter[str]:
space = Counter()
with codecs.open(fn, 'r', 'utf-8') as f:
for line in f:
Expand All @@ -48,7 +49,7 @@ def add_file_gen(epi, ft, fn):
return space


def add_file_op(epi, ft, fn):
def add_file_op(epi: epitran.Epitran, ft: panphon.FeatureTable, fn: str) -> Counter[str]:
space = Counter()
with codecs.open(fn, 'r', 'utf-8') as f:
for line in f:
Expand All @@ -71,15 +72,15 @@ def add_file_op(epi, ft, fn):
return space


def print_space(output, space):
def print_space(output: str, space: Counter[str]) -> None:
pairs = enumerate(sorted(filter(lambda x: x, space.keys())))
with open(output, 'wb') as f:
writer = csv.writer(f)
for i, char in pairs:
writer.writerow((i, char))


def main(code, op, infiles, output):
def main(code: str, op: bool, infiles: List[str], output: str) -> None:
epi = epitran.Epitran(code)
ft = panphon.FeatureTable()
space = Counter()
Expand Down
2 changes: 1 addition & 1 deletion epitran/bin/decompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys


def main(fn):
def main(fn: str) -> None:
with open(fn, encoding='utf-8') as f:
print(unicodedata.normalize('NFD', f.read()))

Expand Down
2 changes: 1 addition & 1 deletion epitran/bin/detectcaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import fileinput


def main():
def main() -> None:
for line in fileinput.input():
line = line.decode('utf-8')
token = line.strip()
Expand Down
2 changes: 1 addition & 1 deletion epitran/bin/epitranscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import argparse


def main(code):
def main(code: str) -> None:
epi = epitran.Epitran(code)
for line in sys.stdin: # pointless
line = line.decode('utf-8')
Expand Down
7 changes: 4 additions & 3 deletions epitran/bin/isbijective.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#!/usr/bin/env pythoh

import glob
from typing import List, Tuple

import csv


def read_map(fn):
def read_map(fn: str) -> List[Tuple[str, str]]:
with open(fn, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
next(reader)
return [(a, b) for [a, b] in reader]


def is_bijection(mapping):
def is_bijection(mapping: List[Tuple[str, str]]) -> bool:
a, b = zip(*mapping)
distinct_a, distinct_b = set(a), set(b)
return len(distinct_a) == len(mapping) and len(distinct_b) == len(mapping)


def main(map_fns):
def main(map_fns: List[str]) -> None:
for fn in map_fns:
mapping = read_map(fn)
is_b = is_bijection(mapping)
Expand Down
9 changes: 5 additions & 4 deletions epitran/bin/ltf2ipaspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import argparse
import glob
import os.path
from typing import List, Set

from lxml import etree
import csv
Expand All @@ -12,13 +13,13 @@
import panphon.featuretable


def read_tokens(fn):
def read_tokens(fn: str) -> List[str]:
tree = etree.parse(fn)
root = tree.getroot()
return [tok.text for tok in root.findall('.//TOKEN')]


def read_input(input_, langscript):
def read_input(input_: List[List[str]], langscript: str) -> Set[str]:
space = set()
epi = epitran.Epitran(langscript)
ft = panphon.featuretable.FeatureTable()
Expand All @@ -31,14 +32,14 @@ def read_input(input_, langscript):
return space


def write_output(output, space):
def write_output(output: str, space: Set[str]) -> None:
with open(output, 'wb') as f:
writer = csv.writer(f)
for n, ch in enumerate(sorted(list(space))):
writer.writerow((n, ch))


def main(langscript, input_, output):
def main(langscript: str, input_: List[List[str]], output: str) -> None:
space = read_input(input_, langscript)
write_output(output, space)

Expand Down
6 changes: 4 additions & 2 deletions epitran/bin/migraterules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@

import glob
import re
import io

Check failure on line 7 in epitran/bin/migraterules.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

epitran/bin/migraterules.py:7:8: F401 `io` imported but unused
from typing import List, Optional

import csv

Check failure on line 10 in epitran/bin/migraterules.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

epitran/bin/migraterules.py:10:8: F401 `csv` imported but unused


def build_rule(fields):
def build_rule(fields: List[str]) -> Optional[str]:
try:
a, b, X, Y = fields
b = "0" if not b else b
a = "0" if not a else a
return '{} -> {} / {} _ {}'.format(a, b, X, Y)
except ValueError:
print('Malformed rule: {}'.format(','.join(fields)))
return None


def main():
def main() -> None:
for csv in glob.glob('*.csv'):

Check failure on line 25 in epitran/bin/migraterules.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F402)

epitran/bin/migraterules.py:25:9: F402 Import `csv` from line 10 shadowed by loop variable
txt = re.match('[A-Za-z-]+', csv).group(0) + '.txt'
with open(csv, 'r', encoding='utf-8') as f, open(txt, 'w', encoding='utf-8') as g:
reader = csv.reader(f)
Expand Down
3 changes: 2 additions & 1 deletion epitran/bin/space2punc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import sys
import unicodedata
from typing import List
import csv


def main(fns, fnn):
def main(fns: List[str], fnn: str) -> None:
punc = set()
for fn in fns:
with open(fn, 'r', encoding='utf-8') as f:
Expand Down
3 changes: 2 additions & 1 deletion epitran/bin/testvectorgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

import argparse
import codecs
from typing import List

import epitran.vector


def main(code, space, infile):
def main(code: str, space: List[str], infile: str) -> None:
vec = epitran.vector.VectorsWithIPASpace(code, space)
with codecs.open(infile, 'r', 'utf-8') as f:
for line in f:
Expand Down
4 changes: 2 additions & 2 deletions epitran/bin/vie-tones.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}


def shuffle_tone(orth, phon):
def shuffle_tone(orth: str, phon: str) -> str:
orth = unicodedata.normalize('NFD', orth)
if re.search('[aeiouơư]', orth):
for tone in tones:
Expand All @@ -27,7 +27,7 @@ def shuffle_tone(orth, phon):
return phon


def main():
def main() -> None:
fnin = sys.argv[1]
fnout = os.path.basename(fnin)
with open(fnin) as fin, open(fnout, 'w') as fout:
Expand Down
Loading
Loading