Skip to content

Commit

Permalink
Merge pull request #357 from roedoejet/dev.misc-fixes
Browse files Browse the repository at this point in the history
Improve coverage and type checking slightly
  • Loading branch information
dhdaines authored Apr 4, 2024
2 parents f50768e + ff7de51 commit 010d621
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 19 deletions.
4 changes: 2 additions & 2 deletions g2p/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def make_g2p( # noqa: C901
NoPath: if there is path between in_lang and out_lang
"""
# Defer expensive imports
from networkx import shortest_path
from networkx.exception import NetworkXNoPath
from networkx import shortest_path # type: ignore
from networkx.exception import NetworkXNoPath # type: ignore

from g2p.log import LOGGER
from g2p.mappings import Mapping
Expand Down
6 changes: 3 additions & 3 deletions g2p/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

from flask import Blueprint, abort
from flask_cors import CORS
from flask_restful import Api, Resource, inputs, reqparse
from networkx.algorithms.dag import ancestors, descendants
from networkx.exception import NetworkXError
from flask_restful import Api, Resource, inputs, reqparse # type: ignore
from networkx.algorithms.dag import ancestors, descendants # type: ignore
from networkx.exception import NetworkXError # type: ignore

from g2p import make_g2p
from g2p.exceptions import InvalidLanguageCode, NoPath
Expand Down
4 changes: 2 additions & 2 deletions g2p/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from flask import Flask, render_template
from flask_cors import CORS
from flask_socketio import SocketIO, emit
from networkx import shortest_path
from flask_socketio import SocketIO, emit # type: ignore
from networkx import shortest_path # type: ignore

from g2p import make_g2p
from g2p.api import g2p_api
Expand Down
2 changes: 1 addition & 1 deletion g2p/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def convert( # noqa: C901
fra->fra-ipa, fra-ipa->eng-ipa and eng-ipa->eng-arpabet.
"""
# Defer expensive imports
from networkx import has_path
from networkx import has_path # type: ignore

from g2p.log import LOGGER
from g2p.mappings import MAPPINGS_AVAILABLE, Mapping, MappingConfig
Expand Down
2 changes: 1 addition & 1 deletion g2p/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import logging

import coloredlogs
import coloredlogs # type: ignore

FIELD_STYLES = dict(
levelname=dict(color="green"),
Expand Down
2 changes: 1 addition & 1 deletion g2p/mappings/create_fallback_mapping.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime as dt

from text_unidecode import unidecode
from text_unidecode import unidecode # type: ignore

from g2p import make_g2p
from g2p.log import LOGGER
Expand Down
2 changes: 1 addition & 1 deletion g2p/mappings/create_ipa_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def process_character(p, is_xsampa=False):
global _xsampa_converter
if _xsampa_converter is None:
# Expensive import, do it only when needed:
from panphon.xsampa import XSampa
from panphon.xsampa import XSampa # type: ignore

_xsampa_converter = XSampa()
p = _xsampa_converter.convert(p)
Expand Down
2 changes: 1 addition & 1 deletion g2p/mappings/langs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
import os

import networkx
import networkx # type: ignore

from g2p.constants import LANGS_DIR, LANGS_FILE_NAME, NETWORK_FILE_NAME
from g2p.log import LOGGER
Expand Down
7 changes: 4 additions & 3 deletions g2p/mappings/langs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from pathlib import Path
from typing import Any, Optional

import networkx
from networkx.algorithms.dag import ancestors, descendants
import networkx # type: ignore
from networkx.algorithms.dag import ancestors, descendants # type: ignore

from g2p.log import LOGGER
from g2p.mappings import MAPPINGS_AVAILABLE, Mapping, MappingConfig
Expand All @@ -32,7 +32,8 @@
def getPanphonDistanceSingleton():
global _PANPHON_DISTANCE_SINGLETON
if _PANPHON_DISTANCE_SINGLETON is None:
import panphon.distance # Expensive import, only do it when actually needed
# Expensive import, only do it when actually needed
import panphon.distance # type: ignore

_PANPHON_DISTANCE_SINGLETON = panphon.distance.Distance()
return _PANPHON_DISTANCE_SINGLETON
Expand Down
2 changes: 1 addition & 1 deletion g2p/mappings/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import re
from typing import List

from networkx.exception import NetworkXError
from networkx.exception import NetworkXError # type: ignore

from g2p.exceptions import MappingMissing
from g2p.log import LOGGER
Expand Down
32 changes: 32 additions & 0 deletions g2p/tests/test_create_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,31 @@ def setUp(self):
{"in": "ɡ", "out": "G"},
{"in": "ʒ", "out": "ZH"},
]
self.mappings_xsampa = [
{"in": "A", "out": "AA"},
{"in": "e:", "out": "EY"},
{"in": "i", "out": "IY"},
{"in": "u", "out": "UW"},
{"in": "tS", "out": "CH"},
{"in": "p", "out": "P"},
{"in": "t", "out": "T"},
{"in": "k", "out": "K"},
{"in": "w", "out": "W"},
{"in": "g", "out": "G"},
{"in": "Z", "out": "ZH"},
]
self.target_mapping = Mapping(
rules=self.mappings,
in_lang="eng-ipa",
out_lang="eng-arpabet",
out_delimiter=" ",
)
self.target_mapping_xsampa = Mapping(
rules=self.mappings_xsampa,
in_lang="eng-xsampa",
out_lang="eng-arpabet",
out_delimiter=" ",
)

def test_unigram_mappings(self):
src_mappings = [
Expand Down Expand Up @@ -79,6 +98,19 @@ def test_trigram_mappings(self):
self.assertEqual(transducer("t͡ʃu").output_string, "tʃu")
self.assertEqual(transducer("t͡ʃa").output_string, "tʃɑ")

def test_trigram_mappings_xsampa(self):
src_mappings = [
{"in": "ᒋ", "out": "tSi"},
{"in": "ᒍ", "out": "tSu"},
{"in": "ᒐ", "out": "tSa"},
]
src_mapping = Mapping(rules=src_mappings, in_lang="crj", out_lang="crj-xsampa")
mapping = create_mapping(src_mapping, self.target_mapping_xsampa, quiet=True)
transducer = Transducer(mapping)
self.assertEqual(transducer("tSi").output_string, "tSi")
self.assertEqual(transducer("tSu").output_string, "tSu")
self.assertEqual(transducer("tSa").output_string, "tSA")

def test_long_mappings(self):
src_mappings = [
{"in": "ᐧᐯ", "out": "pʷeː"},
Expand Down
4 changes: 2 additions & 2 deletions g2p/tests/time_panphon.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"""

import panphon.distance
from linetimer import CodeTimer
import panphon.distance # type: ignore
from linetimer import CodeTimer # type: ignore

from g2p.mappings import Mapping
from g2p.mappings.langs.utils import is_panphon
Expand Down
2 changes: 1 addition & 1 deletion g2p/transducer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from collections import OrderedDict, defaultdict
from typing import DefaultDict, Dict, List, Optional, Set, Tuple, Union

import text_unidecode
import text_unidecode # type: ignore

from g2p.log import LOGGER
from g2p.mappings import MAPPING_TYPE, Mapping
Expand Down

0 comments on commit 010d621

Please sign in to comment.