-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateindexForTitle.py
More file actions
91 lines (77 loc) · 2.46 KB
/
createindexForTitle.py
File metadata and controls
91 lines (77 loc) · 2.46 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
from bs4 import BeautifulSoup
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import SnowballStemmer
from collections import defaultdict
stemmer = SnowballStemmer("english")
import re
import sys
import string
import math
from pymongo import MongoClient
reload(sys)
sys.setdefaultencoding('utf-8')
index = defaultdict(list)
numberOfDocuments = 0
client = MongoClient('localhost', 27017)
db = client.test_database
collection = db.titleIndex
def tokenize(text):
try:
text_tokenized = word_tokenize(text)
except:
text = unicode(text, errors='ignore')
text_tokenized = word_tokenize(text)
return text_tokenized
def parseWordList(tokens):
tokenlist = []
for token in tokens:
try:
token = token.translate(string.punctuation)
except:
token = token
m = re.match(r'^[a-zA-Z]+$', token)
if m:
#if not token in stopwords.words('english'):
tokenlist.append(stemmer.stem(token).encode('utf-8'))
return tokenlist
for i in range(75):
for j in range(500):
docid = 500 * i + j
try:
with open(str(i) + '/' + str(j)) as f:
soup = BeautifulSoup(f, 'lxml')
try:
body = soup.body.text
except:
print 'folder', i, 'doc', j, "is not a valid html"
continue
try:
title = soup.title.text
titlelist = parseWordList(tokenize(title))
except:
print 'folder', i, 'doc', j, "does not have a title"
continue
titlelist = []
termdict = {}
for position, term in enumerate(titlelist):
if not term in stopwords.words('english'):
try:
termdict[term][1][1].append(position)
except:
termdict[term] = [docid, [[1], [position]]]
for term, postingpage in termdict.iteritems():
index[term].append(postingpage)
except:
print 'folder', i, 'doc', j, "It's a not valid html"
continue
for term, doc in index.iteritems():
finalindex = {}
df = len(doc)
finalindex["_id"] = term
finalindex["docid"] = {}
for eachdoc in doc:
docid = eachdoc[0]
finalindex["docid"][str(docid)] = eachdoc[1][1]
collection.insert_one(finalindex)
print "finished"