Python 7 5/8/2017


SUBMITTED BY: canonical

DATE: May 8, 2017, 6 p.m.

FORMAT: Python

SIZE: 3.2 kB

HITS: 405

  1. If you want more of my pastes visit: https://randompaste.000webhostapp.com/index.html
  2. --------------------------------------------------------------------------------------
  3. view my last post at: https://bitbin.it/SGh8LiEJ/
  4. --------------------------------------------------------------------------------------
  5. import random
  6. import requests, json
  7. import _pickle as pickle
  8. import googlemaps
  9. from datetime import datetime
  10. gmaps = googlemaps.Client(key='AIzaSyCZs17oOJSf7i6GyL19gV5sjV8TNL9RguQ')
  11. vec = []
  12. detected_locations = {}
  13. NERs = {}
  14. recognized = {}
  15. outputFormat = 'json'
  16. def apply_developed_rules(x, ln):
  17. i = 0
  18. try:
  19. while i < len(x["sentences"][0]["tokens"]):
  20. if x["sentences"][0]["tokens"][i]["ner"] == "LOCATION" and x["sentences"][0]["tokens"][i-1]["ner"] != "LOCATION":
  21. j = i + 1
  22. loc = x["sentences"][0]["tokens"][i]["word"]
  23. while x["sentences"][0]["tokens"][j]["ner"] == "LOCATION" or x["sentences"][0]["tokens"][j]["pos"] == "NNP":
  24. loc += " " + x["sentences"][0]["tokens"][j]["word"]
  25. j += 1
  26. i = j
  27. try:
  28. NERs[ln].append(loc)
  29. except:
  30. NERs[ln] = []
  31. NERs[ln].append(loc)
  32. #print(ln, loc)
  33. try:
  34. detected_locations[loc] += 1
  35. except KeyError:
  36. detected_locations[loc] = 1
  37. #out_file.write(loc+"\n");
  38. else:
  39. i += 1
  40. except KeyError:
  41. pass
  42. def apply_collection(x,ln):
  43. i = 0
  44. try:
  45. while i < len(x["sentences"][0]["tokens"]):
  46. if x["sentences"][0]["tokens"][i]["ner"] == "LOCATION":
  47. loc = x["sentences"][0]["tokens"][i]["word"]
  48. #print (loc)
  49. try:
  50. NERs[ln].append(loc)
  51. except:
  52. NERs[ln] = []
  53. NERs[ln].append(loc)
  54. try:
  55. detected_locations[loc] += 1
  56. except KeyError:
  57. detected_locations[loc] = 1
  58. #out_file.write(loc+"\n");
  59. i += 1
  60. except KeyError:
  61. pass
  62. def main():
  63. to_annotate = "final_files/hand_annotated_dataset.txt"
  64. hand_annotated = "final_files/hand_annotated_dataset.tsv"
  65. use_rules = True
  66. with open("final_files/entity_database.dat", "rb") as myFile:
  67. recognized = pickle.load(myFile)
  68. with open("final_files/testset.txt", encoding="utf8") as in_file:
  69. for l in in_file:
  70. vec.append(l)
  71. with open(to_annotate, 'r', encoding="utf8") as x_file:
  72. for line in x_file:
  73. ln = line
  74. total = ""
  75. try:
  76. total = recognized[ln]
  77. #print("\n\n\n")
  78. except:
  79. pass
  80. try:
  81. x = json.loads(total)
  82. if use_rules:
  83. apply_developed_rules(x,ln)
  84. else:
  85. apply_collection(x,ln)
  86. except:
  87. pass
  88. #print(detected_locations)
  89. #for i in NERs:
  90. # print(i)
  91. #print((i.replace("\n","") + "\t" + str(NERs[i]).replace("[","").replace("]","").replace(', ',"\t")).split('\t'))
  92. num_correct = 0
  93. with open(hand_annotated, 'r', encoding="utf8") as x_file:
  94. for l in x_file:
  95. v = l.replace('\n','').split('\t')
  96. a = []
  97. for i in range(1, len(v)):
  98. a.append(v[i].replace('\'',''))
  99. try:
  100. print(NERs[v[0]+'\n'], a, NERs[v[0]+'\n'] == a)
  101. if NERs[v[0]+'\n'] == a:
  102. num_correct += 1
  103. except:
  104. pass
  105. print (len(NERs), num_correct, num_correct/len(NERs)*100)
  106. if __name__ == '__main__':
  107. main()

comments powered by Disqus