Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
import pickle
|
4 |
+
import gib_detect_train
|
5 |
+
|
6 |
+
model_data = pickle.load(open('gib_model.pki', 'rb'))
|
7 |
+
|
8 |
+
|
9 |
+
import math
|
10 |
+
import pickle
|
11 |
+
|
12 |
+
accepted_chars = 'abcdefghijklmnopqrstuvwxyz '
|
13 |
+
|
14 |
+
pos = dict([(char, idx) for idx, char in enumerate(accepted_chars)])
|
15 |
+
|
16 |
+
|
17 |
+
def normalize(line):
|
18 |
+
""" Return only the subset of chars from accepted_chars.
|
19 |
+
This helps keep the model relatively small by ignoring punctuation,
|
20 |
+
infrequently symbols, etc. """
|
21 |
+
return [c.lower() for c in line if c.lower() in accepted_chars]
|
22 |
+
|
23 |
+
|
24 |
+
def ngram(n, l):
|
25 |
+
""" Return all n grams from l after normalizing """
|
26 |
+
filtered = normalize(l)
|
27 |
+
for start in range(0, len(filtered) - n + 1):
|
28 |
+
yield ''.join(filtered[start:start + n])
|
29 |
+
|
30 |
+
|
31 |
+
def get_lines():
|
32 |
+
datasets = ['big.txt']
|
33 |
+
for filename in datasets:
|
34 |
+
with open(filename) as fp:
|
35 |
+
for line in fp:
|
36 |
+
yield line
|
37 |
+
|
38 |
+
|
39 |
+
def avg_transition_prob(l, log_prob_mat):
|
40 |
+
""" Return the average transition prob from l through log_prob_mat. """
|
41 |
+
log_prob = 0.0
|
42 |
+
transition_ct = 0
|
43 |
+
for a, b, c in ngram(3, l):
|
44 |
+
log_prob += log_prob_mat[pos[a]][pos[b]][pos[c]]
|
45 |
+
transition_ct += 1
|
46 |
+
# The exponentiation translates from log probs to probs.
|
47 |
+
return math.exp(log_prob / (transition_ct or 1))
|
48 |
+
# The exponentiation translates from log probs to probs.
|
49 |
+
return math.exp(log_prob / (transition_ct or 1))
|
50 |
+
|
51 |
+
while True:
|
52 |
+
l = st.text_area('enter a prospection message')
|
53 |
+
model_mat = model_data['mat']
|
54 |
+
threshold = model_data['thresh']
|
55 |
+
st.write(avg_transition_prob(l, model_mat) > threshold)
|