-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.py
175 lines (137 loc) · 4.18 KB
/
parse.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import json
import itertools
import util
import warnings
# loads raw ocr data
path = "images/img5.jpg"
fi = open("data/"+path.split("/")[-1].split(".")[0]+".json","r")
js = json.loads(fi.read())
bds = js[0]
ccr = js[1]
# dictionary
wl = open("dict/linuxwords.txt","r").read().upper().split("\n")
# format raw ocr data
def guessformat():
result = ""
lines = []
for i in range(0,len(bds)):
found = 0
for j in range(0,len(lines)):
if len(lines[j]) > 0:
if abs(bds[i][1]- sum([bds[l][1] for l in lines[j]])/len(lines[j])) < 5:
lines[j].append(i)
found = 1
break
if found == 0:
lines.append([i])
for i in range(0,len(lines)):
lines[i] = sorted(lines[i],key=lambda x: bds[x][0])
lines = sorted(lines, key = lambda x: bds[x[0]][1])
for j in range(0,len(lines)):
l = lines[j]
nl = []
for i in range(0,len(l)):
nl.append(l[i])
if i < len(l)-1:
if bds[l[i+1]][0] - bds[l[i]][2] > 0.3*sum([bds[k][2]-bds[k][0] for k in l])/len(l):
nl.append(" ")
if bds[l[i]][3]-bds[l[i]][1] < 0.5*sum([bds[k][3]-bds[k][1] for k in l])/len(l):
nl.pop()
nl.append("'")
elif bds[l[i]][3]-bds[l[i]][1] < 0.9*sum([bds[k][3]-bds[k][1] for k in l])/len(l):
nl.pop()
if ccr[l[i]][0][0] == "I" or ccr[l[i]][0][0] == "J":
nl.append("!")
else:
nl.append("?")
lines[j] = nl
return lines
# guess a line of text
def guessline(line,simple=False):
words = [[]]
for i in line:
if i == " ":
words.append([])
elif i == "?" or i == "!":
words.append(i)
else:
if type(words[-1]) == list:
words[-1].append(i)
else:
words.append([i])
for i in range(0,len(words)):
if type(words[i])==list:
words[i] = guessword(words[i],simple)
return " ".join(words)
# sort possible corrections
def sortchange(word):
changes = []
for w in word:
if type(w) == int:
for i in range(1,len(ccr[w])):
changes.append( (w,ccr[w][i]) )
changes = sorted(changes, key = lambda x: x[1][1])
return changes
# guess a word
def guessword(w, simple=False):
word = w[:]
changes = {}
pot = sortchange(word)
guess = ""
for i in range(0,len(word)):
if type(word[i]) == int:
word[i] = (word[i],ccr[word[i]][0][0])
else:
word[i] = (-1,word[i])
guess = "".join([w[1] for w in word])
guess0 = guess
if simple:
return guess0
def ind(word,i):
for j in range(0,len(word)):
if word[j][0] == i:
return j
return -1
while guess not in wl:
if len(pot) == 0 or pot[-1][1][1] < 0.8:
return guess0
nc = pot.pop()
if nc[0] not in changes.keys():
changes[nc[0]] = []
changes[nc[0]].append(nc)
chco = util.allchoice(changes.values())
for c in chco:
newword = word[:]
for d in c:
newword[ind(newword,d[0])] = (d[0],d[1][0])
guess = "".join([w[1] for w in newword])
#print guess
if guess in wl:
return guess
return guess
# check if some text is noise
def isgibber(t):
if len(t)<3:
return True
if len(t)<4 and " " in t:
return True
if t.count(" ") > len(t)/3:
return True
return False
# guess the caption of a meme
def guesscaption(simple = False):
output = ""
gf = guessformat()
print "raw: "
for g in gf:
print guessline(g,True)
print
with warnings.catch_warnings():
warnings.simplefilter("ignore")
for g in gf:
gl = guessline(g,simple = simple)
if not isgibber(gl):
output += gl.replace(" !","!").replace(" ?","?")+"\n"
return output
if __name__ == "__main__":
print guesscaption()