-
Notifications
You must be signed in to change notification settings - Fork 2
/
SentenceExtractore.py
175 lines (116 loc) · 3.84 KB
/
SentenceExtractore.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
__author__ = 'davide'
import urllib
import os
def sentence_tag(sentence, word):
# print(sentence)
# print(word)
import nltk
text = nltk.word_tokenize(sentence)
tagged = nltk.pos_tag(text)
# print tagged
# for t in tagged:
#print t[0]
for t in tagged:
if t[0].lower() == word.lower():
#print(t[0].lower())
#print(word.lower())
os.system("printf \"" + t[0].lower() + ": " + t[1] + "\n\" >> wordstoken.txt")
os.system("printf \"" + sentence + "\n\" >> words/" + word + ".txt")
#print("-------------------------------------")
from BeautifulSoup import BeautifulSoup
j = 0
previous_word = "aarori"
with open("words.txt") as f:
for line in f:
url = "http://www.urbandictionary.com/define.php?term=" + line
page = urllib.urlopen(url).read()
# soup = BeautifulSoup(page)
soup = BeautifulSoup(page.decode('utf-8', 'ignore'))
soup.prettify()
i = 0
if (j != 0):
print
text;
# passare =text.decode('utf-8').strip()
passare = "".join([ch for ch in text if ord(ch) < 128])
# passare = text.encode('ascii','ignore')
passare2 = "".join([ch for ch in previous_word if ord(ch) < 128])
#passare2 =previous_word.strip()
sentence_tag(passare.strip(), passare2.strip())
#print(url)
previous_word = line
text = ""
j = 1
for anchor in soup.find("div", {"class": "example"}):
# print (i)
if i == 0:
text = str(anchor).replace("<br />", " ")
else:
text += str(anchor).replace("<br />", " ")
i = i + 1;
# print text
text = text.replace("&", "&")
text = text.replace(""", '"')
text = text.replace("'", "'")
text = text.replace(">", ">")
text = text.replace("&l;", "<")
# print text
# from HTMLParser import HTMLParser
# parser = HTMLParser()
# text = text.encode('utf-8', 'ignore')
#
# text = text.decode('utf-8')
#
#text = parser.unescape(text)
#print text
#for anchor in soup.findAll('div'):
# print anchor['class'];
'''
print(url)
req = urllib.urlopen(url)
html = str(req.read()) # make it a str object
print("aaaaaa")
# print(html)
html = html.replace("<br>", " ")
html = html.replace("<br/>", " ")
print("bbbb")
#print (html)
parser = MyHTMLParser()
# parser.feed(html)
print("ciao")
parser.feed(html)
'''
#
#
#
# class MyHTMLParser(HTMLParser):
# def __init__(self):
# HTMLParser.__init__(self)
# self.inLink = False
# self.dataArray = []
# self.countLanguages = 0
# self.lasttag = None
# self.lastname = None
# self.lastvalue = None
#
# def handle_starttag(self, tag, attrs):
# self.inLink = False
# if tag == 'div':
# for name, value in attrs:
# if name == 'class' and value == 'example':
# self.countLanguages += 1
# self.inLink = True
# self.lasttag = tag
# print("miao")
#
# def handle_endtag(self, tag):
# if tag == "div":
# self.inlink = False
#
# def handle_data(self, data):
# if self.lasttag == 'div' and self.inLink and data.strip():
# text = html_decoded_string = parser.unescape(data)
# content = text.decode('utf-8')
# print(content.rstrip())
# print("end")
# sentence_tag(content.rstrip())