-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mouthing): add mouthing generation
- Loading branch information
Showing
7 changed files
with
216 additions
and
34 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
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
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,82 @@ | ||
import functools | ||
import json | ||
import re | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
from epitran import Epitran | ||
from signwriting.formats.sign_to_fsw import sign_to_fsw | ||
|
||
from signwriting.formats.fsw_to_sign import fsw_to_sign | ||
|
||
from signwriting.utils.join_signs import join_signs_horizontal, sign_from_symbols | ||
|
||
MOUTHING_INDEX = Path(__file__).parent / "mouthing.json" | ||
|
||
|
||
@functools.lru_cache() | ||
def get_mouthings(): | ||
with open(MOUTHING_INDEX, "r", encoding="utf-8") as f: | ||
mouthings = json.load(f) | ||
|
||
for info in list(mouthings.values()): | ||
if "alternatives" in info: | ||
for alternative in info["alternatives"]: | ||
mouthings[alternative] = info | ||
|
||
return mouthings | ||
|
||
|
||
@functools.lru_cache() | ||
def get_mouthings_without_aspiration(): | ||
mouthings = get_mouthings() | ||
|
||
for info in mouthings.values(): | ||
if "S335" in info["writing"]: | ||
info["writing"] = re.sub(r"S335..\d{3}x\d{3}", "", info["writing"]) | ||
sign = fsw_to_sign(info["writing"]) | ||
sign = sign_from_symbols(sign["symbols"]) | ||
info["writing"] = sign_to_fsw(sign) | ||
|
||
return mouthings | ||
|
||
|
||
def mouth_ipa_single(word: str, aspiration=False) -> Union[str, None]: | ||
mouthings = get_mouthings() if aspiration else get_mouthings_without_aspiration() | ||
|
||
# Make sure to look at long symbols first | ||
mouthings = sorted(list(mouthings.items()), key=lambda x: len(x[0]), reverse=True) | ||
|
||
sl = [] | ||
caret = 0 | ||
while caret < len(word): | ||
found = False | ||
for symbol, info in mouthings: | ||
if word[caret:caret + len(symbol)].lower() == symbol: | ||
sl.append(info["writing"]) | ||
caret += len(symbol) | ||
found = True | ||
break | ||
if not found: | ||
print(f"Symbol not found: {word[caret:caret + 1]}") | ||
return None | ||
return join_signs_horizontal(*sl, spacing=-10) | ||
|
||
|
||
def mouth_ipa(characters: str, aspiration=False) -> Union[str, None]: | ||
words = [mouth_ipa_single(word, aspiration=aspiration) for word in characters.split(" ")] | ||
if any(word is None for word in words): | ||
return None | ||
|
||
return join_signs_horizontal(*words, spacing=10) | ||
|
||
|
||
def mouth(word: str, language: str, aspiration=False): | ||
epi = Epitran(language, ligatures=True) | ||
ipa = epi.transliterate(word) | ||
return mouth_ipa(ipa, aspiration=aspiration) | ||
|
||
|
||
if __name__ == "__main__": | ||
for _word in ["hello", "Amit", "high", "sign writing", "SignWriting"]: | ||
print(_word, mouth(_word, language='eng-Latn')) |
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,33 +1,87 @@ | ||
from collections import namedtuple | ||
|
||
from signwriting.formats.fsw_to_sign import fsw_to_sign | ||
from signwriting.formats.sign_to_fsw import sign_to_fsw | ||
from signwriting.types import Sign | ||
from signwriting.types import Sign, SignSymbol | ||
from signwriting.visualizer.visualize import get_symbol_size | ||
|
||
|
||
def all_ys(_sign): | ||
return [s["position"][1] for s in _sign["symbols"]] | ||
def all_axis(_sign, axis): | ||
axis_index = 0 if axis == "x" else 1 | ||
return [s["position"][axis_index] for s in _sign["symbols"]] | ||
|
||
|
||
def join_signs(*fsws: str, spacing: int = 0): | ||
def init_join(*fsws: str): | ||
signs = [fsw_to_sign(fsw) for fsw in fsws] | ||
new_sign: Sign = {"box": {"symbol": "M", "position": (500, 500)}, "symbols": []} | ||
return [sign for sign in signs if len(sign["symbols"]) > 0] | ||
|
||
|
||
def join_signs_vertical(*fsws: str, spacing: int = 0): | ||
signs = init_join(*fsws) | ||
symbols = [] | ||
accumulative_offset = 0 | ||
|
||
for sign in signs: | ||
sign_min_y = min(all_ys(sign)) | ||
sign_min_y = min(all_axis(sign, "y")) | ||
sign_offset_y = accumulative_offset + spacing - sign_min_y | ||
accumulative_offset += (sign["box"]["position"][1] - sign_min_y) + spacing # * 2 | ||
|
||
new_sign["symbols"] += [{ | ||
"symbol": s["symbol"], | ||
"position": (s["position"][0], s["position"][1] + sign_offset_y) | ||
} for s in sign["symbols"]] | ||
for symbol in sign["symbols"]: | ||
symbols.append({ | ||
"symbol": symbol["symbol"], | ||
"position": (symbol["position"][0], symbol["position"][1] + sign_offset_y) | ||
}) | ||
|
||
new_sign = sign_from_symbols(symbols, fix_x=False) | ||
return sign_to_fsw(new_sign) | ||
|
||
|
||
def join_signs_horizontal(*fsws: str, spacing: int = 0): | ||
signs = init_join(*fsws) | ||
symbols = [] | ||
accumulative_offset = 0 | ||
|
||
# Recenter around box center | ||
sign_middle = max(all_ys(new_sign)) // 2 | ||
for sign in signs: | ||
sign_min_x = min(all_axis(sign, "x")) | ||
sign_offset_x = accumulative_offset + spacing - sign_min_x | ||
accumulative_offset += (sign["box"]["position"][0] - sign_min_x) + spacing # * 2 | ||
|
||
for symbol in new_sign["symbols"]: | ||
symbol["position"] = (symbol["position"][0], | ||
new_sign["box"]["position"][1] - sign_middle + symbol["position"][1]) | ||
for symbol in sign["symbols"]: | ||
symbols.append({ | ||
"symbol": symbol["symbol"], | ||
"position": (symbol["position"][0] + sign_offset_x, symbol["position"][1]) | ||
}) | ||
|
||
new_sign = sign_from_symbols(symbols, fix_y=False) | ||
return sign_to_fsw(new_sign) | ||
|
||
|
||
Point = namedtuple("Point", ["x", "y"]) | ||
|
||
|
||
def sign_from_symbols(symbols: list[SignSymbol], fix_x=True, fix_y=True) -> Sign: | ||
min_p = Point(x=999, y=999) | ||
max_p = Point(x=0, y=0) | ||
for symbol in symbols: | ||
min_p = Point(x=min(min_p.x, symbol["position"][0]), | ||
y=min(min_p.y, symbol["position"][1])) | ||
|
||
symbol_width, symbol_height = get_symbol_size(symbol["symbol"]) | ||
max_p = Point(x=max(max_p.x, symbol["position"][0] + symbol_width), | ||
y=max(max_p.y, symbol["position"][1] + symbol_height)) | ||
|
||
box_p = Point(x=500 + (max_p.x - min_p.x) // 2, | ||
y=500 + (max_p.y - min_p.y) // 2) | ||
box = {"symbol": "M", "position": box_p} | ||
size = Point(x=max_p.x - min_p.x, | ||
y=max_p.y - min_p.y) | ||
|
||
for symbol in symbols: | ||
symbol_x, symbol_y = symbol["position"] | ||
if fix_x: | ||
symbol_x += box_p.x - min_p.x - size.x | ||
if fix_y: | ||
symbol_y += box_p.y - min_p.y - size.y | ||
symbol["position"] = (symbol_x, symbol_y) | ||
|
||
return {"box": box, "symbols": symbols} |
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,30 +1,34 @@ | ||
import unittest | ||
|
||
from signwriting.utils.join_signs import join_signs | ||
from signwriting.utils.join_signs import join_signs_vertical | ||
|
||
|
||
class JoinSignsCase(unittest.TestCase): | ||
|
||
def test_join_two_characters(self): | ||
char_a = 'M507x507S1f720487x492' | ||
char_b = 'M507x507S14720493x485' | ||
result_sign = join_signs(char_a, char_b) | ||
self.assertEqual(result_sign, 'M500x500S1f720487x493S14720493x508') | ||
result_sign = join_signs_vertical(char_a, char_b) | ||
self.assertEqual('M510x518S1f720487x481S14720493x496', result_sign) | ||
|
||
def test_join_alphabet_characters(self): | ||
chars = [ | ||
"M510x508S1f720490x493", "M507x511S14720493x489", "M509x510S16d20492x490", "M508x515S10120492x485", | ||
"M508x508S14a20493x493", "M511x515S1ce20489x485", "M515x508S1f000486x493", "M515x508S11502485x493", | ||
"M511x510S19220490x491", "M519x518S19220498x499S2a20c482x483" | ||
] | ||
result_sign = join_signs(*chars, spacing=10) | ||
result_sign = join_signs_vertical(*chars, spacing=10) | ||
# pylint: disable=line-too-long | ||
self.assertEqual( | ||
result_sign, | ||
'M500x500S1f720490x362S14720493x387S16d20492x419S10120492x449S14a20493x489S1ce20489x514S1f000486x554S11502485x579S19220490x604S19220498x649S2a20c482x633' | ||
# noqa: E501 | ||
'M518x653S1f720490x347S14720493x372S16d20492x404S10120492x434S14a20493x474S1ce20489x499S1f000486x539S11502485x564S19220490x589S19220498x634S2a20c482x618', | ||
result_sign | ||
) | ||
|
||
def test_join_empty_sign(self): | ||
char_a = 'M507x507S1f720487x492' | ||
char_b = 'M507x507' | ||
result_sign = join_signs_vertical(char_a, char_b) | ||
self.assertEqual('M510x507S1f720487x492', result_sign) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |