-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
150 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"language": "en", | ||
"generator": "test", | ||
"segments": [ | ||
{ | ||
"start": 0.0, | ||
"end": 3.0, | ||
"text": "你好" | ||
}, | ||
{ | ||
"start": 3.0, | ||
"end": 5.0, | ||
"text": "你好" | ||
}, | ||
{ | ||
"start": 6.0, | ||
"end": 9.0, | ||
"text": "好好好好好好好好好好好好好好好好好好好好好好好好" | ||
}, | ||
{ | ||
"start": 10.0, | ||
"end": 10.1, | ||
"text": "好好" | ||
}, | ||
{ | ||
"start": 12, | ||
"end": 15, | ||
"text": "这太长打发螺丝扣搭街坊拉克斯酱豆腐垃圾啊阿里山扩大飞机拉克斯基的flak涉及到了反馈啊螺丝扣搭街坊拉啊手动阀手动阀阿斯顿发射点发射点发生发射点发射点发萨看见对方" | ||
}, | ||
{ | ||
"start": 16, | ||
"end": 19, | ||
"text": "繁體的字" | ||
}, | ||
{ | ||
"start": 20, | ||
"end": 22, | ||
"text": "<unk>unk<unk>" | ||
}, | ||
{ | ||
"start": 23, | ||
"end": 25, | ||
"text": "" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,75 @@ | ||
# Copyright (C) 2023. Hao Zheng | ||
# All rights reserved. | ||
|
||
import json | ||
import os | ||
|
||
import pytest | ||
|
||
from openlrc.opt import SubtitleOptimizer | ||
from openlrc.subtitle import Subtitle | ||
|
||
|
||
@pytest.fixture | ||
def subtitle(): | ||
return Subtitle('data/test_subtitle.json') | ||
|
||
|
||
def test_merge_same(subtitle): | ||
original_len = len(subtitle) | ||
optimizer = SubtitleOptimizer(subtitle) | ||
optimizer.merge_same() | ||
assert len(optimizer.subtitle.segments) == original_len - 1 | ||
|
||
|
||
def test_merge_short(subtitle): | ||
original_len = len(subtitle) | ||
optimizer = SubtitleOptimizer(subtitle) | ||
optimizer.merge_short() | ||
assert len(optimizer.subtitle.segments) == original_len - 1 | ||
|
||
|
||
def test_merge_repeat(subtitle): | ||
optimizer = SubtitleOptimizer(subtitle) | ||
optimizer.merge_repeat() | ||
assert optimizer.subtitle.segments[2].text == '好好...' | ||
|
||
|
||
def test_cut_long(subtitle): | ||
optimizer = SubtitleOptimizer(subtitle) | ||
optimizer.cut_long(threshold=3, keep=2) | ||
assert optimizer.subtitle.segments[4].text == '这太(Cut to 2)' | ||
|
||
|
||
def test_traditional2mandarin(subtitle): | ||
optimizer = SubtitleOptimizer(subtitle) | ||
optimizer.traditional2mandarin() | ||
assert optimizer.subtitle.segments[5].text == '繁体的字' | ||
|
||
|
||
def test_remove_unk(subtitle): | ||
optimizer = SubtitleOptimizer(subtitle) | ||
optimizer.remove_unk() | ||
assert optimizer.subtitle.segments[6].text == 'unk' | ||
|
||
|
||
def test_remove_empty(subtitle): | ||
original_len = len(subtitle) | ||
optimizer = SubtitleOptimizer(subtitle) | ||
optimizer.remove_empty() | ||
assert len(optimizer.subtitle.segments) == original_len - 1 | ||
|
||
|
||
def test_save(subtitle): | ||
optimizer = SubtitleOptimizer(subtitle) | ||
optimizer.perform_all() | ||
optimizer.save(output_name='test_subtitle_optimized.json') | ||
|
||
with open('test_subtitle_optimized.json', 'r', encoding='utf-8') as f: | ||
optimized_subtitle = json.load(f) | ||
|
||
assert optimized_subtitle['language'] == 'en' | ||
assert optimized_subtitle['generator'] == 'test' | ||
assert len(optimized_subtitle['segments']) == 5 | ||
|
||
os.remove('test_subtitle_optimized.json') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters