-
Notifications
You must be signed in to change notification settings - Fork 0
/
processing.py
76 lines (62 loc) · 1.71 KB
/
processing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import re
from typing import List
def split_syllables(sentence: str) -> List[str]:
"""
Splits a target sentence into syllables.
For now, this is a simple implementation that works
for Rapanui and Japanese.
Parameters
----------
sentence : str
The sentence to split.
Returns
-------
List[str]
A list of syllables.
"""
res = []
words = sentence.split()
for word in words:
word = re.sub(r'([aeiou])', r'\1.', word)
word = re.sub(r'n(?![aeiou])', r'n.', word)
word = re.sub(r'([ptk])([ptk])', r'\1.\2', word)
word = word.split('.')
syllables = [syl for syl in word if syl]
res += syllables
return res
def get_top_symbols(corpus: List[List[str]], n: int, ignore: List[str] = list()) -> List[str]:
"""
Gets the top n symbols from a corpus.
Parameters
----------
corpus : List[List[str]]
The corpus to get the symbols from.
n : int
The number of symbols to get.
ignore : List[str]
A list of symbols to ignore.
Returns
-------
List[str]
A list of the top n symbols.
"""
symbols = {}
for line in corpus:
for symbol in line:
symbols[symbol] = symbols.get(symbol, 0) + 1
for symbol in ignore:
symbols.pop(symbol, None)
return sorted(symbols, key=symbols.get, reverse=True)[:n]
def split_symbols(sentence: str) -> List[str]:
"""
Splits a source sentence into symbols.
Parameters
----------
sentence : str
The sentence to split.
Returns
-------
List[str]
A list of symbols.
"""
return sentence.split() if ' ' in sentence else list(sentence)