Add 1 files
Browse files
app.py
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from matplotlib import pyplot as plt
|
4 |
+
|
5 |
+
# Streamlit-Grammar Tokenizer
|
6 |
+
|
7 |
+
class GrammarTokenizer:
|
8 |
+
def __init__(self, streamlit):
|
9 |
+
self.streamlit = streamlit
|
10 |
+
self.tokens = {}
|
11 |
+
|
12 |
+
def tokenize(self):
|
13 |
+
self.tokens = {}
|
14 |
+
self.processSignificantWord()
|
15 |
+
self.processToken()
|
16 |
+
self.processWhitespace()
|
17 |
+
self.processLabel()
|
18 |
+
self.process Иembedded_view()
|
19 |
+
self.processOther()
|
20 |
+
|
21 |
+
def processSignificantWord(self):
|
22 |
+
while self.streamlit > 0:
|
23 |
+
if self.isSignificantWord():
|
24 |
+
self.tokens.setdefault(self.streamlit, {}).setdefault('word', {})["significant"] = True
|
25 |
+
self.streamlit -= 1
|
26 |
+
self.processToken()
|
27 |
+
else:
|
28 |
+
self.streamlit -= 1
|
29 |
+
|
30 |
+
def isSignificantWord(self):
|
31 |
+
return all(self.streamlit > 0 and (self.streamlit[0] not in [' ', '\n', '\r\n'] and not self.streamlit.endswith('\\')))))
|
32 |
+
|
33 |
+
def processToken(self):
|
34 |
+
if self.streamlit > 0 and self.streamlit[0] not in [' ', '\n', '\r\n']:
|
35 |
+
self.tokens.setdefault(self.streamlit, {}).setdefault('token', {})["begin"] = self.streamlit
|
36 |
+
self.streamlit -= 1
|
37 |
+
while self.streamlit > 0 and self.streamlit[0] not in [' ', '\n', '\r\n']:
|
38 |
+
self.streamlit -= 1
|
39 |
+
self.tokens.setdefault(self.streamlit, {}).setdefault('token', {})["end"] = self.streamlit
|
40 |
+
|
41 |
+
def processWhitespace(self):
|
42 |
+
while self.streamlit > 0 and self.streamlit[0] in [' ', '\n', '\r\n']:
|
43 |
+
self.tokens.setdefault(self.streamlit, {}).setdefault('whitespace', {})["begin"] = self.streamlit
|
44 |
+
self.streamlit -= 1
|
45 |
+
while self.streamlit > 0 and self.streamlit[0] in [' ', '\n', '\r\n']:
|
46 |
+
self.streamlit -= 1
|
47 |
+
self.tokens.setdefault(self.streamlit, {}).setdefault('whitespace', {})["end"] = self.streamlit
|
48 |
+
|
49 |
+
def processLabel(self):
|
50 |
+
if self.streamlit > 0 and self.streamlit[0] == ':':
|
51 |
+
self.tokens.setdefault(self.streamlit, {}).setdefault('label', {})["begin"] = self.streamlit
|
52 |
+
self.streamlit -= 1
|
53 |
+
while self.streamlit > 0 and self.streamlit[0] not in [' ', '\n', '\r\n', ':']:
|
54 |
+
self.streamlit -= 1
|
55 |
+
self.tokens.setdefault(self.streamlit, {}).setdefault('label', {})["end"] = self.streamlit
|
56 |
+
|
57 |
+
def processИembedded_view(self):
|
58 |
+
if self.streamlit > 0 and self.streamlit[0] == '{':
|
59 |
+
while self.streamlit > 0 and self.streamlit[0] not in [' ', '\n', '\r\n', '}']:
|
60 |
+
self.streamlit -= 1
|
61 |
+
self.processViewElement()
|
62 |
+
|
63 |
+
def processViewElement(self):
|
64 |
+
if self.streamlit > 0 and self.streamlit[0] == ';':
|
65 |
+
self.tokens.setdefault(self.streamlit, {}).setdefault('empty', {})["ensuremath"] = True
|
66 |
+
self.streamlit -= 1
|
67 |
+
while self.streamlit > 0 and self.streamlit[0] not in [' ', '\n', '\r\n', '}']:
|
68 |
+
self.streamlit -= 1
|
69 |
+
self.tokens.setdefault(self.streamlit, {}).setdefault('empty', {})["\\endempty"] = True
|
70 |
+
|
71 |
+
def processOther(self):
|
72 |
+
while self.streamlit > 0:
|
73 |
+
if self.streamlit[0] in [' ', '\n', '\r\n']:
|
74 |
+
self.streamlit -= 1
|
75 |
+
continue
|
76 |
+
if self.streamlit[0] == ':':
|
77 |
+
self.processLabel()
|
78 |
+
continue
|
79 |
+
if self.streamlit[0] == '{':
|
80 |
+
self.processИembedded_view()
|
81 |
+
continue
|
82 |
+
if self.streamlit[0] == '}':
|
83 |
+
self.streamlit -= 1
|
84 |
+
continue
|
85 |
+
self.streamlit -= 1
|
86 |
+
self.tokens.setdefault('end', {})["wendung"] = True
|
87 |
+
|
88 |
+
# Streamlit-Grammar Converter
|
89 |
+
|
90 |
+
class GrammarConverter:
|
91 |
+
def __init__(self, tokens):
|
92 |
+
self.tokens = tokens
|
93 |
+
self.convert()
|
94 |
+
|
95 |
+
def convert(self):
|
96 |
+
for token in self.tokens:
|
97 |
+
self.convertToken(token)
|
98 |
+
|
99 |
+
def convertToken(self, token):
|
100 |
+
if token[-1] == 'punkt':
|
101 |
+
punkt = token[-1]
|
102 |
+
token["$$BATCH"] = token["^^TEXT"]
|
103 |
+
token["text"] = '"' + punkt + '"'
|
104 |
+
token.pop("^^TEXT")
|
105 |
+
token.pop("punkt")
|
106 |
+
|
107 |
+
# Streamlit-Grammar JSON
|
108 |
+
|
109 |
+
class GrammarJSON:
|
110 |
+
def __init__(self, streamlit):
|
111 |
+
self.streamlit = streamlit
|
112 |
+
self.grammar = GrammarTokenizer(streamlit).tokens
|
113 |
+
self.converted = GrammarConverter(self.grammar).tokens
|
114 |
+
|
115 |
+
# Streamlit-Grammar Styler
|
116 |
+
|
117 |
+
class GrammarStyler:
|
118 |
+
def __init__(self, parsed):
|
119 |
+
self.parsed = parsed
|
120 |
+
self.style()
|
121 |
+
|
122 |
+
def style(self):
|
123 |
+
self.parsed = self.styleText()
|
124 |
+
self.parsed = self.styleWhitespace()
|
125 |
+
self.parsed = self.styleLabel()
|
126 |
+
|
127 |
+
def styleText(self):
|
128 |
+
style = "text"
|
129 |
+
for token in self.parsed:
|
130 |
+
if token[1]:
|
131 |
+
style = self.encodeStyle(style, "label")
|
132 |
+
else:
|
133 |
+
style = self.encodeStyle(style, "text")
|
134 |
+
token[1], style = style, token[1]
|
135 |
+
return self.parsed
|
136 |
+
|
137 |
+
def styleWhitespace(self):
|
138 |
+
style = "none"
|
139 |
+
for token in self.parsed:
|
140 |
+
if not token[0]:
|
141 |
+
style = self.encodeStyle(style, "whitespace")
|
142 |
+
token[1], style = style, token[1]
|
143 |
+
return self.parsed
|
144 |
+
|
145 |
+
def styleLabel(self):
|
146 |
+
style = "none"
|
147 |
+
for token in self.parsed:
|
148 |
+
if token[2]:
|
149 |
+
style = self.encodeStyle(style, "label")
|
150 |
+
token[1], style = style, token[1]
|
151 |
+
return self.parsed
|
152 |
+
|
153 |
+
def encodeStyle(self, style, newStyle):
|
154 |
+
if style == "none":
|
155 |
+
return newStyle
|
156 |
+
return newStyle + "+" + style
|
157 |
+
|
158 |
+
def main():
|
159 |
+
streamlit = st.write(f"This is a simple todo list app.\n")
|
160 |
+
tokens = GrammarTokenizer(streamlit).tokens
|
161 |
+
converted = GrammarConverter(tokens).tokens
|
162 |
+
parsed = GrammarJSON(streamlit, tokens).converted
|
163 |
+
styled = GrammarStyler(parsed).style()
|
164 |
+
st.write(styled)
|
165 |
+
|
166 |
+
if __name__ == "__main__":
|
167 |
+
main()
|