-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindexer.py
53 lines (45 loc) · 2.19 KB
/
indexer.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
import codecs
import os
import ijson
from whoosh import index
from whoosh.fields import Schema, TEXT, ID, KEYWORD
from whoosh.analysis import SimpleAnalyzer
from pattern.en import wordnet as WN
u = unicode
SCHEMA = Schema(motif = ID(stored=True, unique=True),
description = TEXT(analyzer=SimpleAnalyzer(),
field_boost=2.0, stored=True, phrase=True),
additional = TEXT(analyzer=SimpleAnalyzer(),
field_boost=1.5, stored=True, phrase=True),
wn = KEYWORD(field_boost=1.0, stored=True, lowercase=True,
scorable=True, commas=True),
references = TEXT(analyzer=SimpleAnalyzer(), field_boost=0.5,
stored=True, phrase=True),
location = KEYWORD(field_boost=0.5, stored=True, lowercase=True,
scorable=True, commas=True))
if __name__ == '__main__':
if not os.path.exists('index'):
os.mkdir('index')
ix = index.create_in('index', schema = SCHEMA, indexname="tmi")
ix = index.open_dir('index', indexname='tmi')
writer = ix.writer()
with open("data/tmi.json") as infile:
for item in ijson.items(infile, "item"):
keywords = set()
for lemma in item['lemmas']:
if not lemma: continue
syn_id = 0 if lemma != 'white' else 1
try:
hypernyms = WN.synsets(lemma, 'NN')[syn_id].hypernyms(recursive=True)
hypernyms = [set(w.senses) for w in hypernyms]
if hypernyms:
keywords.update(reduce(set.union, hypernyms))
except (IndexError, ValueError):
pass
writer.add_document(motif = u(item['motif']),
description = u(item['description']),
additional = u(item['additional_description']),
wn = u(', '.join(keywords)),
references=u(item['references']),
location=u(', '.join(item['locations'])))
writer.commit()