Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.90 KB | None | 0 0
  1. '''
  2. Created on 8/17/17
  3. Sample code for
  4. @author: Noushin
  5. '''
  6.  
  7.  
  8. def selectional_preferencer(indata, out_dict):
  9.     with open(indata, "r") as file:
  10.         lines = file.readlines()
  11.     lines = lines + ['\n']
  12.     verbs = set(["VB", "VBD", "VBG", "VBN", "VBP", "VBZ"])
  13.     subjects = set(["nsubj", "nsubjpass", "csubj", "csubjpass"])
  14.     sentence_index = 0
  15.     verb_dict_temp = {}
  16.     verb_obj = []
  17.     #main_dict = {}
  18.     main_list = []
  19.     cnt1 = 0
  20.     cnt2 = 0
  21.     cnt3 = 0
  22.     cnt4 = 0
  23.  
  24.     block_counter = 0
  25.  
  26.     for c, line in enumerate(lines):
  27.         line = line.strip()
  28.  
  29.         #if the line is empty, clear verb_obj and verb_dict_temp
  30.         if not line:
  31.             #if verb_obj:
  32.             #main_dict[sentence_index] = verb_obj
  33.             ##main_list.extend(verb_obj)
  34.             verb_dict_temp.clear()
  35.             ##verb_obj = []
  36.             sentence_index += 1
  37.             block_counter = c
  38.  
  39.  
  40.         else:
  41.  
  42.             linearr = line.split("\t")
  43.  
  44.             wordIndex = linearr[0]
  45.             token = linearr[1]
  46.             lemma = linearr[2]
  47.             POS = linearr[3]
  48.             NER = linearr[4]
  49.             head = linearr[5]
  50.             depRel = linearr[6]
  51.  
  52.             if POS in verbs:
  53.                 verb_dict_temp[int(wordIndex)] = lemma
  54.  
  55.             isObject = False
  56.             isSubject = False
  57.             isNER = False
  58.  
  59.             if depRel == "dobj":
  60.                 isObject = True
  61.  
  62.             if depRel in subjects:
  63.                 isSubject = True
  64.  
  65.             if NER != "O":
  66.                 isNER = True
  67.  
  68.  
  69.             if isObject or isSubject:
  70.                 if POS != "PRP":
  71.                     index_v = int(head)
  72.                     verb_of_obj = None
  73.  
  74.                     try:
  75.                         verb_of_obj = verb_dict_temp[index_v]
  76.  
  77.                     except KeyError:
  78.                         print("this key doesn't exist:", index_v)
  79.  
  80.                         print("looking for line:", index_v)
  81.                         bc = -1 if block_counter == 0 else block_counter
  82.                         nline = lines[bc + index_v]
  83.                         nlinearr = nline.split("\t")
  84.                         nwordIndex = nlinearr[0]
  85.                         nlemma = nlinearr[2]
  86.                         #print("new entry:", nwordIndex, nlemma)
  87.                         verb_dict_temp[int(nwordIndex)] = nlemma
  88.                         if nlinearr[3] in verbs:
  89.                             verb_of_obj = verb_dict_temp[index_v]
  90.  
  91.                     if verb_of_obj is not None:
  92.                         new_dict = {}
  93.                         if isNER:
  94.                             NER_type = NER
  95.  
  96.  
  97.                         if isSubject and isNER:
  98.                             previous_key = verb_of_obj + "- subject "
  99.                             print(type(previous_key))
  100.                             print("**************************************************************************")
  101.                             if 'previous_key' in new_dict:
  102.                                 cnt1 += 1
  103.                                 new_dict[previous_key].append(NER_type,cnt1)
  104.                             else:
  105.                                 new_dict[previous_key] = (NER_type,cnt1)
  106.                             #verb_obj.append(verb_of_obj + ": subject : " + NER_type)
  107.  
  108.                         if isSubject and not isNER:
  109.                             previous_key = verb_of_obj + "-subject "
  110.                             if previous_key in new_dict:
  111.                                 cnt2 += 1
  112.                                 new_dict[previous_key].append(token,cnt2)
  113.                             else:
  114.                                 new_dict[previous_key] = (token,cnt2)
  115.                             #verb_obj.append(verb_of_obj + ": subject : " + token)
  116.  
  117.                         if isObject and not isNER:
  118.                             previous_key = verb_of_obj + "- object "
  119.                             if previous_key in new_dict:
  120.                                 cnt3 += 1
  121.                                 new_dict[previous_key].append(token,cnt3)
  122.                             else:
  123.                                 new_dict[previous_key] = token
  124.  
  125.                         if isObject and isNER:
  126.                             previous_key = verb_of_obj + "- object "
  127.                             if previous_key in new_dict:
  128.                                 cnt4 += 1
  129.                                 new_dict[previous_key].append(NER_type,cnt4)
  130.                             else:
  131.                                 new_dict[previous_key] = (NER_type,cnt4)
  132.                         main_list.append(new_dict)
  133.     with open('{0}_output.txt'.format(out_dict), 'w') as output:
  134.         output.write(str(main_list))
  135.  
  136.         output.close()
  137.     return (main_list)
  138.  
  139.  
  140. #selectional_preferencer("simple_test.conll", "selectt")
  141. selectional_preferencer("dev-muc3-0001-0100.conll", "select_pe")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement