Added docstring to apply function
This commit is contained in:
parent
5956894770
commit
38e3661dc5
@ -1,18 +1,40 @@
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
def apply(changes, strings, categories={}, ignore_errors=True, apply=True, zero_characters=['∅']):
|
def apply(
|
||||||
|
changes,
|
||||||
|
strings,
|
||||||
|
apply=True,
|
||||||
|
categories={},
|
||||||
|
ignore_errors=True,
|
||||||
|
zero_characters=['∅']):
|
||||||
"""
|
"""
|
||||||
Applies a sound change or a list of sound changes to a string or
|
Applies a sound change or a list of sound changes to a string or
|
||||||
a list of given strings.
|
a list of given strings.
|
||||||
|
|
||||||
|
Accepts inputs of type str or list.
|
||||||
|
If the input value is of type str, the output will also be of type str.
|
||||||
|
|
||||||
|
Options:
|
||||||
|
- apply (default: True)
|
||||||
|
Whether or not the changes should be applied.
|
||||||
|
- categories (default: {})
|
||||||
|
Which categories will be detected.
|
||||||
|
For vowels it would be {'V'='aeiou'} or {'V'=['a', 'e', 'i', 'o', 'u']})
|
||||||
|
- ignore_errors (default: True)
|
||||||
|
If this option is set to `True`, any erroneous sound change will be skipped.
|
||||||
|
If set to `False`, a ValueError will be raised.
|
||||||
|
- zero_characters (default: ['∅'])
|
||||||
|
These characters will be removed in the changed words.
|
||||||
|
For example, `apply('h>∅', 'aha')` will return 'aa', not 'a∅a'.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not apply:
|
if not apply:
|
||||||
return strings
|
return strings
|
||||||
|
|
||||||
if isinstance(changes, str):
|
if isinstance(changes, str):
|
||||||
changes = [changes]
|
changes = [changes]
|
||||||
|
|
||||||
return_str = isinstance(strings, str)
|
return_str = isinstance(strings, str)
|
||||||
if isinstance(strings, str):
|
if isinstance(strings, str):
|
||||||
strings = [strings]
|
strings = [strings]
|
||||||
@ -20,7 +42,6 @@ def apply(changes, strings, categories={}, ignore_errors=True, apply=True, zero_
|
|||||||
strings = strings.copy()
|
strings = strings.copy()
|
||||||
|
|
||||||
for change in changes:
|
for change in changes:
|
||||||
|
|
||||||
if validate_change(change, ignore_errors=ignore_errors) == False:
|
if validate_change(change, ignore_errors=ignore_errors) == False:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -33,7 +54,7 @@ def apply(changes, strings, categories={}, ignore_errors=True, apply=True, zero_
|
|||||||
|
|
||||||
for i, string in enumerate(strings):
|
for i, string in enumerate(strings):
|
||||||
strings[i] = re.sub(pattern, replacement, f"#{string}#").strip('#')
|
strings[i] = re.sub(pattern, replacement, f"#{string}#").strip('#')
|
||||||
|
|
||||||
if return_str:
|
if return_str:
|
||||||
return strings[0]
|
return strings[0]
|
||||||
return strings
|
return strings
|
||||||
@ -59,7 +80,7 @@ def convert_change_to_regex(change, categories, zero_characters):
|
|||||||
change = re.sub(f'((?<=,){k}|{k}(?=,))', '|'.join(v), change)
|
change = re.sub(f'((?<=,){k}|{k}(?=,))', '|'.join(v), change)
|
||||||
# Replacements of categories anywhere else
|
# Replacements of categories anywhere else
|
||||||
change = change.replace(k, '('+'|'.join(v)+')')
|
change = change.replace(k, '('+'|'.join(v)+')')
|
||||||
|
|
||||||
# Remove zero characters
|
# Remove zero characters
|
||||||
for char in zero_characters:
|
for char in zero_characters:
|
||||||
change = change.replace(char, '')
|
change = change.replace(char, '')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user