-
Notifications
You must be signed in to change notification settings - Fork 3
/
prepocessing.py
49 lines (45 loc) · 1.61 KB
/
prepocessing.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
import sys
import csv
def parse_into_sentiments_and_tags(input_file):
fp = open(input_file, 'rb' )
sentences = open('sentences','wb')
tags = open('tags','wb')
reader = csv.reader( fp, delimiter=',', quotechar='"', escapechar='\\' )
for row in reader:
sentences.write(row[5]+'\n')
tags.write(row[0]+'\n')
def load_into_dict(input_file):
fp = open(input_file, 'rb' )
sentences = fp.readlines()
abbreviations = {}
for line in sentences:
if len(line.split(' \t'))>1:
a,b = line.split(' \t')
abbreviations[a.strip().lower()] = b.strip()
return abbreviations
def replace_with_abbreviations(abbreviations,input_file):
fp = open(input_file,'rb')
abbreviated = open(input_file+'.abbreviated','wb')
sentences = fp.readlines()
for line in sentences:
m = line.split()
new_m = []
for i in m:
try:
new_m += [j.lower() for j in abbreviations[(i.strip()).lower()].split()]
except:
new_m.append((i.strip()).lower())
abbreviated.write(' '.join(new_m)+'\n')
abbreviated.close()
return None
if __name__ == "__main__":
if len(sys.argv) < 3:
print "This is not the place for you."
if sys.argv[1]=='parse' and sys.argv[2]:
input_file = sys.argv[2]
parse_into_sentiments_and_tags(input_file)
if sys.argv[1]=='abbreviations' and sys.argv[2] and sys.argv[3]:
abbr_file = sys.argv[2]
input_file = sys.argv[3]
abbreviations = load_into_dict(abbr_file)
replace_with_abbreviations(abbreviations,sys.argv[3])