Example #1
0
def check( code_map_entry ) :
    code_map = CodeMap()
    code_map.entry = code_map_entry
    while( 1 ) :
        code = sys.stdin.readline()[:-1]
        flag = -1
        time_stamp = time.time()
        cache = QueryCache( code_map, code )
        print "query cast", time.time() - time_stamp, "s"
        if cache.vaild() :
            cand = cache.get_cand( 0, 6 )
            for index in range( len( cand ) ) :
                code, pinyin, hanzi, freq = cache.get_prop( index )
                print code, pinyin, hanzi, freq
 def __init__( self ) :
     code_map = load_code_map( self.CODE_MAP_FILE_PATH )
     self.cache = QueryCache( code_map )
     self.invailed_code = ""
     self.page_index = 0
     self.cand_list = []
     self.selected = SelectedStack()
     self.get_pinyin_list = self.cache.get_pinyin_list
Example #3
0
def stored_cache():
    scenario = request.args.get('scenario')
    #get the input
    print('Scenario: ',scenario)
    if scenario != '':
        try:
            #initiate the query cache process
            querycacheobj = QueryCache(scenario)
            fnlqrycachejson = querycacheobj.searchquery()

            if (fnlqrycachejson['data'] != ''):
                return constructresp(200, 42, 'SUCCESS', scenario, fnlqrycachejson['data'])
            else:
                return constructresp(400, 42, 'QUERY_CACHE_ERROR', scenario, fnlqrycachejson['error'])
        except ValueError:
            return constructresp(400, 42, 'QUERY_CACHE_ERROR', scenario, 'Application unable to process the input')
    else:
        return constructresp(400, 42, 'MISSING_SCENARIO', scenario, 'Empty \'scenario\' paramater')
Example #4
0
def findqueryusingscenario(scenario):
    output = {'data': '' ,'error': ''}
    if scenario != '':
        try:
            #initiate the query cache process
            querycacheobj = QueryCache(scenario)
            fnlqrycachejson = querycacheobj.searchquery()

            if (fnlqrycachejson['data'] != ''):
                output['data'] = fnlqrycachejson['data']
            else:
                output['error'] = fnlqrycachejson['error']
            return output
        except ValueError:
            output['error'] = 'QUERY_CACHE_ERROR'
            return output          
    else:
        output['error'] = 'MISSING_SCENARIO'
        return output
class Backend() :
    CAND_LENGTH = 6
    CODE_MAP_FILE_PATH = "/opt/mcip/dict/dict.0"
    #CODE_MAP_FILE_PATH = "../dict/dict.0"
    def __init__( self ) :
        code_map = load_code_map( self.CODE_MAP_FILE_PATH )
        self.cache = QueryCache( code_map )
        self.invailed_code = ""
        self.page_index = 0
        self.cand_list = []
        self.selected = SelectedStack()
        self.get_pinyin_list = self.cache.get_pinyin_list
    def set_filter( self, pinyin ) :
        self.page_index = 0
        self.cache.set_filter( pinyin )
    def set_code( self, code ) :
        vaild_code = self.cache.set( code )
        self.invailed_code = code[ len( vaild_code ) : ]
        self.page_index = 0
    def append( self, code ) :
        if len( self.invailed_code ) > 0 :
            self.invailed_code = self.invailed_code + code
        else :
            flag = self.cache.append( code )
            if not flag :
                self.invailed_code = self.invailed_code + code
            self.page_index = 0
    def pop( self ) :
        if len( self.invailed_code ) > 0 :
            code = self.invailed_code[-1]
            self.invailed_code = self.invailed_code[:-1]
        else :
            code = self.cache.pop()
            self.page_index = 0
        return code
    def gen_cand_list( self ) :
        start_index = self.page_index * self.CAND_LENGTH
        request_length = start_index + self.CAND_LENGTH
        cand_length = self.cache.gen_cand( request_length )
        #print self.code(), "cand_length", cand_length
        self.cand_list = []
        if request_length < cand_length :
            cand_length = request_length
        if start_index < cand_length :
            for index in range( start_index, cand_length ) :
                #print self.code(), "index", index
                code, pinyin, hanzi, freq = self.cache.get_prop( index )
                self.cand_list.append( [ code, pinyin, hanzi.decode( "utf-8" ) ] )
    def commit( self ) :
        self.invailed_code = ""
        self.cache.set("")
        self.selected.clear()
    def code( self ) :
        return self.cache.code + self.invailed_code
    def get_selected( self ) :
        code, pinyin, hanzi = self.selected.get()
        return hanzi.decode( "utf-8" )
    def deselect( self ) :
        code = self.selected.pop()
        if len( code ) > 0 :
            #print code, self.code()
            code = code + self.code()
            self.set_code( code )
        return code
    def select( self, index ) :
        index = self.CAND_LENGTH * self.page_index + index
        remained_code, code, cache, cand_index = self.cache.select( index )
        self.selected.push( code, cache, cand_index )
        remained_code = remained_code + self.invailed_code
        self.set_code( remained_code )
    def page_next( self ) :
        page_index = self.page_index + 1
        start_index = page_index * self.CAND_LENGTH
        cand_length = self.cache.gen_cand( start_index + self.CAND_LENGTH )
        if cand_length > start_index :
            self.page_index = page_index
    def page_prev( self ) :
        if self.page_index > 0 :
            self.page_index = self.page_index - 1