-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadData.py
63 lines (58 loc) · 1.5 KB
/
ReadData.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
import os
import glob
import cPickle
from bs4 import BeautifulSoup as bs
try:
os.remove("ReadData.pyc")
except:
print "The file wasn't previously complied"
#Address 1 for News Data
path = "C:\Project\STS\SourceCode\WordBased\Data\\2554\\2554\download\Texts\Data"
print path
os.chdir(path)
pathAllFiles = path + "\*"
FileNames = list(glob.glob(pathAllFiles))
print FileNames
allFiles = []
MoreNewFiles = []
for i in FileNames:
pathAllFiles = i + "\*"
MoreNewFiles += list(glob.glob(pathAllFiles))
for i in MoreNewFiles:
pathAllFiles = i + "\*"
allFiles += list(glob.glob(pathAllFiles))
print allFiles
homePath = "C:\Project\STS\SourceCode\WordBased\BigDatabase"
#To Stop Terminal Chaning its Address
os.chdir(homePath)
#print len(allFiles), allFiles[0]
#Parsing XML Files:
database = []
see = 0
for files in allFiles:
see += 1
print files
f = open(files).read()
f = bs(f, "lxml")
#print "File Length", len(f)
count = 0
for text in f.findAll('s'):
count += 1
temp = []
#if count >= 2: break
for word in text.findAll('w'):
#print word.next.strip()
if '/' in word['hw']:
w1 = word['hw'].strip().split('/')
for i in w1:
temp.append(i)
#if '-' in word['hw']:
# w1 = word['hw'].strip().split('-')
# for i in w1:
# temp.append(i)
else: temp.append(word['hw'].strip())
database.append(temp)
print "Count ", count
cPickle.dump(database, open('databaseD+E.txt', 'wb'))
print "Total No of Files read", see
del database[:]