-
Notifications
You must be signed in to change notification settings - Fork 5
/
wikiloader.py
36 lines (29 loc) · 903 Bytes
/
wikiloader.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
import os
class Wikiloader():
def __init__(self, files, title):
"""
:param files: list of wikipedia files
"""
# load name of all txt files
self.filenames = files
self.length = -1
self.title = title
def __iter__(self):
for name in self.filenames:
print("Reading: ", name)
with open(name, 'r')as f:
for l in f:
if l == '':
break
else:
yield l
yield l
def __len__(self):
if self.length == -1:
self.length = 0
for name in self.filenames:
with open(name, 'r')as f:
for l in f:
self.length += 1
print(self.length, " sentences has been found in the ", self.title)
return self.length