getLabelDatas.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. '''
  2. Created on 2019年1月10日
  3. @author: User
  4. '''
  5. # DEPRECATED(Phase1): 本文件中的 PostgreSQL 硬编码连接(host=192.168.*,
  6. # password=postgres 等)将在后续 training/ Phase 迁移到 BiddingKG.dl.infra.db。
  7. # 迁移完成前可临时使用:from BiddingKG.dl.infra.db import get_connection;
  8. # conn = get_connection("<dbname>")
  9. # 详见 ARCHITECTURE.md 第 11 章 Phase 1 与 REFACTOR_LOG.md。
  10. import psycopg2
  11. import codecs
  12. import re
  13. import os
  14. import pandas as pd
  15. from BiddingKG.dl.common.Utils import *
  16. def getDatasToExcel():
  17. '''
  18. @summary: 将预标注的数据导出到excel中
  19. '''
  20. list_entity_id = []
  21. list_label = []
  22. list_before = []
  23. list_center = []
  24. list_after = []
  25. list_label_text = []
  26. conn = psycopg2.connect(dbname="BiddingKG",user="postgres",password="postgres",host="192.168.2.101")
  27. cursor = conn.cursor()
  28. sql = " select A.entity_id,A.label,A.entity_text,A.begin_index,A.end_index,B.tokens,case when A.label=1 then '招标联系人' when A.label=2 then '代理联系人' when A.label=3 then '联系人' else '无' end as link from predict_entity_copy A,predict_sentences_copy B where A.entity_type='person' and A.doc_id=B.doc_id and A.sentence_index=B.sentence_index order by A.label"
  29. cursor.execute(sql)
  30. rows = cursor.fetchall()
  31. for row in rows:
  32. tokens = row[5]
  33. begin_index = row[3]
  34. end_index = row[4]
  35. entity_text = row[2]
  36. label_text = row[6]
  37. list_entity_id.append(row[0])
  38. list_label.append(str(row[1]))
  39. beforeafter = spanWindow(tokens,begin_index,end_index,10)
  40. list_before.append(beforeafter[0])
  41. list_center.append(entity_text)
  42. list_after.append(beforeafter[1])
  43. list_label_text.append(label_text)
  44. columns = ["id","label","before","center","after","label_text"]
  45. nums = 3
  46. parts = len(list_entity_id)//nums
  47. print(parts)
  48. i = 0
  49. while(i<nums-1):
  50. pdframe = pd.DataFrame({"id":list_entity_id[i*parts:(i+1)*parts],"label":list_label[i*parts:(i+1)*parts],"before":list_before[i*parts:(i+1)*parts],"center":list_center[i*parts:(i+1)*parts],"after":list_after[i*parts:(i+1)*parts],"label_text":list_label_text[i*parts:(i+1)*parts]})
  51. pdframe.to_excel("person_"+str(i)+".xls",columns = columns)
  52. i += 1
  53. pdframe = pd.DataFrame({"id":list_entity_id[i*parts:],"label":list_label[i*parts:],"before":list_before[i*parts:],"center":list_center[i*parts:],"after":list_after[i*parts:],"label_text":list_label_text[i*parts:]})
  54. pdframe.to_excel("person_"+str(i)+".xls",columns = columns)
  55. def getDatasFromExcel():
  56. '''
  57. @summary: 将人工标注好的数据从excel中导入到数据库中
  58. '''
  59. conn = psycopg2.connect(dbname="BiddingKG",user="postgres",password="postgres",host="192.168.2.101")
  60. cursor = conn.cursor()
  61. home = "./label/"
  62. files = os.listdir(home)
  63. for file in files:
  64. data = pd.read_excel(home+file)
  65. list_entity_id = data['id']
  66. list_label = data['label']
  67. list_relabel = data['relabel']
  68. for i in range(len(list_entity_id)):
  69. if str(list_relabel[i])!="nan":
  70. label = str(int(list_relabel[i]))
  71. else:
  72. label = str(int(list_label[i]))
  73. entity_id = list_entity_id[i]
  74. sql = " insert into hand_label_person(entity_id,label) values('"+str(entity_id)+"',"+label+")"
  75. cursor.execute(sql)
  76. conn.commit()
  77. conn.close()
  78. if __name__=="__main__":
  79. #getDatasToExcel()
  80. getDatasFromExcel()