项目实训工作记录(四)赵乐乐 201800301115
下面展示近期工作: 基于关键词的语义泛化系统聚类算法部分代码
。
for line in f:
if line.startswith("8824330"):
continue
array = line.strip().split(' ')
vector = [float(i)for i in array[1:]]
absv = abs(vector)
change=0
if clu_num == 0:
clu_num=1
cluster=clu_num
change=1
else:
for i in range(clu_num):
if cos(vector,a[i][1],absv,a[i][2])>classstandard:
change=-1
cluster=a[i][0]
break
if change==0:
clu_num=clu_num+1
cluster=clu_num
change=1
sql1='insert into words(word,cluster_id,vector,abs) values(%s,%s,"%s",%s)'
values=(array[0],cluster,array[1:],absv)
cursor.execute(sql1,values)
if change==1:
sql2 = 'insert into clusters(cluster_id,vector,abs) values(%s,"%s",%s)'
values = (cluster, array[1:], absv)
cursor.execute(sql2, values)
a.append([cluster,vector,absv])
db.commit()