-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevisor.py
120 lines (101 loc) · 3.18 KB
/
revisor.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
import sys
from record import record_and_transcript
alumnos = []
entries = []
def get_alumnos():
with open("alumnos.txt") as f:
for linea in f:
comps = linea.split(",")
alumnos.append({
"nombre": comps[1],
"rut": comps[0],
"num": comps[2][:-1]
})
def lev(s1, s2):
if len(s1) < len(s2):
return lev(s2, s1)
if len(s2) == 0:
return len(s1)
previous_row = range(len(s2) + 1)
for i, c1 in enumerate(s1):
current_row = [i + 1]
for j, c2 in enumerate(s2):
insertions = previous_row[j + 1] + 1
deletions = current_row[j] + 1
substitutions = previous_row[j] + (c1 != c2)
current_row.append(min(insertions, deletions, substitutions))
previous_row = current_row
return previous_row[-1]
def expected_alumno(text):
min_dist = 99999
min_alum = ""
for alumno in alumnos:
d = lev(alumno["nombre"], text)
if d < min_dist:
min_dist = d
min_alum = alumno
return min_alum
def expected_nota(text, raw=False):
if len(text) == 0 or len(text) >= 3:
return -1
if raw:
return int(text)
else:
if len(text) == 1:
text += "0"
return int(text) / 10.0
def divide_alum_nota(text):
words = text.split(" ")
alum = ""
nota = []
for word in words:
if word.isalpha():
alum += word + " "
elif word.isdigit():
nota.append(word)
return alum.strip(), nota
def register(text, n=1, raw=False):
text_alum, text_nota = divide_alum_nota(text)
enotas = []
for i in range(n):
if i < len(text_nota):
enotas.append(expected_nota(text_nota[i], raw=raw))
else:
enotas.append(-1)
ealum = expected_alumno(text_alum)
row = ealum["num"] + "," + ealum["rut"] + "," + ealum["nombre"] + "," + ",".join(map(str, enotas))
print "Registro: " + row.replace(",", "\t")
entries.append(row)
def save_to_file(n=1):
s = raw_input("Nombre archivo: ") + ".csv"
with open(s, "w") as f:
f.write("N,RUT,Nombre," + ",".join(map(lambda e: "P" + str(e+1), range(n))) + "\n")
emap = {}
for entry in entries:
num = entry.split(",")[0]
emap[num] = ",".join(entry.split(",")[3:])
for alum in alumnos:
row = alum["num"] + "," + alum["rut"] + "," + alum["nombre"] + ","
if alum["num"] in emap:
row = row + emap[alum["num"]]
f.write(row + "\n")
f.write("\n")
if __name__ == '__main__':
# PARAMETERS
N = int(sys.argv[sys.argv.index("-n") + 1]) if "-n" in sys.argv else 1
textual = "-t" in sys.argv
verbose = "-v" in sys.argv
raw = "-r" in sys.argv
# PROGRAM
print "\t\taudioRev"
get_alumnos()
s = raw_input("Ingresar otra: [si]/no: ")
while s != "no":
if not textual:
text = record_and_transcript(verbose=verbose)
else:
text = raw_input(">> ")
register(text, n=N, raw=raw)
s = raw_input("Ingresar otra: [si]/no: ")
save_to_file(n=N)
print "Guardado."