""" Classes for mapping Korp keyword in context (kwic) search results into Python objects. Author: Richard Johansson """ import random from ling_units import Token, Sentence class match: def __init__(self, t): self.position = int(t['position']) self.start = int(t['start']) self.end = int(t['end']) def __str__(self): return "(" + str(self.position) + ", " + str(self.start) + ", " + str(self.end) + ")" class structs: def __init__(self, t): self.sentence_id = t['sentence_id'] def __str__(self): return "(" + str(self.sentence_id) + ")" class kwic: def __init__(self, t): self.index = None self.source = t self.corpus = t['corpus'] try: self.match = match(t['match']) except KeyError: #to handle corpora not yet in Korp (no 'match') self.match = "" try: self.structs = structs(t['structs']) except KeyError: print "No 'structs attribute'" self.structs = "-" self.sentence = Sentence(t['tokens'], kwic_json=True) def __str__(self): return "[KWIC: match = " + str(self.match) + ": " + str(self.sentence) + "]" def get_source(self): return self.source class search_result: def __init__(self, t): #self.nhits = int(t['hits']) if not t.has_key('kwic'): print "Error: no kwic" print t exit(1) else: self.kwics = map(kwic, t['kwic']) self.corpus_hits = t['corpus_hits'] self.time = t['time'] if t.has_key('hits'): self.nhits = int(t['hits']) #print "Length:", len(self.kwics)