Skip to content

Commit ddec01a

Browse files
committed
trivial fix
1 parent 420819b commit ddec01a

18 files changed

+73
-172
lines changed

EECS767_05032016.pptx

-136 Bytes
Binary file not shown.

QueryWithTF.pyc

1.44 KB
Binary file not shown.

candidateVector.pyc

400 Bytes
Binary file not shown.

candidatefile.pyc

1.04 KB
Binary file not shown.

inverted_index.pyc

2.56 KB
Binary file not shown.

locate_terms.pyc

1.41 KB
Binary file not shown.

minWindow.pyc

2.61 KB
Binary file not shown.

php_python.py

100644100755
+5-50
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,24 @@
1-
#!/usr/bin/env python3
21
# -*- coding: UTF-8 -*-
32

43
import time
54
import signal
65
import sys
76
import socket
87
import os
9-
108
import process
119
import utils
1210

13-
#import togetherIndiceResult
14-
15-
# -------------------------------------------------
16-
# 基本配置
17-
# -------------------------------------------------
18-
LISTEN_PORT = 21230 #服务侦听端口
19-
CHARSET = "utf-8" #设置字符集(和PHP交互的字符集)
20-
21-
2211

23-
# -------------------------------------------------
24-
# Oracle数据库连接配置
25-
# -------------------------------------------------
26-
# import cx_Oracle
27-
# 数据库字符集
28-
# os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
29-
# 数据库连接池
30-
# pool = cx_Oracle.SessionPool(
31-
# user='diaoyf',
32-
# password='700327',
33-
# dsn='127.0.0.1/xe',
34-
# min=5,
35-
# max=10,
36-
# increment=1,
37-
# connectiontype=cx_Oracle.Connection,
38-
# threaded=True,
39-
# getmode=cx_Oracle.SPOOL_ATTRVAL_NOWAIT,
40-
# homogeneous=True)
12+
LISTEN_PORT = 21230
13+
CHARSET = "utf-8"
4114

42-
# def getConn():
43-
# """获得数据库连接的公共函数"""
44-
# return pool.acquire()
45-
46-
# def closeConn(conn):
47-
# """释放数据库连接的公共函数"""
48-
# pool.release(conn)
49-
50-
51-
# -------------------------------------------------
52-
# 主程序
53-
# 请不要随意修改下面的代码
54-
# -------------------------------------------------
15+
#build connect between php and pyhon
5516
if __name__ == '__main__':
5617

57-
print ("-------------------------------------------")
5818
print ("- PHP-Python Connection")
5919
print ("- Time: %s" % time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) )
60-
print ("-------------------------------------------")
6120

62-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP/IP
21+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6322
sock.bind(('', LISTEN_PORT))
6423
sock.listen(5)
6524

@@ -69,15 +28,11 @@
6928

7029
utils.init_all_data()
7130
print ("Data Loaded!")
72-
#togetherIndiceResult.together_indice_result('ku bast')
7331

7432

7533
while 1:
76-
connection,address = sock.accept() #收到一个请求
77-
78-
#print ("client's IP:%s, PORT:%d" % address)
34+
connection,address = sock.accept()
7935

80-
# 处理线程
8136
try:
8237
process.ProcessThread(connection).start()
8338
except:

php_python.pyc

1.01 KB
Binary file not shown.

preprocess.pyc

2.75 KB
Binary file not shown.

process.py

+50-104
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# -*- coding: UTF-8 -*-
2+
3+
# -*- coding: UTF-8 -*-
4+
#-----------------------------------------------------------
5+
# PPython(PHP and Python).
6+
# (2012-15 http://code.google.com/p/ppython/)
7+
#
8+
# License: http://www.apache.org/licenses/LICENSE-2.0
9+
#-----------------------------------------------------------
10+
211
import sys
312
import time
413
import threading
@@ -10,14 +19,12 @@
1019
REQUEST_MIN_LEN = 10
1120
TIMEOUT = 180
1221

13-
pc_dict = {} #预编译字典,key:调用模块、函数、参数字符串,值是编译对象
14-
global_env = {} #global环境变量
22+
pc_dict = {}
23+
global_env = {}
1524

25+
# find position of c in bytes
26+
# pos: the start position
1627
def index(bytes, c, pos=0):
17-
"""
18-
查找c字符在bytes中的位置(从0开始),找不到返回-1
19-
pos: 查找起始位置
20-
"""
2128
for i in range(len(bytes)):
2229
if (i <= pos):
2330
continue
@@ -28,48 +35,42 @@ def index(bytes, c, pos=0):
2835
return -1
2936

3037

38+
# encode parameters from python to php
3139
def z_encode(p):
32-
"""
33-
encode param from python data
34-
"""
35-
if p == None: #None->PHP中的NULL
40+
if p == None: #python None to PHP NULL
3641
return "N;"
37-
elif isinstance(p, int): #int->PHP整形
42+
elif isinstance(p, int): #python int to PHP int
3843
return "i:%d;" % p
39-
elif isinstance(p, str): #String->PHP字符串
44+
elif isinstance(p, str): #python String to PHP String
4045
p_bytes = p.encode(php_python.CHARSET);
4146
ret = 's:%d:"' % len(p_bytes)
4247
ret = ret.encode(php_python.CHARSET)
4348
ret = ret + p_bytes + '";'.encode(php_python.CHARSET)
4449
#ret = str(ret, php_python.CHARSET)
4550
ret = str(ret)
4651
return ret
47-
elif isinstance(p, bool): #boolean->PHP布尔
52+
elif isinstance(p, bool): #python boolean to PHP boolean
4853
b=1 if p else 0
4954
return 'b:%d;' % b
50-
elif isinstance(p, float): #float->PHP浮点
55+
elif isinstance(p, float): #python float to PHP float
5156
return 'd:%r;' % p
52-
elif isinstance(p, list) or isinstance(p, tuple): #list,tuple->PHP数组(下标int)
57+
elif isinstance(p, list) or isinstance(p, tuple): #python list,tuple to PHP array with int indice
5358
s=''
5459
for pos,i in enumerate(p):
5560
s+=z_encode(pos)
5661
s+=z_encode(i)
5762
return "a:%d:{%s}" % (len(p),s)
58-
elif isinstance(p, dict): #字典->PHP数组(下标str)
63+
elif isinstance(p, dict): #python dictionary to PHP array with string indice
5964
s=''
6065
for key in p:
6166
s+=z_encode(key)
6267
s+=z_encode(p[key])
6368
return "a:%d:{%s}" % (len(p),s)
64-
else: #其余->PHP中的NULL
69+
else: #Other types in python to PHP NULL
6570
return "N;"
6671

67-
72+
#decode php parameters from string to python
6873
def z_decode(p):
69-
"""
70-
decode php param from string to python
71-
p: bytes
72-
"""
7374
if p[0]==chr(0x4e): #NULL 0x4e-'N'
7475
return None,p[2:]
7576
elif p[0]==chr(0x62): #bool 0x62-'b'
@@ -91,18 +92,18 @@ def z_decode(p):
9192
#return str(v, php_python.CHARSET), p[end+1:]
9293
return v.encode(php_python.CHARSET), p[end+1:]
9394
elif p[0]==chr(0x61): #array 0x61-'a'
94-
list_=[] #数组
95-
dict_={} #字典
96-
flag=True #类型,true-元组 false-字典
95+
list_=[] #array
96+
dict_={} #dictionary
97+
flag=True #true-tuple false-dictionary
9798
second = index(p, chr(0x3a), 2) # 0x3a-":"
98-
num = int(p[2:second]) #元素数量
99-
pp = p[second+2:] #所有元素
99+
num = int(p[2:second]) #number of elements
100+
pp = p[second+2:] #all elements
100101
for i in range(num):
101-
key,pp=z_decode(pp) #key解析
102-
if (i == 0): #判断第一个元素key是否int 0
102+
key,pp=z_decode(pp) #key decode
103+
if (i == 0):
103104
if (not isinstance(key, int)) or (key != 0):
104105
flag = False
105-
val,pp=z_decode(pp) #value解析
106+
val,pp=z_decode(pp)
106107
list_.append(val)
107108
dict_[key]=val
108109
return (list_, pp[2:]) if flag else (dict_, pp[2:])
@@ -111,144 +112,89 @@ def z_decode(p):
111112

112113

113114
def parse_php_req(p):
114-
"""
115-
解析PHP请求消息
116-
返回:元组(模块名,函数名,入参list)
117-
"""
118115
while p:
119-
v,p=z_decode(p) #v:值 p:bytes(每次z_decode计算偏移量)
116+
v,p=z_decode(p)
120117
params = v
121118

122-
modul_func = params[0] #第一个元素是调用模块和函数名
123-
#print("模块和函数名:%s" % modul_func)
124-
#print("参数:%s" % params[1:])
119+
modul_func = params[0]
125120
pos = modul_func.find("::")
126-
modul = modul_func[:pos] #模块名
127-
func = modul_func[pos+2:] #函数名
121+
modul = modul_func[:pos]
122+
func = modul_func[pos+2:]
128123
return modul, func, params[1:]
129124

130125

131126
class ProcessThread(threading.Thread):
132-
"""
133-
preThread 处理线程
134-
"""
135127
def __init__(self, socket):
136128
threading.Thread.__init__(self)
137-
138-
#客户socket
139129
self._socket = socket
140130

141-
def run(self):
142-
143-
#---------------------------------------------------
144-
# 1.接收消息
145-
#---------------------------------------------------
146-
131+
def run(self):
147132
try:
148-
self._socket.settimeout(TIMEOUT) #设置socket超时时间
149-
firstbuf = self._socket.recv(16 * 1024) #接收第一个消息包(bytes)
150-
if len(firstbuf) < REQUEST_MIN_LEN: #不够消息最小长度
151-
#print ("非法包,小于最小长度: %s" % firstbuf)
133+
self._socket.settimeout(TIMEOUT)
134+
firstbuf = self._socket.recv(16 * 1024)
135+
if len(firstbuf) < REQUEST_MIN_LEN:
152136
print 'error message,less than minimum length:',firstbuf
153137
self._socket.close()
154138
return
155139

156-
firstComma = index(firstbuf, chr(0x2c)) #查找第一个","分割符
157-
#firstComma = index(firstbuf, ',')
140+
firstComma = index(firstbuf, chr(0x2c))
158141
print firstbuf
159-
totalLen = int(firstbuf[0:firstComma]) #消息包总长度
160-
#print("消息长度:%d" % totalLen)
142+
totalLen = int(firstbuf[0:firstComma])
161143
print 'message length:',totalLen
162144
reqMsg = firstbuf[firstComma+1:]
163145
print 'reqMsg:',reqMsg
164146
while (len(reqMsg) < totalLen):
165147
reqMsg = reqMsg + self._socket.recv(16 * 1024)
166-
167-
#调试
168-
#print ("请求包:%s" % reqMsg)
169-
170148
except Exception,e:
171-
#print ('接收消息异常', e)
172149
print 'getMessage error',str(e)
173150
self._socket.close()
174151
return
175-
176-
#---------------------------------------------------
177-
# 2.调用模块、函数检查,预编译。
178-
#---------------------------------------------------
179-
180-
#从消息包中解析出模块名、函数名、入参list
181152
modul, func, params = parse_php_req(reqMsg)
182153
print 'module:',modul,'func:',func,'parmas:',params
183154

184-
if (modul not in pc_dict): #预编译字典中没有此编译模块
185-
#检查模块、函数是否存在
155+
if (modul not in pc_dict):
186156
try:
187-
callMod = __import__ (modul) #根据module名,反射出module
188-
pc_dict[modul] = callMod #预编译字典缓存此模块
157+
callMod = __import__ (modul)
158+
pc_dict[modul] = callMod
189159
except Exception,e:
190160
print 'module not exist:',modul
191-
#print ('模块不存在:%s' % modul)
192-
self._socket.sendall(("F" + "module '%s' is not exist!" % modul).encode(php_python.CHARSET)) #异常
161+
self._socket.sendall(("F" + "module '%s' is not exist!" % modul).encode(php_python.CHARSET))
193162
self._socket.close()
194163
return
195164
else:
196-
callMod = pc_dict[modul] #从预编译字典中获得模块对象
165+
callMod = pc_dict[modul]
197166

198167
try:
199168
callMethod = getattr(callMod, func)
200169
except Exception,e:
201170
print 'function not exist:',func
202-
#print ('函数不存在:%s' % func)
203171
self._socket.sendall(("F" + "function '%s()' is not exist!" % func).encode(php_python.CHARSET)) #异常
204172
self._socket.close()
205173
return
206-
207-
#---------------------------------------------------
208-
# 3.Python函数调用
209-
#---------------------------------------------------
210-
211174
try:
212175
params = ','.join([repr(x) for x in params])
213-
#print ("调用函数及参数:%s(%s)" % (modul+'.'+func, params) )
214-
215-
#加载函数
216176
compStr = "import %s\nret=%s(%s)" % (modul, modul+'.'+func, params)
217-
#print("函数调用代码:%s" % compStr)
218177
rpFunc = compile(compStr, "", "exec")
219178

220179
if func not in global_env:
221180
global_env[func] = rpFunc
222181
local_env = {}
223-
exec (rpFunc, global_env, local_env) #函数调用
224-
#print (global_env)
225-
#print (local_env)
182+
exec (rpFunc, global_env, local_env)
226183
except Exception,e:
227-
#print ('调用Python业务函数异常', e )
228184
print 'call python error:',str(e)
229185
errType, errMsg, traceback = sys.exc_info()
230-
self._socket.sendall(("F%s" % errMsg).encode(php_python.CHARSET)) #异常信息返回
186+
self._socket.sendall(("F%s" % errMsg).encode(php_python.CHARSET))
231187
self._socket.close()
232188
return
233-
234-
#---------------------------------------------------
235-
# 4.结果返回给PHP
236-
#---------------------------------------------------
237-
#retType = type(local_env['ret'])
238-
#print ("函数返回:%s" % retType)
239-
rspStr = z_encode(local_env['ret']) #函数结果组装为PHP序列化字符串
189+
rspStr = z_encode(local_env['ret'])
240190

241191
try:
242-
#加上成功前缀'S'
243192
rspStr = "S" + rspStr
244-
#调试
245-
#print ("返回包:%s" % rspStr)
246193
self._socket.sendall(rspStr.encode(php_python.CHARSET))
247194
except Exception,e:
248195
print 'send message error:',str(e)
249-
#print ('发送消息异常', e)
250196
errType, errMsg, traceback = sys.exc_info()
251-
self._socket.sendall(("F%s" % errMsg).encode(php_python.CHARSET)) #异常信息返回
197+
self._socket.sendall(("F%s" % errMsg).encode(php_python.CHARSET))
252198
finally:
253199
self._socket.close()
254200
return

process.pyc

5.9 KB
Binary file not shown.

relevance_feedback.pyc

941 Bytes
Binary file not shown.

similarity.pyc

1.55 KB
Binary file not shown.

tfidf_for_Query.pyc

1.95 KB
Binary file not shown.

0 commit comments

Comments
 (0)