-
Notifications
You must be signed in to change notification settings - Fork 4
/
build_entity_corpus.py
37 lines (27 loc) · 961 Bytes
/
build_entity_corpus.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
__author__ = 'matias'
from gensim import corpora
import os
from textanalysis.texts import CaseReportLibrary
from textanalysis.Analyzers import EntityAnalyzer
def create_entity_corpus():
data_folder = os.path.join(*[os.path.dirname(__file__), 'data', 'corpora'])
analyzer = EntityAnalyzer()
docs = []
count = 1
max_count = 50000
for case in CaseReportLibrary():
text = case.get_text()
# get symptom and disease entities
tokens = analyzer.parse(text)
docs.append(tokens)
count += 1
if count % 100 == 0:
print count,"/",max_count
if count >= max_count:
break
dictionary = corpora.Dictionary(docs)
corpus = [dictionary.doc2bow(doc) for doc in docs]
dictionary.save(os.path.join(data_folder, 'entity.dict'))
corpora.MmCorpus.serialize(os.path.join(data_folder, 'entity.mm'), corpus)
if __name__ == "__main__":
create_entity_corpus()