<s> from collections import defaultdict <EOL> import json <EOL> import os . path <EOL> import numpy as np <EOL> from vsm . viewer . wrappers import doc_label_name , def_label_fn <EOL> lda_v = None <EOL> metadata = None <EOL> context_type = None <EOL> class keydefaultdict ( defaultdict ) : <EOL> def __missing__ ( self , key ) : <EOL> if self . default_factory is None : <EOL> raise KeyError ( key ) <EOL> else : <EOL> ret = self [ key ] = self . default_factory ( key ) <EOL> return ret <EOL> ctx_md = keydefaultdict ( lambda ctx : lda_v . corpus . view_metadata ( ctx ) ) <EOL> def init ( viewer , config , args ) : <EOL> global lda_v <EOL> global context_type <EOL> lda_v = viewer <EOL> context_type = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def label ( doc ) : <EOL> global ctx_md <EOL> context_md = ctx_md [ '<STR_LIT>' ] <EOL> where = np . squeeze ( np . where ( np . in1d ( context_md [ '<STR_LIT>' ] , [ str ( doc ) ] ) ) ) <EOL> print where , context_md <EOL> page_no = context_md [ where ] <EOL> print where , page_no [ '<STR_LIT:title>' ] <EOL> return page_no [ '<STR_LIT:title>' ] <EOL> def id_fn ( md ) : <EOL> context_md = lda_v . corpus . view_metadata ( context_type ) <EOL> ctx_label = doc_label_name ( context_type ) <EOL> return context_md [ ctx_label ] </s>
