import_predict.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. # -*- coding: utf-8 -*-
  2. # DEPRECATED(Phase1): 本文件中的 PostgreSQL 硬编码连接(host=192.168.*,
  3. # password=postgres 等)将在后续 training/ Phase 迁移到 BiddingKG.dl.infra.db。
  4. # 迁移完成前可临时使用:from BiddingKG.dl.infra.db import get_connection;
  5. # conn = get_connection("<dbname>")
  6. # 详见 ARCHITECTURE.md 第 11 章 Phase 1 与 REFACTOR_LOG.md。
  7. import codecs
  8. import psycopg2
  9. #此文件是作导入导出数据用
  10. def importPredict():
  11. file = "predict.txt"
  12. conn = psycopg2.connect(dbname="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101")
  13. cursor = conn.cursor()
  14. cursor.execute(" delete from dl_predict ")
  15. with codecs.open(file,"r",encoding="utf8") as f:
  16. while(True):
  17. line = f.readline()
  18. if not line:
  19. break
  20. line_split = line.split("\t")
  21. entity_id=line_split[0]
  22. dl_predict = line_split[1]
  23. sql = " insert into dl_predict(entity_id,dl_predict) values('"+str(entity_id)+"',"+str(dl_predict)+")"
  24. cursor.execute(sql)
  25. f.close()
  26. conn.commit()
  27. conn.close()
  28. def importIterateLabel():
  29. file = "final_label.txt"
  30. conn = psycopg2.connect(dbname="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101")
  31. cursor = conn.cursor()
  32. cursor.execute(" delete from final_label ")
  33. with codecs.open(file,"r",encoding="utf8") as f:
  34. while(True):
  35. line = f.readline()
  36. if not line:
  37. break
  38. line_split = line.split("\t")
  39. entity_id=line_split[0]
  40. if int(line_split[2])==1:
  41. label = "1"
  42. else:
  43. label = "-1"
  44. sql = " insert into final_label(entity_id,label) values('"+str(entity_id)+"',"+str(label)+")"
  45. cursor.execute(sql)
  46. f.close()
  47. conn.commit()
  48. conn.close()
  49. def exportResult():
  50. exporttable = " is_wintenderer "
  51. column = " win_tenderer "
  52. conn = psycopg2.connect(dbname="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101")
  53. cursor = conn.cursor()
  54. sql = "select distinct B.doc_id from "+exporttable+" A,entity_mention B,dd_graph_variables_holdout D where A.id=D.variable_id and A.entity_id=B.entity_id"
  55. cursor.execute(sql)
  56. rows = cursor.fetchall()
  57. with codecs.open("testCompare"+exporttable+".html","w",encoding="utf8") as f:
  58. f.write('<html><head>\
  59. <meta http-equiv="Content-Type"\
  60. content="text/html; charset=UTF-8">\
  61. </head>\
  62. <body bgcolor="#FFFFFF">\
  63. <table border="1">\
  64. <tr>\
  65. <td>id</td>\
  66. <td>sumvalue</td>\
  67. <td>sum期望</td>\
  68. <td>ddvalue</td>\
  69. <td>dd期望</td>\
  70. <td>dlvalue<</td>\
  71. <td>dl期望</td>\
  72. <td>标注第一候选</td>\
  73. <td>相比</td>\
  74. </tr>')
  75. for row in rows:
  76. f.write("<tr>")
  77. doc_id = row[0]
  78. sql = " select A.entity_text,B.expectation,B.entity_id from entity_mention A,dl_predict B where A.doc_id='"+doc_id+"' and A.entity_id=B.entity_id order by B.expectation desc limit 1"
  79. cursor.execute(sql)
  80. dd_row = cursor.fetchall()[0]
  81. dd_entity = dd_row[0]
  82. dd_expectation = dd_row[1]
  83. sql = " select A.entity_text,B.dl_predict,B.entity_id from entity_mention A,dl_predict B where A.doc_id='"+doc_id+"' and A.entity_id=B.entity_id order by B.dl_predict desc limit 1"
  84. cursor.execute(sql)
  85. dl_row = cursor.fetchall()[0]
  86. dl_entity = dl_row[0]
  87. dl_expectation = dl_row[1]
  88. sql = " select A.entity_text,B.dl_predict*0.6+B.expectation*0.4 from entity_mention A,dl_predict B where A.doc_id='"+doc_id+"' and A.entity_id=B.entity_id order by B.dl_predict*0.5+B.expectation*0.5 desc limit 1"
  89. cursor.execute(sql)
  90. row = cursor.fetchall()[0]
  91. sum_entity = row[0]
  92. sum_expectation = row[1]
  93. sql = " select "+column+" from articles where id='"+doc_id+"' "
  94. cursor.execute(sql)
  95. re_match = cursor.fetchall()[0][0]
  96. if sum_expectation>0.5:
  97. if sum_entity==re_match:
  98. isSame_sum = "#A0"
  99. else:
  100. isSame_sum = "#A1"
  101. else:
  102. if sum_entity==re_match:
  103. isSame_sum = "#A2"
  104. else:
  105. isSame_sum = "#A3"
  106. if dd_expectation>0.5:
  107. if dd_entity==re_match:
  108. isSame_dd_re = "#B0"
  109. else:
  110. isSame_dd_re = "#B1"
  111. else:
  112. if dd_entity==re_match:
  113. isSame_dd_re = "#B2"
  114. else:
  115. isSame_dd_re = "#B3"
  116. if dl_expectation>0.5:
  117. if dl_entity==re_match:
  118. isSame_dl_re = "#C0"
  119. else:
  120. isSame_dl_re = "#C1"
  121. else:
  122. if dl_entity==re_match:
  123. isSame_dl_re = "#C2"
  124. else:
  125. isSame_dl_re = "#C3"
  126. f.write("<td>"+str(doc_id)+"</td>"+"<td>"+str(sum_entity)+"</td>"+"<td>"+str(sum_expectation)+"</td>"+"<td>"+str(dd_entity)+"</td>"+"<td>"+str(dd_expectation)+"</td>"+"<td>"+str(dl_entity)+"</td>"+"<td>"+str(dl_expectation)+"</td>"+"<td>"+str(re_match)+"</td>"+"<td>"+str(isSame_sum+isSame_dd_re+isSame_dl_re)+"</td>")
  127. f.write("</tr>")
  128. f.write("\n")
  129. f.write('</tr>\
  130. </table>\
  131. </body>\
  132. </html>')
  133. f.flush()
  134. f.close()
  135. conn.close()
  136. def exportDDResult():
  137. exporttable = " is_wintenderer"
  138. column = " win_tenderer "
  139. conn = psycopg2.connect(dbname="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101")
  140. cursor = conn.cursor()
  141. sql = "select distinct B.doc_id from "+exporttable+" A,entity_mention B,dd_graph_variables_holdout D where A.id=D.variable_id and A.entity_id=B.entity_id"
  142. cursor.execute(sql)
  143. rows = cursor.fetchall()
  144. with codecs.open("testCompare"+exporttable+".html","w",encoding="utf8") as f:
  145. f.write('<html><head>\
  146. <meta http-equiv="Content-Type"\
  147. content="text/html; charset=UTF-8">\
  148. </head>\
  149. <body bgcolor="#FFFFFF">\
  150. <table border="1">\
  151. <tr>\
  152. <td>id</td>\
  153. <td>ddvalue</td>\
  154. <td>dd期望</td>\
  155. <td>标注中标</td>\
  156. <td>标注最大期望</td>\
  157. <td>相比</td>\
  158. </tr>')
  159. for row in rows:
  160. f.write("<tr>")
  161. doc_id = row[0]
  162. sql = " select A.entity_text,B.expectation,B.entity_id from entity_mention A,"+exporttable+"_label_inference B where A.doc_id='"+doc_id+"' and A.entity_id=B.entity_id order by B.expectation desc limit 1"
  163. cursor.execute(sql)
  164. dd_row = cursor.fetchall()[0]
  165. dd_entity = dd_row[0]
  166. dd_expectation = dd_row[1]
  167. sql = " select "+column+" from articles where id='"+doc_id+"' "
  168. cursor.execute(sql)
  169. re_match = cursor.fetchall()[0][0]
  170. sql = " select A.entity_text,B.expectation,B.entity_id from entity_mention A,"+exporttable+"_label_inference B where A.doc_id='"+doc_id+"' and A.entity_id=B.entity_id and A.entity_text='"+re_match+"' order by B.expectation desc limit 1"
  171. cursor.execute(sql)
  172. dd_row = cursor.fetchall()
  173. if len(dd_row)>0:
  174. re_expectation = dd_row[0][1]
  175. else:
  176. re_expectation = -1
  177. isSame_dd_re = ""
  178. if re_expectation>0.8:
  179. isSame_dd_re = "#B0"
  180. else:
  181. if dd_entity==re_match:
  182. isSame_dd_re = "#B0"
  183. else:
  184. if dd_expectation>0.8:
  185. isSame_dd_re = "#B1"
  186. f.write("<td>"+str(doc_id)+"</td>"+"<td>"+str(dd_entity)+"</td>"+"<td>"+str(dd_expectation)+"</td>"+"<td>"+str(re_match)+"</td>"+"<td>"+str(re_expectation)+"</td>"+"<td>"+str(isSame_dd_re)+"</td>")
  187. f.write("</tr>")
  188. f.write("\n")
  189. f.write('</tr>\
  190. </table>\
  191. </body>\
  192. </html>')
  193. f.flush()
  194. f.close()
  195. conn.close()
  196. def exportDLResult():
  197. exporttable = " is_wintenderer"
  198. column = " win_tenderer "
  199. conn = psycopg2.connect(dbname="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101")
  200. cursor = conn.cursor()
  201. sql = "select distinct B.doc_id from "+exporttable+" A,entity_mention B where A.entity_id=B.entity_id and B.doc_id in(select id from articles_processed order by id limit 1000)"
  202. cursor.execute(sql)
  203. rows = cursor.fetchall()
  204. with codecs.open("testCompare"+exporttable+".html","w",encoding="utf8") as f:
  205. f.write('<html><head>\
  206. <meta http-equiv="Content-Type"\
  207. content="text/html; charset=UTF-8">\
  208. </head>\
  209. <body bgcolor="#FFFFFF">\
  210. <table border="1">\
  211. <tr>\
  212. <td>id</td>\
  213. <td>dlvalue</td>\
  214. <td>dl期望</td>\
  215. <td>标注中标</td>\
  216. <td>标注最大期望</td>\
  217. <td>相比</td>\
  218. </tr>')
  219. for row in rows:
  220. f.write("<tr>")
  221. doc_id = row[0]
  222. sql = " select A.entity_text,B.dl_predict from entity_mention A,dl_predict B where A.doc_id='"+doc_id+"' and A.entity_id=B.entity_id order by B.dl_predict desc limit 1"
  223. cursor.execute(sql)
  224. dl_row = cursor.fetchall()[0]
  225. dl_entity = dl_row[0]
  226. dl_expectation = dl_row[1]
  227. sql = " select "+column+" from articles where id='"+doc_id+"' "
  228. cursor.execute(sql)
  229. re_match = cursor.fetchall()[0][0]
  230. sql = " select A.entity_text,B.dl_predict,B.entity_id from entity_mention A,dl_predict B where A.doc_id='"+doc_id+"' and A.entity_id=B.entity_id and A.entity_text='"+re_match+"' order by B.dl_predict desc limit 1"
  231. cursor.execute(sql)
  232. dl_row = cursor.fetchall()
  233. if len(dl_row)>0:
  234. re_expectation = dl_row[0][1]
  235. else:
  236. re_expectation = -1
  237. isSame_dd_re = ""
  238. if re_expectation>0.8:
  239. isSame_dd_re = "#B0"
  240. else:
  241. if dl_entity==re_match:
  242. isSame_dd_re = "#B0"
  243. else:
  244. if dl_expectation>0.8:
  245. isSame_dd_re = "#B1"
  246. f.write("<td>"+str(doc_id)+"</td>"+"<td>"+str(dl_entity)+"</td>"+"<td>"+str(dl_expectation)+"</td>"+"<td>"+str(re_match)+"</td>"+"<td>"+str(re_expectation)+"</td>"+"<td>"+str(isSame_dd_re)+"</td>")
  247. f.write("</tr>")
  248. f.write("\n")
  249. f.write('</tr>\
  250. </table>\
  251. </body>\
  252. </html>')
  253. f.flush()
  254. f.close()
  255. conn.close()
  256. def importReLabel():
  257. file = "relabel.txt"
  258. conn = psycopg2.connect(dbname="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101")
  259. cursor = conn.cursor()
  260. cursor.execute(" delete from relabel ")
  261. with codecs.open(file,"r",encoding="utf8") as f:
  262. while(True):
  263. line = f.readline()
  264. if not line:
  265. break
  266. line_split = line.split("\t")
  267. entity_id = line_split[0]
  268. label = line_split[1]
  269. sql = " insert into relabel(entity_id,label) values('"+str(entity_id)+"',"+str(label)+")"
  270. cursor.execute(sql)
  271. f.close()
  272. conn.commit()
  273. conn.close()
  274. if __name__=="__main__":
  275. importPredict()
  276. #importIterateLabel()
  277. #exportResult()
  278. #exportDDResult()
  279. #exportDLResult()
  280. #importReLabel()