commit dd5b8ea1d5c71462cd2bcf40353a9d0b5632969e Author: Patrick Date: Sun Jan 29 10:05:48 2023 +0900 Implementation as class diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b0f8a61 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.pytest* \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f018368 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# SoundChanger + +Applies sound changes to words. \ No newline at end of file diff --git a/__pycache__/soundchanger.cpython-310.pyc b/__pycache__/soundchanger.cpython-310.pyc new file mode 100644 index 0000000..e7e1bae Binary files /dev/null and b/__pycache__/soundchanger.cpython-310.pyc differ diff --git a/soundchanger.py b/soundchanger.py new file mode 100644 index 0000000..add1d02 --- /dev/null +++ b/soundchanger.py @@ -0,0 +1,32 @@ +import re + + +class Change: + """ + Instantiates a string representation of a sound change. + """ + + def __init__(self, change): + self.parse(change) + + def __repr__(self): + return f"{self.b}>{self.a}/{self.f}_{self.t}" + + def parse(self, change): + if '/' in change: + _change, _env = change.split('/') + else: + _change = change + _env = '_' + self.b, self.a = _change.split('>') + self.f, self.t = _env.split('_') + + if self.f.startswith("{") and self.f.endswith("}"): + self.f = self.f[1:-1].split(',') + self.f = f"[{''.join(self.f)}]" + if self.t.startswith("{") and self.t.endswith("}"): + self.t = self.t[1:-1].split(',') + self.t = f"[{''.join(self.t)}]" + + def sub(self, string): + return re.sub(f"(?<={self.f}){self.b}(?={self.t})", f"{self.a}", f"#{string}#").strip('#') diff --git a/tests/__pycache__/test_apply.cpython-310-pytest-7.1.2.pyc b/tests/__pycache__/test_apply.cpython-310-pytest-7.1.2.pyc new file mode 100644 index 0000000..21b4975 Binary files /dev/null and b/tests/__pycache__/test_apply.cpython-310-pytest-7.1.2.pyc differ diff --git a/tests/test_apply.py b/tests/test_apply.py new file mode 100644 index 0000000..e1b0042 --- /dev/null +++ b/tests/test_apply.py @@ -0,0 +1,24 @@ +from soundchanger import Change + + +def test_simple_change_without_environment(): + c = Change('p>f') + assert c.sub('pana') == 'fana' + assert c.sub('kapa') == 'kafa' + +def test_simple_change_with_environment(): + c = Change('p>f/#_a') + assert c.sub('pana') == 'fana' + assert c.sub('pune') == 'pune' + assert c.sub('kapa') == 'kapa' + +def test_complex_change_with_environment(): + c = Change('p>f/{#,a}_{a,u}') + assert c.sub('pana') == 'fana' + assert c.sub('pune') == 'fune' + assert c.sub('kapa') == 'kafa' + +# def test_simple_change_with_category(): +# c = Change('w>h/V_V') +# assert c.sub('kawa') == 'kaha' +# assert c.sub('tuwo') == 'tuho' \ No newline at end of file