# -*- coding: utf-8 -*- # vim: set noet ts=4: # # Name: FunInputClient # # Copyright: Copyright (c) 2007-2008 Huajun Feng # # $Id: # # Author: fenghuajun@gmail.com (Feng Huajun) # """Description of this module""" __author__ = "fenghuajun@gmail.com (Feng Huajun)" import socket import os import sys from syslog import syslog class Word: def __init__ (self,string, code="", hit=0, freq=0, isNew=1, wordId=0): if code=="": tmp = string.split(",") code = tmp[0] string = tmp[1] hit = int(tmp[2]) freq = int(tmp[3]) isNew = int(tmp[4]) wordId = int(tmp[5]) self._string = string self._code = code self._hit = hit self._freq = freq self._isNew = isNew self._wordId = wordId def toString(self): s = self._code + "," +self._string s += ","+str(self._hit)+","+str(self._freq)+","+str(self._isNew)+","+str(self._wordId) return s def __cmp__(self, other): c = self._hit - other._hit if c!=0: return c c = self._freq - other._freq if c!=0: return c return 0 def __repr__(self): return "%s %s"%(self._code,self._string) class FunInputClient(object): """FIT input client class""" def __init__(self, path): """Constructor to init the client object. Note: Args: path: """ self._path = path @property def path(self): """Return the socket file used for this server""" return self._path def _start_fitx(self): if not os.path.exists(self._path): syslog('restart fitx') os.system("fitx -f " + self._path + " &") else: syslog("%s exist!" % self._path) def request(self, req): """send request to FIT server Args: req: a request string. Returns: unicode data string sent back from server """ try: #syslog(req) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(self._path) req = req.encode("utf-8") sock.send(req) data = sock.recv(1024000) data = unicode(data,"utf-8").strip() sock.close() return data except: self._start_fitx() def fix(self, astr): """Fix request. Args: astr: a word string for requesting. Returns: return value from request call. """ return self.request("fix " + astr) def search(self, astr): """Search request. Args: astr: a word string for requesting. Returns: a list of word objects. """ #fix server exit when inputting some upper chars if(not astr.islower()):return []; r = self.request("search " + astr) if not r: return [] wl=r.split(" ") rwl=[] #use this to fix server error( if you input 'zz', the candidate words are too many, and the last will be 'zh',which is not a valid format of the word) for w in wl: wcl=w.split(",") if len(wcl)==6: rwl.append(Word(wcl[1],wcl[0],int(wcl[2]),int(wcl[3]),int(wcl[4]),int(wcl[5]))) #return [Word(string=s) for s in ] return rwl def add(self, ws): """Add request. Args: a list of word object. """ req = "add " + " ".join(['%s' % i.toString() for i in ws]) self.request(req) def remove(self, w): """remove request Args: word: a word object. """ #c=str(w._code) #w._code=c req = "remove %s" % w.toString() #req=req.decode("utf-8") self.request(req) def reload(self): """Reload request. """ self.request("reload") def release(self): """Release request. """ try: self.request("exit") except: pass