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
14 changes: 8 additions & 6 deletions kalpy/aligner.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def __init__(
self.self_loop_scale = self_loop_scale
self.boost_silence = boost_silence
self.careful = careful
if self.has_multiple_lexicons:
silence_symbols = next(iter(self.lexicon_compiler.values())).silence_symbols
else:
silence_symbols = self.lexicon_compiler.silence_symbols
self.aligner = GmmAligner(
self.acoustic_model.model_path,
beam=beam,
Expand All @@ -94,6 +98,7 @@ def __init__(
acoustic_scale=acoustic_scale,
self_loop_scale=self_loop_scale,
careful=careful,
silence_phones=silence_symbols,
)
self.ali_aligner = None
if self.acoustic_model.alignment_model_path != self.acoustic_model.model_path:
Expand All @@ -105,15 +110,12 @@ def __init__(
acoustic_scale=acoustic_scale,
self_loop_scale=self_loop_scale,
careful=careful,
silence_phones=silence_symbols,
)
if self.boost_silence != 1.0:
if self.has_multiple_lexicons:
silence_symbols = next(iter(self.lexicon_compiler.values())).silence_symbols
else:
silence_symbols = self.lexicon_compiler.silence_symbols
if self.ali_aligner is not None:
self.ali_aligner.boost_silence(boost_silence, silence_symbols)
self.aligner.boost_silence(boost_silence, silence_symbols)
self.ali_aligner.boost_silence(boost_silence)
self.aligner.boost_silence(boost_silence)

def _align_utterance(
self,
Expand Down
10 changes: 4 additions & 6 deletions kalpy/data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Data classes for kalpy"""
from __future__ import annotations

import os.path
import os
import pathlib
import typing
from dataclasses import dataclass
Expand All @@ -18,16 +18,14 @@
)
from kalpy.utils import generate_read_specifier

PathLike = typing.Union[str, pathlib.Path]


@dataclass
class Segment:
"""
Data class for information about acoustic segments
"""

file_path: PathLike
file_path: os.PathLike
begin: typing.Optional[float] = 0.0
end: typing.Optional[float] = None
channel: typing.Optional[int] = 0
Expand Down Expand Up @@ -102,7 +100,7 @@ def __init__(self, *args, list_mapping: bool = False, **kwargs):
self.list_mapping = list_mapping
super().__init__(*args, **kwargs)

def export(self, file_path: typing.Union[str, pathlib.Path], skip_safe: bool = False) -> None:
def export(self, file_path: os.PathLike, skip_safe: bool = False) -> None:
with open(file_path, "w", encoding="utf8") as f:
for k in sorted(self.keys()):
v = self[k]
Expand Down Expand Up @@ -140,7 +138,7 @@ class MatrixArchive:
Path to archive or SCP file to read from
"""

def __init__(self, file_name: typing.Union[pathlib.Path, str], double: bool = False):
def __init__(self, file_name: os.PathLike, double: bool = False):
if not os.path.exists(file_name):
raise OSError(f"Specified file does not exist: {file_name}")
self.file_name = str(file_name)
Expand Down
2 changes: 1 addition & 1 deletion kalpy/decoder/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FstArchive:
Path to archive or SCP file to read from
"""

def __init__(self, file_name: typing.Union[pathlib.Path, str]):
def __init__(self, file_name: os.PathLike):
if not os.path.exists(file_name):
raise OSError(f"Specified file does not exist: {file_name}")
self.file_name = str(file_name)
Expand Down
44 changes: 22 additions & 22 deletions kalpy/decoder/decode_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class DecodeGraphCompiler:
"""
Parameters
----------
acoustic_model_path: str
acoustic_model_path: :class:`~os.PathLike`
Path to model file
tree_path: str
tree_path: :class:`~os.PathLike`
Path to tree file
lexicon_compiler: :class:`~kalpy.fstext.lexicon.LexiconCompiler`
Lexicon compiler to use in generating training graphs
Expand All @@ -58,10 +58,10 @@ class DecodeGraphCompiler:

def __init__(
self,
acoustic_model_path: typing.Union[str, pathlib.Path],
tree_path: typing.Union[str, pathlib.Path],
acoustic_model_path: os.PathLike,
tree_path: os.PathLike,
lexicon_compiler: LexiconCompiler,
arpa_path: typing.Union[str, pathlib.Path] = None,
arpa_path: typing.Optional[os.PathLike] = None,
transition_scale: float = 1.0,
self_loop_scale: float = 0.1,
):
Expand All @@ -83,8 +83,8 @@ def __init__(

def export_hclg(
self,
arpa_path: typing.Union[pathlib.Path, str],
file_name: typing.Union[pathlib.Path, str],
arpa_path: os.PathLike,
file_name: os.PathLike,
) -> None:
"""
Export HCLG.fst
Expand All @@ -103,7 +103,7 @@ def export_hclg(

def export_g(
self,
file_name: typing.Union[pathlib.Path, str],
file_name: os.PathLike,
) -> None:
"""
Export g.fst
Expand All @@ -120,7 +120,7 @@ def export_g(

def load_from_file(
self,
hclg_fst_path: typing.Union[pathlib.Path, str],
hclg_fst_path: os.PathLike,
) -> None:
"""
Read HCLG.fst from file
Expand All @@ -135,7 +135,7 @@ def load_from_file(

def load_g_from_file(
self,
g_fst_path: typing.Union[pathlib.Path, str],
g_fst_path: os.PathLike,
) -> None:
"""
Read g.fst from file
Expand All @@ -148,13 +148,13 @@ def load_g_from_file(
g_fst_path = str(g_fst_path)
self.g_fst = VectorFst.Read(g_fst_path)

def compile_g_fst(self, arpa_path: typing.Union[str, pathlib.Path]) -> VectorFst:
def compile_g_fst(self, arpa_path: os.PathLike) -> VectorFst:
"""
Compile G.fst for a language model

Parameters
----------
arpa_path: str
arpa_path: :class:`~os.PathLike`
Path to ARPA format language model

Returns
Expand All @@ -175,17 +175,17 @@ def g_carpa(self) -> ConstArpaLm:

def compile_g_carpa(
self,
arpa_path: typing.Union[str, pathlib.Path],
compiled_path: typing.Union[str, pathlib.Path],
arpa_path: os.PathLike,
compiled_path: os.PathLike,
) -> None:
"""
Compile G.carpa for a language model

Parameters
----------
arpa_path: str
arpa_path: :class:`~os.PathLike`
Path to ARPA format language model
compiled_path: str
compiled_path: :class:`~os.PathLike`
Path to compiled G.carpa
"""
compiled_path = str(compiled_path)
Expand Down Expand Up @@ -239,13 +239,13 @@ def compile_g_carpa(
else:
os.remove(temp_carpa_path)

def compile_lg_fst(self, arpa_path: typing.Union[str, pathlib.Path]) -> VectorFst:
def compile_lg_fst(self, arpa_path: os.PathLike) -> VectorFst:
"""
Compile LG.fst for a language model

Parameters
----------
arpa_path: str
arpa_path: :class:`~os.PathLike`
Path to ARPA format language model

Returns
Expand All @@ -263,14 +263,14 @@ def compile_lg_fst(self, arpa_path: typing.Union[str, pathlib.Path]) -> VectorFs
return lg_fst

def compile_clg_fst(
self, arpa_path: typing.Union[str, pathlib.Path]
self, arpa_path: os.PathLike
) -> typing.Tuple[VectorFst, typing.List[typing.List[int]]]:
"""
Compile CLG.fst for a language model

Parameters
----------
arpa_path: str
arpa_path: :class:`~os.PathLike`
Path to ARPA format language model

Returns
Expand All @@ -292,13 +292,13 @@ def compile_clg_fst(
fst_arc_sort(clg_fst, sort_type="ilabel")
return clg_fst, ilabels

def compile_hclg_fst(self, arpa_path: typing.Union[str, pathlib.Path]):
def compile_hclg_fst(self, arpa_path: os.PathLike):
"""
Compile HCLG.fst for a language model

Parameters
----------
arpa_path: str
arpa_path: :class:`~os.PathLike`
Path to ARPA format language model

Returns
Expand Down
7 changes: 4 additions & 3 deletions kalpy/decoder/training_graphs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Classes for generating training graphs"""
import logging
import os
import pathlib
import typing

Expand Down Expand Up @@ -61,8 +62,8 @@ class TrainingGraphCompiler:

def __init__(
self,
model_path: typing.Union[pathlib.Path, str],
tree_path: typing.Union[pathlib.Path, str],
model_path: os.PathLike,
tree_path: os.PathLike,
lexicon_compiler: LexiconCompiler,
transition_scale: float = 0.0,
self_loop_scale: float = 0.0,
Expand Down Expand Up @@ -131,7 +132,7 @@ def compiler(self):

def export_graphs(
self,
file_name: typing.Union[pathlib.Path, str],
file_name: os.PathLike,
transcripts: typing.Iterable[typing.Tuple[str, str]],
write_scp: bool = False,
callback: typing.Callable = None,
Expand Down
3 changes: 2 additions & 1 deletion kalpy/feat/cmvn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import logging
import os
import pathlib
import typing

Expand Down Expand Up @@ -100,7 +101,7 @@ def compute_cmvn_for_export(

def export_cmvn(
self,
file_name: typing.Union[pathlib.Path, str],
file_name: os.PathLike,
feature_archive: FeatureArchive,
spk2utt: KaldiMapping,
write_scp: bool = False,
Expand Down
14 changes: 7 additions & 7 deletions kalpy/feat/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
class FeatureArchive:
def __init__(
self,
file_name: typing.Union[pathlib.Path, str],
utt2spk: KaldiMapping = None,
cmvn_file_name: typing.Union[pathlib.Path, str] = None,
lda_mat_file_name: typing.Union[pathlib.Path, str] = None,
transform_file_name: typing.Union[pathlib.Path, str] = None,
vad_file_name: typing.Union[pathlib.Path, str] = None,
file_name: os.PathLike,
utt2spk: typing.Optional[KaldiMapping] = None,
cmvn_file_name: typing.Optional[os.PathLike] = None,
lda_mat_file_name: typing.Optional[os.PathLike] = None,
transform_file_name: typing.Optional[os.PathLike] = None,
vad_file_name: typing.Optional[os.PathLike] = None,
use_sliding_cmvn: bool = False,
cmvn_norm_vars: bool = False,
cmvn_reverse: bool = False,
Expand All @@ -35,7 +35,7 @@ def __init__(
sliding_cmvn_window: int = 300,
sliding_cmvn_center_window: bool = True,
double: bool = False,
callback: typing.Callable = None,
callback: typing.Optional[typing.Callable] = None,
):
self.cmvn_reader = None
self.transform_reader = None
Expand Down
8 changes: 4 additions & 4 deletions kalpy/feat/fmllr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

import logging
import pathlib
import os
import threading
import typing

Expand All @@ -23,8 +23,8 @@
class FmllrComputer:
def __init__(
self,
alignment_acoustic_model_path: typing.Union[pathlib.Path, str],
acoustic_model_path: typing.Union[pathlib.Path, str],
alignment_acoustic_model_path: os.PathLike,
acoustic_model_path: os.PathLike,
silence_phones: typing.List[int],
spk2utt: KaldiMapping = None,
weight_distribute: bool = False,
Expand Down Expand Up @@ -207,7 +207,7 @@ def compute_fmllr(

def export_transforms(
self,
file_name: typing.Union[pathlib.Path, str],
file_name: os.PathLike,
feature_archive: FeatureArchive,
alignment_archive: typing.Union[AlignmentArchive, LatticeArchive],
previous_transform_archive: MatrixArchive = None,
Expand Down
6 changes: 3 additions & 3 deletions kalpy/feat/lda.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

import logging
import pathlib
import os
import typing

from _kalpy import transform
Expand All @@ -20,7 +20,7 @@
class LdaStatsAccumulator:
def __init__(
self,
acoustic_model_path: typing.Union[pathlib.Path, str],
acoustic_model_path: os.PathLike,
silence_phones: typing.List[int],
weight_distribute: bool = False,
rand_prune: float = 0.0,
Expand Down Expand Up @@ -80,7 +80,7 @@ def export_transform(
class MlltStatsAccumulator:
def __init__(
self,
acoustic_model_path: typing.Union[pathlib.Path, str],
acoustic_model_path: os.PathLike,
silence_phones: typing.List[int],
weight_distribute: bool = False,
rand_prune: float = 0.0,
Expand Down
4 changes: 2 additions & 2 deletions kalpy/feat/mfcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from __future__ import annotations

import logging
import os
import pathlib
import typing

import librosa
import numpy as np

from _kalpy import feat
Expand Down Expand Up @@ -249,7 +249,7 @@ def compute_mfccs_for_export(

def export_feats(
self,
file_name: typing.Union[pathlib.Path, str],
file_name: os.PathLike,
segments: typing.Iterable[typing.Tuple[str, Segment]],
write_scp: bool = False,
compress: bool = True,
Expand Down
Loading
Loading