-
Notifications
You must be signed in to change notification settings - Fork 22
/
mac_2.py
181 lines (112 loc) · 4.19 KB
/
mac_2.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
172
173
174
175
176
177
178
##########################
###
###
### Lecture 2, Macine Learning
###
###
###
##########################
from bs4 import BeautifulSoup
from urllib import request
import re, os
url = request.urlopen('http://avalon.law.yale.edu/19th_century/gettyb.asp').read()
soup = BeautifulSoup(url)
text = soup.p.contents[0]
text_1 = text.lower()
text_2 = re.sub('\W', ' ', text_1)
from nltk import word_tokenize
from nltk import bigrams
from nltk import trigrams
from nltk import ngrams
text_3 = word_tokenize(text_2)
text_3_bi = bigrams(text_3)
text_3_tri = trigrams(text_3)
text_3_n = ngrams(text_3, 4)
stop_words = str(request.urlopen('http://www.ai.mit.edu/projects/jmlr/papers/volume5/lewis04a/a11-smart-stop-list/english.stop').read())
stop_words.split('\\n')
##we can then identify the stop words and then eliminate them from the list
##this is code that executes a very simple for loop to check the list
text_4 = [x for x in text_3 if x not in stop_words]
##you can check what was removed with:
text_rem = [x for x in text_3 if x not in text_4]
##we're going to use a similar format to apply various stemming/lemmatizing/synonyms algorithms
from nltk.stem.lancaster import LancasterStemmer
st = LancasterStemmer()
from nltk.stem import PorterStemmer
pt = PorterStemmer()
from nltk.stem.snowball import EnglishStemmer
sb = EnglishStemmer()
from nltk.stem.wordnet import WordNetLemmatizer
wn = WordNetLemmatizer()
##let's examine the word ``better"
st.stem('better')
pt.stem('better')
sb.stem('better')
wn.lemmatize('better', 'a')
wn.lemmatize('families', 'n')
##
##applying the porter stemmer to the gettysburg address
text_5 = list(map(pt.stem, text_4))
##now creating a dictionary that will count the occurrence of the words
getty = {}
used = []
for word in text_5:
if word in getty:
getty[word] += 1
if word not in getty and word not in used:
getty[word] = 1
used.append(word)
getty_count = list(getty.values())
getty_keys = list(getty.keys())
rfile = open('/users/justingrimmer/dropbox/teaching/text/mac18/class2/GettysburgFinal.txt', 'w')
rfile.write('stem, count')
rfile.write('\n')
for j in range(len(getty_keys)):
rfile.write('%s,%s' %(getty_keys[j], getty_count[j]))
rfile.write('\n')
rfile.close()
##position it so that it creates a document
dtm = open('/users/justingrimmer/dropbox/teaching/text/mac18/class2/GettysburgFinalDTM.txt', 'w')
getty_words = 'Document'
getty_numbers = 'Address'
for m in range(len(getty_keys)):
getty_words += ','
getty_words += getty_keys[m]
getty_numbers += ','
getty_numbers += str(getty_count[m])
dtm.write(getty_words)
dtm.write('\n')
dtm.write(getty_numbers)
for m in range(len(getty_keys)):
dtm.write(getty_keys[m])
dtm.write(',')
dtm.write('\n')
dtm.write('address')
for m in range(len(getty_count)):
dtm.write(str(getty_count[m]))
dtm.write(',')
dtm.write('\n')
dtm.close()
############
##Regular Expression
create_data.write('Number,Earmark, Wasteful_Spending,Gov_Waste,Tax_Payer,Nat_Debt,Debt,Deficit, Budget, Bud_Def, Big_Gov, Tax_Inc')
create_data.write('\n')
for z in range(1, len(text)):
ff = open(text[z].strip('\n'), 'r')
gg = ff.read()
ff.close()
ears = re.findall('earmark|Earmark', gg[1:len(gg)])
waste = re.findall('wasteful spending|Wasterful spending', gg[1:len(gg)])
gov_waste= re.findall('wasteful', gg[1:len(gg)])
tax_payer = re.findall('taxpayer', gg[1:len(gg)])
nat_debt = re.findall('national debt|National debt', gg[1:len(gg)])
debt = re.findall('debt|Debt', gg[1:len(gg)])
deficit = re.findall('deficit|Deficit', gg[1:len(gg)])
budget = re.findall('budget|Budget', gg[1:len(gg)])
bud_def = re.findall('Budget deficit|budget deficit', gg[1:len(gg)])
big_gov = re.findall('Big government|big government', gg[1:len(gg)])
tax_inc = re.findall('high tax|High tax|higher tax|Higher tax|tax increase|Tax increase|more tax|More tax', gg[1:len(gg)])
create_data.write('%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s' %(str(z), str(len(ears)), str(len(waste)), str(len(gov_waste)), str(len(tax_payer)), str(len(nat_debt)), str(len(debt)), str(len(deficit)), str(len(budget)), str(len(bud_def)), str(len(big_gov)) , str(len(tax_inc))))
create_data.write('\n')
if z%1000==0:
print(z)