Advertisement
Pella86

Noush - conll parser

Aug 18th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.40 KB | None | 0 0
  1. '''
  2. this is just a suggestion on how it should work
  3. '''
  4.  
  5.  
  6. def selectional_preferencer(indata, out_dict):
  7.     with open(indata, "r") as file:
  8.         lines = file.readlines()
  9.        
  10.    
  11.  
  12.     verbs = set(["VB", "VBD", "VBG", "VBN", "VBP", "VBZ"])
  13.     subjects = set(["nsubj", "nsubjpass", "csubj", "csubjpass"])
  14.     sentence_index = 1
  15.     verb_dict_temp = {}
  16.     verb_obj = []
  17.     main_dict = {}
  18.    
  19.     block_counter = 0
  20.    
  21.     for c, line in enumerate(lines):
  22.         line = line.strip()
  23.        
  24.         # here you say, if the line is empty, then clear verb_obj and verb_dict_temp
  25.         if not line:
  26.             if verb_obj:
  27.                 main_dict[sentence_index] = verb_obj
  28.             verb_dict_temp.clear()
  29.             verb_obj = []
  30.             sentence_index += 1
  31.             block_counter = c
  32.  
  33.            
  34.         else:
  35.            
  36.             linearr = line.split("\t")
  37.             print(linearr)
  38.            
  39.             wordIndex = linearr[0]
  40.             token = linearr[1]
  41.             lemma = linearr[2]
  42.             POS = linearr[3]
  43.             NER = linearr[4]
  44.             head = linearr[5]
  45.             depRel = linearr[6]
  46.            
  47.            
  48.             if POS in verbs:
  49.                 verb_dict_temp[int(wordIndex)] = lemma
  50.            
  51.             isObject = False
  52.             isSubject = False
  53.             isNERO = False
  54.            
  55.             if depRel == "dobj":
  56.                 isObject = True
  57.            
  58.             if depRel in subjects:
  59.                 isSubject = True
  60.            
  61.             if NER == "=":
  62.                 isNERO = True
  63.                
  64.            
  65.             print(isObject, isSubject)    
  66.             if isObject or isSubject:
  67.                 print(POS)
  68.                 if POS != "PRP":
  69.                     index_v = int(head)
  70.                     verb_of_obj = None
  71.                    
  72.                     try:
  73.                         verb_of_obj = verb_dict_temp[index_v]
  74.                        
  75.                     except KeyError:   # Never leave a naked expression, except KeyError: is better
  76.                         print("this key doesn't exist:", index_v)
  77.                        
  78.                         print("looking for line:", index_v)
  79.                         bc = -1 if block_counter == 0 else block_counter
  80.                         nline = lines[bc + index_v]
  81.                         nlinearr = nline.split("\t")
  82.                         nwordIndex = nlinearr[0]
  83.                         nlemma = nlinearr[2]
  84.                         print("new entry:", nwordIndex, nlemma)
  85.                         verb_dict_temp[int(nwordIndex)] = nlemma
  86.                        
  87.                         verb_of_obj = verb_dict_temp[index_v]
  88.                        
  89.                    
  90.                     print(verb_of_obj)
  91.                     if verb_of_obj is not None:
  92.                         if isObject:
  93.                             verb_obj.append(verb_of_obj + ": object : " + token)
  94.                        
  95.                         if isSubject or isNERO:
  96.                             NER_type = NER
  97.                             verb_obj.append(verb_of_obj + ":" + NER_type)
  98.                
  99.    
  100.     with open('{0}_output.txt'.format(out_dict), 'w') as output:
  101.         output.write(str(main_dict))
  102.         output.close()
  103.     return (main_dict)
  104.  
  105.  
  106. selectional_preferencer("d1dHm8uS.txt", "select")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement