-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.py
182 lines (146 loc) · 6.19 KB
/
reader.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
176
177
178
179
180
181
182
import os
import re
import time
import nltk
import pickle
import logging
import pymorphy2
from docx import Document
from nltk.corpus.reader.api import CorpusReader, CategorizedCorpusReader
from nltk import pos_tag, sent_tokenize, wordpunct_tokenize
import logging
import sys
from logging.handlers import TimedRotatingFileHandler
FORMATTER = logging.Formatter("%(asctime)s — %(name)s — %(levelname)s — %(message)s")
LOG_FILE = "my_app.log"
def get_console_handler():
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(FORMATTER)
return console_handler
def get_file_handler():
file_handler = TimedRotatingFileHandler(LOG_FILE, when='midnight')
file_handler.setFormatter(FORMATTER)
return file_handler
def get_logger(logger_name):
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG) # better to have too much log than not enough
logger.addHandler(get_console_handler())
logger.addHandler(get_file_handler())
# with this pattern, it's rarely necessary to propagate the error up to parent
logger.propagate = False
return logger
CAT_PATTERN = r'/\d+'
DOC_PATTERN = r'(?!\.)\w+\.docx'
PKL_PATTERN = r'(?!\.)[\d\s]+/[\w\s]+\.pickle'
DIAGNOS_PATTERN = 'Клинический диагноз'
class PickledCorpusReader(CategorizedCorpusReader, CorpusReader):
def __init__(self, root, fileids=PKL_PATTERN, **kwargs):
"""
Initialize the corpus reader. Categorization arguments
(``cat_pattern``, ``cat_map``, and ``cat_file``) are passed to
the ``CategorizedCorpusReader`` constructor. The remaining arguments
are passed to the ``CorpusReader`` constructor.
"""
# Add the default category pattern if not passed into the class.
if not any(key.startswith('cat_') for key in kwargs.keys()):
kwargs['cat_pattern'] = CAT_PATTERN
CategorizedCorpusReader.__init__(self, kwargs)
CorpusReader.__init__(self, root, fileids)
def resolve(self, fileids, categories):
"""
Returns a list of fileids or categories depending on what is passed
to each internal corpus reader function. This primarily bubbles up to
the high level ``docs`` method, but is implemented here similar to
the nltk ``CategorizedPlaintextCorpusReader``.
"""
if fileids is not None and categories is not None:
raise ValueError("Specify fileids or categories, not both")
if categories is not None:
return self.fileids(categories)
return fileids
def docs(self, fileids=None, categories=None):
"""
Returns the document loaded from a pickled object for every file in
the corpus. Similar to the BaleenCorpusReader, this uses a generator
to acheive memory safe iteration.
"""
# Resolve the fileids and the categories
fileids = self.resolve(fileids, categories)
# Create a generator, loading one document into memory at a time.
for path in self.abspaths(fileids):
with open(path, 'rb') as f:
yield pickle.load(f)
def paras(self, fileids=None, categories=None):
"""
Returns a generator of paragraphs where each paragraph is a list of
sentences, which is in turn a list of (token, tag) tuples.
"""
for doc in self.docs(fileids, categories):
for paragraph in doc['text']:
yield paragraph
def sents(self, fileids=None, categories=None):
"""
Returns a generator of sentences where each sentence is a list of
(token, tag) tuples.
"""
for paragraph in self.paras(fileids, categories):
for sentence in paragraph:
yield sentence
def tables(self, fileids=None, categories=None):
for doc in self.docs(fileids, categories):
for table in doc['tables']:
yield table
def tagged(self, fileids=None, categories=None):
for sent in self.sents(fileids, categories):
for token in sent:
yield token
def words(self, fileids=None, categories=None):
"""
Returns a generator of (token, tag) tuples.
"""
for token in self.tagged(fileids, categories):
yield token[0]
def describe(self, fileids=None, categories=None):
"""
Performs a single pass of the corpus and
returns a dictionary with a variety of metrics
concerning the state of the corpus.
"""
started = time.time()
# Structures to perform counting.
counts = nltk.FreqDist()
tokens = nltk.FreqDist()
# Perform single pass over paragraphs, tokenize and count
for para in self.paras(fileids, categories):
counts['paras'] += 1
for sent in sent_tokenize(para):
counts['sents'] += 1
for word in wordpunct_tokenize(sent):
counts['words'] += 1
tokens[word] += 1
# Compute the number of files and categories in the corpus
n_fileids = len(self.fileids())
# n_topics = len(self.categories(self.resolve(fileids, categories)))
# Return data structure with information
return {
'files': n_fileids,
# 'topics': n_topics,
'paras': counts['paras'],
'sents': counts['sents'],
'words': counts['words'],
'vocab': len(tokens),
'lexdiv': float(counts['words']) / float(len(tokens)),
'ppdoc': float(counts['paras']) / float(n_fileids),
'sppar': float(counts['sents']) / float(counts['paras']),
'secs': time.time() - started,
}
logger = get_logger(__name__)
if __name__ == '__main__':
from collections import Counter
corpus = PickledCorpusReader('/home/igor/Development/diabetes/test_corpus_with_tables')
# tables = list(corpus.tables())
for file_id in corpus.fileids():
logger.info(f"start processing {file_id}")
print(list(corpus.paras(file_id))[:12])
# words = Counter(corpus.words())
# print("{:,} vocabulary {:,} word count".format(len(words.keys()), sum(words.values())))