-
Notifications
You must be signed in to change notification settings - Fork 0
/
suku.py
115 lines (103 loc) · 3.04 KB
/
suku.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#file : suku.py
#auth : Peb Ruswono Aryan
#desc : pemenggalan suku kata bahasa indonesia
import os, sys
vokal = ['a', 'i', 'u', 'e', 'o']
awalan = ["be", "me", "pe"]
def replacer(kata, pola):
result = kata
for p,r in pola.items():
result = result.replace(p, r)
return result
def unreplacer(kata, pola):
result = kata
for p,r in pola.items():
result = result.replace(r, p)
return result
def praproses(kata):
result = []
tmp = ""
i=0
inkonsonan = False
numkonsonan = 0
for karakter in kata:
iskonsonan = (karakter not in vokal)
if iskonsonan:
if not inkonsonan:
inkonsonan = True
numkonsonan += 1
tmp += karakter
else:
if inkonsonan:
inkonsonan = False
if len(tmp)==1:
result += [tmp+karakter]
else:
result += [tmp[0], tmp[1:]+karakter]
tmp = ""
else:
result += [karakter]
if len(tmp)>0:
result += [tmp]
return result
def kaidah1(listsuku):
global vokal
result = []
last = ""
i = 0
for suku in listsuku:
if len(suku)==1 and i>0:
if suku in vokal:
result += [suku]
elif listsuku[i-1][-1] in vokal:
if i<len(listsuku)-1 and len(listsuku[i+1])==1 and listsuku[i+1][0] in vokal:
result += [suku]
else:
result[-1] = result[-1] + suku
else:
result += [suku]
i += 1
last = suku
return result
def kaidah2(listsuku):
global vokal, awalan
dift = ["$", "%", "^", "&", "*", "(", "e)"]
if len(listsuku)>1:
if listsuku[0] in awalan and listsuku[1][0] not in vokal and listsuku[1][0] not in dift and len(listsuku[1])>2:
listsuku[0] += listsuku[1][0]
listsuku[1] = listsuku[1][1:]
if len(listsuku[0])==1:
listsuku = [listsuku[0]+listsuku[1]] + listsuku[2:]
return listsuku
def kaidah3(listsuku):
global vokal
result = []
last = ""
i = 0
for suku in listsuku:
if len(suku)==1 and i>0:
if listsuku[i-1][-1] in vokal:
result[-1] = result[-1] + suku
else:
result += [suku]
else:
result += [suku]
i += 1
last = suku
return result
def pecah(kata):
kdift = {"kh":"$", "ng":"%", "ny":"^", "sy":"&", "tr":"*", "gr":"(", "eu":")"}
suku = praproses(replacer(kata, kdift))
# print suku
suku = kaidah1(suku)
# print "1 ", suku
suku = kaidah2(suku)
# print "2 ", suku
suku = kaidah3(suku)
# print "3 ", suku
return [unreplacer(s, kdift) for s in suku]
#return [unreplacer(s, kdift) for s in kaidah3(kaidah2(kaidah1(praproses(replacer(kata, kdift)))))]
if __name__=='__main__':
if len(sys.argv)>1:
kata = sys.argv[1]
print pecah(kata)