forked from Arisamanjaya/TBO-Final-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyk.py
50 lines (46 loc) · 1.96 KB
/
cyk.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
import streamlit as st
from cnf import get_set_of_production
TRIANGULAR_TABLE = {}
g = None
previousNode = None
# fungsi untuk memeriksa kalimat dan tablefilling
def is_accepted(inputString):
global TRIANGULAR_TABLE
TRIANGULAR_TABLE.clear()
prodRules = get_set_of_production()
temp = inputString.lower().split(" ")
inputString = temp
for i in range(1,len(inputString)+1):
for j in range(i, len(inputString)+1):
TRIANGULAR_TABLE[(i,j)] = []
for i in reversed(range(1, len(inputString)+1)):
for j in range(1, i+1):
if (j == j + len(inputString) - i):
tempList = []
for key, value in prodRules.items():
for val in value:
if (val == inputString[j-1] and key not in tempList):
tempList.append(key)
TRIANGULAR_TABLE[(j, j + len(inputString) - i)] = tempList
else:
tempList = []
resultList = []
for k in range(len(inputString) - i):
first = TRIANGULAR_TABLE[(j,j+k)]
second = TRIANGULAR_TABLE[(j+k+1,j+len(inputString) - i)]
for fi in first:
for se in second:
if (fi + " " + se not in tempList):
tempList.append(fi + " " + se)
for key, value in prodRules.items():
for val in value:
if (val in tempList and key not in resultList):
resultList.append(key)
TRIANGULAR_TABLE[(j,j+len(inputString) - i)] = resultList
if "K" in TRIANGULAR_TABLE[(1, len(inputString))]:
st.success("Sentences Accepted", icon="👍🏻")
return True
else:
st.error("Senteces Rejected", icon="👎🏻")
return False
# print(is_accepted("saya makan nasi"))