Advertisement
Guest User

Untitled

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