-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPItoTuple.py
More file actions
126 lines (121 loc) · 2.8 KB
/
APItoTuple.py
File metadata and controls
126 lines (121 loc) · 2.8 KB
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
import sys
iFile=open(sys.argv[1], 'r')
k=sys.argv[1].find('.')
out1 = sys.argv[1][:k]+"_freshness.txt"
out2 = sys.argv[1][:k]+"_score.txt"
oFile1=open(out1, 'w')
oFile2=open(out2, 'w')
movieIDB=freshnessB=scoreB=quoteB=False
grade_array=["F","D","C-","C","C+","B-","B","B+","A-","A","A+"]
criticList={}
line = iFile.readline()
while(not (line=="")):
i=line.find(':')
if(not(i==-1)):
cat=line[:i]
cat=cat.strip()
if(cat[0]=='\"'):
cat=cat[1:]
#end if
if(cat[-1]=='\"'):
cat=cat[:-1]
#end if
#print cat
value=line[i+1:]
value=value.strip()
if(value[-1]==','):
value=value[:-1]
#end if
if(value[-1]=='\"'):
value=value[:-1]
#end if
if(value[0]=='\"'):
value=value[1:]
#end if
#print value
if(cat=="links"):
start=value.find('{')
value=value[start+1:]
while(value.find('}')==-1):
value=iFile.readline()
#end while
elif(cat=="movieID"):
movieID = value
movieIDB=True
#elif(cat=="publication"):
# tup += value + ','
elif(cat=="quote"):
value = value.replace('\'','\\\'')
quote = '\"' + value + '\"'
quoteB=True
elif(cat=="freshness"):
freshness = '\"' + value + '\"'
freshnessB=True
elif(cat=="critic"):
if(value in criticList):
critic=str(criticList[value])
else:
newval=len(criticList)
criticList.update({value:newval})
critic=str(newval)
#end if
if(movieIDB and freshnessB and quoteB and not(quote=='\"\"') and not(freshness=="\"none\"") and not(critic=='\"\"')):
tup=movieID+','+critic+','+quote+','+freshness+'\n'
#print tup.strip()
oFile1.write(tup)
#end if
if(movieIDB and scoreB and quoteB and not(quote=='\"\"') and not critic=='\"\"' and not score=="INVALID"):
if(not freshnessB):
freshness="\"none\""
#end if
tup=movieID+','+critic+','+freshness+','+score+','+quote+'\n'
#print tup.strip()
oFile2.write(tup)
#end if
movieIDB=freshnessB=scoreB=quoteB=False
elif(cat=="original_score"):
scoreB=True
spot=value.find('/')
divisor=numerator=-1
if(spot==-1):
divisor = 10
j=0
while(j<11):
if(value==grade_array[j]):
numerator=j
break
#end if
j+=1;
#end while
else:
strNumerator = value[:spot].strip()
strDivisor = value[spot+1:].strip()
valid=True
for c in strNumerator:
if(not(c.isdigit() or c == '.')):
valid=False
#end if
#end for
for c in strDivisor:
if(not(c.isdigit() or c=='.')):
valid=False
#end if
#end for
if(valid):
numerator=float(strNumerator)
divisor=float(strDivisor)
#end if
#end if
if(numerator == -1 or divisor == 0):
score = "INVALID"
else:
percent = numerator/divisor
score = str(percent)
#end if
#end if
#end if
line = iFile.readline()
#end while
oFile1.close()
oFile2.close()
iFile.close()