import socket import sys,re # -*- coding: utf-8 -*- def tounicode(data): f = lambda d, enc: d.decode(enc); codecs = ['utf-8','shift_jis','euc_jp','cp932', 'euc_jis_2004','euc_jisx0213','iso2022_jp','iso2022_jp_1', 'iso2022_jp_2','iso2022_jp_2004','iso2022_jp_3','iso2022_jp_ext', 'shift_jis_2004','shift_jisx0213','utf_16','utf_16_be', 'utf_16_le','utf_7','utf_8_sig']; for codec in codecs: try: return f(data, codec); except: continue; return None; def convert_uni2enc(data,enc): data1 = tounicode(data); data2 = ''; for char in data1[:]: try: data2 += char.encode(enc); except: pass; return data2; if __name__ == '__main__': host = "localhost" port = 10500 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, port)) print "Connection to Julius succeeded" sys.stdout.flush() # clear the log file #handle = open("julius_out.txt", "w") #handle.close() word = {} angle = {} parsed = [] currentID = None line = "" while True: # build a valid line tmpline = sock.recv(1024).replace(".\n", "").rstrip() line += tmpline if not tmpline[-1] == ">": continue # make sure line is unicode line = tounicode(line) # parse it. res = re.search( '