-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.py
More file actions
157 lines (132 loc) · 4.13 KB
/
Copy pathinput.py
File metadata and controls
157 lines (132 loc) · 4.13 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
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
#!/usr/local/bin/python3
import random, itertools, sys
from textblob import TextBlob
def load_set(filenames): # for loading a test or training set
set = []
for filename in filenames:
for line in open(filename,'r'):
line = line.rstrip()
if line.endswith(',-1'):
set.append((line[0:-3],'neg'))
elif line.endswith(',1'):
set.append((line[0:-2],'pos'))
elif line.endswith(',0'):
set.append((line[0:-2],'neu'))
return set
def read_anzns(filename):
title = ''
seen = set()
storing = False
fulltext = ''
year = ''
location = ''
store = {}
for line in open(filename,'r'):
line = line.rstrip()
if line.startswith('Full text:'):
storing = True
fulltext = line[10:].lower()
else:
if storing:
fulltext += ' ' + line
if line == '':
storing = False
# if line.startswith('Subject:') or line.startswith('Credit:'):
# storing = False
# fulltext = ''
if line.startswith('Title:'):
title = line[7:]
if line.startswith('Publication year:'):
year = line[18:]
if line.startswith('Place of publication:'):
location = line[22:]
if len(fulltext) > 1 and not storing:
if title not in seen:
print(location)
by_year = {}
try:
by_year = store[location]
except KeyError:
store[location] = {}
try:
by_year[year].append(fulltext)
except KeyError:
by_year[year] = [fulltext]
store[location] = by_year
fulltext = ''
else:
seen.add(title)
fulltext = ''
return store
# Next function needs further testing.
def factiva(filename):
storing = False
dateLineNext = False
fulltext = ''
year = ''
for line in open(filename,'r'):
if line.endswith('words'):
dateLineNext = True
storing = True
if dateLineNext:
year = line[-4:]
dateLineNext = False;
if storing:
fulltext += line
if line.startswith('Document'):
storing = False
store.append((year,fulltext))
fulltext = ''
return store
# Generate input data
if __name__ == '__main__':
filename = ''
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
filename = 'anzns.txt'
articles = read_anzns(filename)
wollongong_articles = []
kiama_articles = []
other_articles = []
wollongong_test = open('wollongong_test.txt','w')
wollongong_training = open('wollongong_training.txt','w')
kiama_test = open('kiama_test.txt','w')
kiama_training = open('kiama_training.txt','w')
other_test = open('other_test.txt','w')
other_training = open('other_training.txt','w')
for location in articles.keys():
if location.startswith('Wollongong'):
for article in [item for sublist in articles[location].values() for item in sublist]:
wollongong_articles.append(article)
elif location.startswith('Kiama'):
for article in [item for sublist in articles[location].values() for item in sublist]:
kiama_articles.append(article)
else:
for article in [item for sublist in articles[location].values() for item in sublist]:
other_articles.append(article)
for i in range(88):
wollongong_article = TextBlob(random.choice(wollongong_articles))
for sentence in wollongong_article.sentences:
if 'NBN' in sentence:
if i < 44:
wollongong_test.write(str(sentence) + ',\n')
if i > 44:
wollongong_training.write(str(sentence) + ',\n')
for i in range(min(len(kiama_articles),88)):
print(str(i))
kiama_article = TextBlob(kiama_articles[i])
for sentence in kiama_article.sentences:
if 'NBN' in sentence:
if i < 44:
kiama_test.write(str(sentence) + ',\n')
if i > 44:
kiama_training.write(str(sentence) + ',\n')
for i in range(min(len(other_articles),88)):
other_article = TextBlob(random.choice(other_articles))
for sentence in other_article.raw_sentences:
if 'NBN' in sentence:
if i < 44:
other_test.write(str(sentence) + ',\n')
if i > 44:
other_training.write(str(sentence) + ',\n')