import_predict.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #coding:utf8
  2. import codecs
  3. import psycopg2
  4. def importPredict():
  5. file = "predict.txt"
  6. conn = psycopg2.connect(dbname="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101")
  7. cursor = conn.cursor()
  8. cursor.execute(" delete from dl_predict ")
  9. with codecs.open(file,"r",encoding="utf8") as f:
  10. while(True):
  11. line = f.readline()
  12. if not line:
  13. break
  14. line_split = line.split("\t")
  15. entity_id=line_split[0]
  16. id = line_split[1]
  17. expectation = line_split[2]
  18. dl_predict = line_split[4]
  19. sql = " insert into dl_predict(entity_id,id,expectation,dl_predict) values('"+str(entity_id)+"',"+str(id)+","+str(expectation)+","+str(dl_predict)+")"
  20. cursor.execute(sql)
  21. f.close()
  22. conn.commit()
  23. conn.close()
  24. def exportResult():
  25. exporttable = " is_thirdtenderer "
  26. column = " third_tenderer "
  27. conn = psycopg2.connect(dbname="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101")
  28. cursor = conn.cursor()
  29. 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"
  30. cursor.execute(sql)
  31. rows = cursor.fetchall()
  32. with codecs.open("testCompare"+exporttable+".html","w",encoding="utf8") as f:
  33. f.write('<html><head>\
  34. <meta http-equiv="Content-Type"\
  35. content="text/html; charset=UTF-8">\
  36. </head>\
  37. <body bgcolor="#FFFFFF">\
  38. <table border="1">\
  39. <tr>\
  40. <td>id</td>\
  41. <td>sumvalue</td>\
  42. <td>sum期望</td>\
  43. <td>ddvalue</td>\
  44. <td>dd期望</td>\
  45. <td>dlvalue<</td>\
  46. <td>dl期望</td>\
  47. <td>标注第一候选</td>\
  48. <td>相比</td>\
  49. </tr>')
  50. for row in rows:
  51. f.write("<tr>")
  52. doc_id = row[0]
  53. 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"
  54. cursor.execute(sql)
  55. dd_row = cursor.fetchall()[0]
  56. dd_entity = dd_row[0]
  57. dd_expectation = dd_row[1]
  58. 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"
  59. cursor.execute(sql)
  60. dl_row = cursor.fetchall()[0]
  61. dl_entity = dl_row[0]
  62. dl_expectation = dl_row[1]
  63. 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"
  64. cursor.execute(sql)
  65. row = cursor.fetchall()[0]
  66. sum_entity = row[0]
  67. sum_expectation = row[1]
  68. sql = " select "+column+" from articles where id='"+doc_id+"' "
  69. cursor.execute(sql)
  70. re_match = cursor.fetchall()[0][0]
  71. if sum_expectation>0.5:
  72. if sum_entity==re_match:
  73. isSame_sum = "#A0"
  74. else:
  75. isSame_sum = "#A1"
  76. else:
  77. if sum_entity==re_match:
  78. isSame_sum = "#A2"
  79. else:
  80. isSame_sum = "#A3"
  81. if dd_expectation>0.5:
  82. if dd_entity==re_match:
  83. isSame_dd_re = "#B0"
  84. else:
  85. isSame_dd_re = "#B1"
  86. else:
  87. if dd_entity==re_match:
  88. isSame_dd_re = "#B2"
  89. else:
  90. isSame_dd_re = "#B3"
  91. if dl_expectation>0.5:
  92. if dl_entity==re_match:
  93. isSame_dl_re = "#C0"
  94. else:
  95. isSame_dl_re = "#C1"
  96. else:
  97. if dl_entity==re_match:
  98. isSame_dl_re = "#C2"
  99. else:
  100. isSame_dl_re = "#C3"
  101. 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>")
  102. f.write("</tr>")
  103. f.write("\n")
  104. f.write('</tr>\
  105. </table>\
  106. </body>\
  107. </html>')
  108. f.flush()
  109. f.close()
  110. conn.close()
  111. if __name__=="__main__":
  112. importPredict()
  113. #exportResult()