Package TEES :: Package ExampleWriters :: Module ModifierExampleWriter
[hide private]

Source Code for Module TEES.ExampleWriters.ModifierExampleWriter

  1  import sys 
  2  from SentenceExampleWriter import SentenceExampleWriter 
  3  import Utils.ElementTreeUtils as ETUtils 
  4  from Core.IdSet import IdSet 
  5  import types 
  6  import itertools 
  7       
8 -class ModifierExampleWriter(SentenceExampleWriter):
9 - def __init__(self):
10 self.xType = "task3"
11
12 - def writeXML(self, examples, predictions, corpus, outputFile, classSet=None, parse=None, tokenization=None, goldCorpus=None, exampleStyle=None):
13 """ 14 Writes task 3 examples to interaction XML. Assumes task 3 classification 15 is done with SVMMulticlass Classifier, used for two classes. 16 """ 17 print >> sys.stderr, "Adding task 3 to Interaction XML" 18 examples, predictions = self.loadExamples(examples, predictions) 19 20 if type(classSet) == types.StringType: # class names are in file 21 classSet = IdSet(filename=classSet) 22 classIds = None 23 if classSet != None: 24 classIds = classSet.getIds() 25 26 corpusTree = ETUtils.ETFromObj(corpus) 27 corpusRoot = corpusTree.getroot() 28 29 # Determine subtask 30 task3Type = None 31 for example in examples: 32 assert example[3].has_key("t3type") 33 task3Type = example[3]["t3type"] 34 break 35 if task3Type == None: 36 if outputFile != None: 37 print >> sys.stderr, "Writing corpus to", outputFile 38 ETUtils.write(corpusRoot, outputFile) 39 return corpusTree 40 assert task3Type in ["multiclass", "speculation", "negation"] 41 42 # Remove the task 3 subtask information if it already exists 43 for entity in corpusRoot.getiterator("entity"): 44 if task3Type == "multiclass": 45 entity.set("speculation", "False") 46 entity.set("negation", "False") 47 elif task3Type == "speculation": 48 entity.set("speculation", "False") 49 else: # task3Type == "negation" 50 entity.set("negation", "False") 51 52 specMap = {} 53 negMap = {} 54 for example, prediction in itertools.izip(examples, predictions): 55 assert example[3]["xtype"] == "task3" 56 if example[3]["t3type"] == "multiclass": 57 predictedClassName = classSet.getName(prediction[0]) 58 if predictedClassName != "neg": 59 predictedModifiers = predictedClassName.split("---") 60 if "negation" in predictedModifiers: 61 assert not negMap.has_key(example[3]["entity"]) 62 negMap[example[3]["entity"]] = (True, prediction) 63 if "speculation" in predictedModifiers: 64 assert not specMap.has_key(example[3]["entity"]) 65 specMap[example[3]["entity"]] = (True, prediction) 66 else: 67 if example[3]["t3type"] == "speculation": 68 map = specMap 69 else: 70 map = negMap 71 if prediction[0] != 1: 72 assert not map.has_key(example[3]["entity"]) 73 map[example[3]["entity"]] = (True, prediction) 74 else: 75 assert not map.has_key(example[3]["entity"]) 76 map[example[3]["entity"]] = (False, prediction) 77 78 for entity in corpusRoot.getiterator("entity"): 79 eId = entity.get("id") 80 if task3Type == "multiclass": 81 if specMap.has_key(eId): 82 entity.set("speculation", str(specMap[eId][0])) 83 entity.set("modPred", self.getPredictionStrengthString(specMap[eId][1], classSet, classIds)) 84 if negMap.has_key(eId): 85 entity.set("negation", str(negMap[eId][0])) 86 entity.set("modPred", self.getPredictionStrengthString(negMap[eId][1], classSet, classIds)) 87 else: 88 if task3Type == "speculation": 89 if specMap.has_key(eId): 90 entity.set("speculation", str(specMap[eId][0])) 91 entity.set("specPred", self.getPredictionStrengthString(specMap[eId][1], classSet, classIds, [""])) 92 elif task3Type == "negation": 93 if negMap.has_key(eId): 94 entity.set("negation", str(negMap[eId][0])) 95 entity.set("negPred", self.getPredictionStrengthString(negMap[eId][1], classSet, classIds, ["","speculation"])) 96 97 # Write corpus 98 if outputFile != None: 99 print >> sys.stderr, "Writing corpus to", outputFile 100 ETUtils.write(corpusRoot, outputFile) 101 return corpusTree
102