-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_data.py
executable file
·49 lines (38 loc) · 1.3 KB
/
test_data.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
from __future__ import absolute_import
__author__ = 'a_medelyan'
import os
import io
# class to hold our test instance (document plus its correct manual keywords)
class TestDoc:
def __init__(self, name):
self.name = name
self.text = ''
self.keywords = []
# reading documents and their keywords from a directory
def read_data(input_dir):
test_set = {}
for doc in os.listdir(input_dir):
file_reader = io.open(os.path.join(input_dir,doc), 'r',encoding="iso-8859-1")
file_name = doc[:-4]
if file_name not in test_set:
d = TestDoc(file_name)
else:
d = test_set[file_name]
if not doc.endswith(".txt"):
continue
# get document text
text = file_reader.read()
d.text = text
# get document keywords
file_reader = open(os.path.join(input_dir,file_name + ".key"), 'r')
manual_keywords = file_reader.read()
for line in manual_keywords.split('\n'):
line = line.rstrip().lower()
if len(line) > 0:
if '\t' in line:
d.keywords.append(line[0:line.find('\t')])
else:
d.keywords.append(line)
# add document to test set
test_set[file_name] = d
return test_set