Skip to content

Commit 998d02d

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 511911a commit 998d02d

8 files changed

Lines changed: 41 additions & 48 deletions

File tree

MANIFEST.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
include ctc_segmentation/ctc_segmentation_dyn.pyx
2-

Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ clean:
1111

1212
upload:
1313
twine upload dist/*
14-
14+
1515
test:
1616
cd tests; python -c "import test_ctc_segmentation as test; test.test_ctc_segmentation()"
1717
cd tests; python -c "import test_ctc_segmentation as test; test.test_determine_utterance_segments()"
@@ -23,7 +23,7 @@ test:
2323
# To test the various installation methods:
2424
github:
2525
cd /; pip install git+https://github.com/lumaku/ctc-segmentation --user
26-
26+
2727
pip:
2828
cd /; pip install ctc-segmentation --user
2929

@@ -32,4 +32,3 @@ local:
3232

3333
rm:
3434
cd /; pip uninstall -y ctc-segmentation
35-

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,29 @@ def align_with_transcript(
5959
with torch.no_grad():
6060
logits = model(inputs.input_values).logits.cpu()[0]
6161
probs = torch.nn.functional.softmax(logits,dim=-1)
62-
62+
6363
# Tokenize transcripts
6464
vocab = tokenizer.get_vocab()
6565
inv_vocab = {v:k for k,v in vocab.items()}
6666
unk_id = vocab["<unk>"]
67-
67+
6868
tokens = []
6969
for transcript in transcripts:
7070
assert len(transcript) > 0
7171
tok_ids = tokenizer(transcript.replace("\n"," ").lower())['input_ids']
7272
tok_ids = np.array(tok_ids,dtype=np.int)
7373
tokens.append(tok_ids[tok_ids != unk_id])
74-
74+
7575
# Align
7676
char_list = [inv_vocab[i] for i in range(len(inv_vocab))]
7777
config = ctc_segmentation.CtcSegmentationParameters(char_list=char_list)
7878
config.index_duration = audio.shape[0] / probs.size()[0] / samplerate
79-
79+
8080
ground_truth_mat, utt_begin_indices = ctc_segmentation.prepare_token_list(config, tokens)
8181
timings, char_probs, state_list = ctc_segmentation.ctc_segmentation(config, probs.numpy(), ground_truth_mat)
8282
segments = ctc_segmentation.determine_utterance_segments(config, utt_begin_indices, char_probs, timings, transcripts)
8383
return [{"text" : t, "start" : p[0], "end" : p[1], "conf" : p[2]} for t,p in zip(transcripts, segments)]
84-
84+
8585
def get_word_timestamps(
8686
audio : np.ndarray,
8787
samplerate : int = SAMPLERATE,
@@ -95,38 +95,38 @@ def get_word_timestamps(
9595
with torch.no_grad():
9696
logits = model(inputs.input_values).logits.cpu()[0]
9797
probs = torch.nn.functional.softmax(logits,dim=-1)
98-
98+
9999
predicted_ids = torch.argmax(logits, dim=-1)
100100
pred_transcript = processor.decode(predicted_ids)
101-
101+
102102
# Split the transcription into words
103103
words = pred_transcript.split(" ")
104-
104+
105105
# Align
106106
vocab = tokenizer.get_vocab()
107107
inv_vocab = {v:k for k,v in vocab.items()}
108108
char_list = [inv_vocab[i] for i in range(len(inv_vocab))]
109109
config = ctc_segmentation.CtcSegmentationParameters(char_list=char_list)
110110
config.index_duration = audio.shape[0] / probs.size()[0] / samplerate
111-
111+
112112
ground_truth_mat, utt_begin_indices = ctc_segmentation.prepare_text(config, words)
113113
timings, char_probs, state_list = ctc_segmentation.ctc_segmentation(config, probs.numpy(), ground_truth_mat)
114114
segments = ctc_segmentation.determine_utterance_segments(config, utt_begin_indices, char_probs, timings, words)
115115
return [{"text" : w, "start" : p[0], "end" : p[1], "conf" : p[2]} for w,p in zip(words, segments)]
116116

117117
print(align_with_transcript(audio,transcripts))
118-
# [{'text': 'A MAN SAID TO THE UNIVERSE', 'start': 0.08124999999999993, 'end': 2.034375, 'conf': 0.0},
118+
# [{'text': 'A MAN SAID TO THE UNIVERSE', 'start': 0.08124999999999993, 'end': 2.034375, 'conf': 0.0},
119119
# {'text': 'SIR I EXIST', 'start': 2.3260775862068965, 'end': 4.078771551724138, 'conf': 0.0}]
120120

121121
print(get_word_timestamps(audio))
122-
# [{'text': 'a', 'start': 0.08124999999999993, 'end': 0.5912715517241378, 'conf': 0.9999501323699951},
123-
# {'text': 'man', 'start': 0.5912715517241378, 'end': 0.9219827586206896, 'conf': 0.9409108982174931},
124-
# {'text': 'said', 'start': 0.9219827586206896, 'end': 1.2326508620689656, 'conf': 0.7700278702302796},
125-
# {'text': 'to', 'start': 1.2326508620689656, 'end': 1.3529094827586206, 'conf': 0.5094435178226225},
126-
# {'text': 'the', 'start': 1.3529094827586206, 'end': 1.4831896551724135, 'conf': 0.4580493446392211},
127-
# {'text': 'universe', 'start': 1.4831896551724135, 'end': 2.034375, 'conf': 0.9285054256219009},
128-
# {'text': 'sir', 'start': 2.3260775862068965, 'end': 3.036530172413793, 'conf': 0.0},
129-
# {'text': 'i', 'start': 3.036530172413793, 'end': 3.347198275862069, 'conf': 0.7995760873559864},
122+
# [{'text': 'a', 'start': 0.08124999999999993, 'end': 0.5912715517241378, 'conf': 0.9999501323699951},
123+
# {'text': 'man', 'start': 0.5912715517241378, 'end': 0.9219827586206896, 'conf': 0.9409108982174931},
124+
# {'text': 'said', 'start': 0.9219827586206896, 'end': 1.2326508620689656, 'conf': 0.7700278702302796},
125+
# {'text': 'to', 'start': 1.2326508620689656, 'end': 1.3529094827586206, 'conf': 0.5094435178226225},
126+
# {'text': 'the', 'start': 1.3529094827586206, 'end': 1.4831896551724135, 'conf': 0.4580493446392211},
127+
# {'text': 'universe', 'start': 1.4831896551724135, 'end': 2.034375, 'conf': 0.9285054256219009},
128+
# {'text': 'sir', 'start': 2.3260775862068965, 'end': 3.036530172413793, 'conf': 0.0},
129+
# {'text': 'i', 'start': 3.036530172413793, 'end': 3.347198275862069, 'conf': 0.7995760873559864},
130130
# {'text': 'exist', 'start': 3.347198275862069, 'end': 4.078771551724138, 'conf': 0.0}]
131131
```
132132

@@ -229,7 +229,7 @@ For examples, see the `prepare_*` functions in `ctc_segmentation.py`, or the exa
229229

230230
### Segments clean-up
231231

232-
Segments that were written to a `segments` file can be filtered using the confidence score. This is the minium confidence score in log space as described in the paper.
232+
Segments that were written to a `segments` file can be filtered using the confidence score. This is the minium confidence score in log space as described in the paper.
233233

234234
Utterances with a low confidence score are discarded in a data clean-up. This parameter may need adjustment depending on dataset, ASR model and used text conversion.
235235

ctc_segmentation/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""Import all functions of the CTC segmentation package."""
2-
from .ctc_segmentation import ctc_segmentation
3-
from .ctc_segmentation import CtcSegmentationParameters
4-
from .ctc_segmentation import determine_utterance_segments
5-
from .ctc_segmentation import prepare_text
6-
from .ctc_segmentation import prepare_tokenized_text
7-
from .ctc_segmentation import prepare_token_list
2+
3+
from .ctc_segmentation import (CtcSegmentationParameters, ctc_segmentation,
4+
determine_utterance_segments, prepare_text,
5+
prepare_token_list, prepare_tokenized_text)
86
from .partitioning import get_partitions

ctc_segmentation/ctc_segmentation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""
1919

2020
import logging
21+
2122
import numpy as np
2223

2324
logger = logging.getLogger("ctc_segmentation")

ctc_segmentation/ctc_segmentation_dyn.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ For a description, see https://arxiv.org/abs/2007.09127
1313
"""
1414

1515
import logging
16+
1617
import numpy as np
18+
1719
cimport numpy as np
1820

1921

setup.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
from setuptools import setup, find_packages, Extension
2-
from setuptools.command.build_ext import build_ext
31
import numpy
4-
2+
from setuptools import Extension, find_packages, setup
3+
from setuptools.command.build_ext import build_ext
54

65
try:
76
from Cython.Build import cythonize
7+
88
USE_CYTHON = True
99
except ImportError:
1010
USE_CYTHON = False
1111

1212
# https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html
13-
ext = '.pyx' if USE_CYTHON else '.c'
13+
ext = ".pyx" if USE_CYTHON else ".c"
1414
extensions = [
1515
Extension(
1616
name="ctc_segmentation.ctc_segmentation_dyn",
17-
sources=["ctc_segmentation/ctc_segmentation_dyn"+ext],
17+
sources=["ctc_segmentation/ctc_segmentation_dyn" + ext],
1818
include_dirs=[numpy.get_include()],
1919
)
2020
]
2121
if USE_CYTHON:
2222
from Cython.Build import cythonize
23+
2324
extensions = cythonize(extensions)
2425

2526
package_information = """
@@ -35,22 +36,18 @@
3536
setup(
3637
name="ctc_segmentation",
3738
version="1.7.5",
38-
39-
python_requires='>=3.9',
39+
python_requires=">=3.9",
4040
packages=find_packages(exclude=["tests"]),
4141
setup_requires=["numpy"],
4242
install_requires=["setuptools", "numpy", "Cython"],
4343
tests_require=["pytest", "torch"],
4444
zip_safe=False,
4545
ext_modules=extensions,
46-
cmdclass={'build_ext': build_ext},
47-
46+
cmdclass={"build_ext": build_ext},
4847
author="Ludwig Kuerzinger <ludwig.kuerzinger@tum.de>, "
49-
"Dominik Winkelbauer <dominik.winkelbauer@tum.de>",
50-
description="CTC segmentation to align utterances within "
51-
"large audio files.",
48+
"Dominik Winkelbauer <dominik.winkelbauer@tum.de>",
49+
description="CTC segmentation to align utterances within " "large audio files.",
5250
url="https://github.com/espnet/ctc-segmentation",
53-
5451
long_description_content_type="text/markdown",
5552
long_description=package_information,
5653
)

tests/test_ctc_segmentation.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
"""Test functions for CTC segmentation."""
88
import numpy as np
99

10-
from ctc_segmentation import ctc_segmentation
11-
from ctc_segmentation import CtcSegmentationParameters
12-
from ctc_segmentation import determine_utterance_segments
13-
from ctc_segmentation import prepare_text
14-
from ctc_segmentation import prepare_tokenized_text
15-
from ctc_segmentation import prepare_token_list
10+
from ctc_segmentation import (CtcSegmentationParameters, ctc_segmentation,
11+
determine_utterance_segments, prepare_text,
12+
prepare_token_list, prepare_tokenized_text)
1613

1714

1815
def test_ctcsegmentationparameters():

0 commit comments

Comments
 (0)