class TestParameter(TestCase): def setUp(self): self.malevalCmd = MaltevalXmlCommands() self.command = Document() self.evaluation = self.command.createElement("evaluation") self.command.appendChild(self.evaluation) self.param = Parameter('metric', self.command, self.evaluation) def _getValue(self, parameterName, valueName, doc): dom = parseString(doc) for node in dom.getElementsByTagName("parameter"): if node.getAttribute('name') == parameterName: lst = [vals.childNodes[0].data.rstrip().lstrip() for vals in node.getElementsByTagName("value")] if valueName in lst: return True return False def _getValueSelf(self, parameterName, valueName): return self._getValue(parameterName, valueName, self.malevalCmd.toPrettyXml()) def _hasParameter(self, parameterName, doc): dom = parseString(doc) for node in dom.getElementsByTagName("parameter"): if node.getAttribute('name') == parameterName: return True return False def _hasParameterSelf(self, parameterName): return self._hasParameter(parameterName, self.malevalCmd.toPrettyXml()) ###TESTS def testParameterDeleteNotAddedValue(self): self.assertRaises(AttributeError, self.param.deleteValue, NOT_EXISTING) def testAddValue(self): self.param.addValue(MaltevalMetric.LAS) self.assertTrue(MaltevalMetric.LAS in self.param.valuesDict) def testAddDeleteProperty(self): self.param.addValue(MaltevalMetric.LAS) self.param.deleteValue(MaltevalMetric.LAS) self.assertFalse(MaltevalMetric.LAS in self.param.valuesDict) def testAddNonExistingCommandParameter(self): self.assertRaises(AttributeError, self.malevalCmd.addValue, NOT_EXISTING, MaltevalMetric.LAS) def testAddNonExistingEverything(self): self.assertRaises(AttributeError, self.malevalCmd.addValue, NOT_EXISTING, NOT_EXISTING) def testAddCorrectParameterAndAnyValue(self): self.malevalCmd.addValue(MaltevalParameters.Metric, NOT_EXISTING) self.assertTrue(MaltevalParameters.Metric in self.malevalCmd.parameters) def testDoubleAdd(self): self.malevalCmd.addValue(MaltevalParameters.Metric, MaltevalMetric.LAS) self.malevalCmd.addValue(MaltevalParameters.Metric, MaltevalMetric.LAS) res = self.malevalCmd.toPrettyXml() splits = str(res).split(MaltevalParameters.Metric) self.assertEqual(len(splits), 2); def testRemove(self): self.malevalCmd.addValue(MaltevalParameters.Metric, MaltevalMetric.LAS) self.malevalCmd.deleteValue(MaltevalParameters.Metric, MaltevalMetric.LAS) self.assertFalse(self.malevalCmd.parameters is {}) def testAddMaxSentenceLength(self): self.malevalCmd.maxSentenceLength(10); self.assertTrue( self._getValueSelf(MaltevalParameters.MaxSentenceLength, "10")) def testDeleteMaxSentenceLength(self): self.malevalCmd.maxSentenceLength(10); self.malevalCmd.deleteMaxSentenceLength() self.assertFalse( self._hasParameterSelf(MaltevalParameters.MaxSentenceLength) ) def testAddMinusSentenceLength(self): self.assertRaises(AttributeError, self.malevalCmd.maxSentenceLength, -1) def testAddMinSentenceLength(self): self.malevalCmd.minSentenceLength(10) self.assertTrue( self._getValueSelf(MaltevalParameters.MinSentenceLength, "10")) def testDeleteMinSentenceLength(self): self.malevalCmd.minSentenceLength(10) self.malevalCmd.deleteMinSentenceLength() self.assertFalse( self._hasParameterSelf(MaltevalParameters.MinSentenceLength) ) def testAddMinusMinSentenceLength(self): self.assertRaises(AttributeError, self.malevalCmd.minSentenceLength, -1) def testAddGroupBy(self): self.malevalCmd.addGroupBy(MaltevalGroupBy.Lemma) self.assertTrue( self._getValueSelf(MaltevalParameters.GroupBy, MaltevalGroupBy.Lemma) ) def testAddMultiple(self): self.malevalCmd.addGroupBy(MaltevalGroupBy.Lemma) self.malevalCmd.addGroupBy(MaltevalGroupBy.ArcDepth) self.malevalCmd.addGroupBy(MaltevalGroupBy.ArcProjectivity) self.assertEqual(len(self.malevalCmd.parameters[MaltevalParameters.GroupBy].valuesDict), 3) def testAddMultipleLemma(self): self.malevalCmd.addGroupBy(MaltevalGroupBy.Lemma) self.malevalCmd.addGroupBy(MaltevalGroupBy.ArcDepth) self.malevalCmd.addGroupBy(MaltevalGroupBy.ArcProjectivity) self.assertTrue( self._getValueSelf(MaltevalParameters.GroupBy, MaltevalGroupBy.Lemma) ) def testAddMultipleArcDepth(self): self.malevalCmd.addGroupBy(MaltevalGroupBy.Lemma) self.malevalCmd.addGroupBy(MaltevalGroupBy.ArcDepth) self.malevalCmd.addGroupBy(MaltevalGroupBy.ArcProjectivity) self.assertTrue( self._getValueSelf(MaltevalParameters.GroupBy, MaltevalGroupBy.ArcDepth) ) def testAddMultipleArcArcProjectivity(self): self.malevalCmd.addGroupBy(MaltevalGroupBy.Lemma) self.malevalCmd.addGroupBy(MaltevalGroupBy.ArcDepth) self.malevalCmd.addGroupBy(MaltevalGroupBy.ArcProjectivity) self.assertTrue( self._getValueSelf(MaltevalParameters.GroupBy, MaltevalGroupBy.ArcProjectivity) ) def testDeleteGroupBy(self): self.malevalCmd.deleteGroupBy(MaltevalGroupBy.ArcDepth) self.assertFalse( self._hasParameterSelf(MaltevalParameters.GroupBy) ) def testDeleteAfterAditionGroupBy(self): self.malevalCmd.addGroupBy(MaltevalGroupBy.ArcDepth) self.malevalCmd.deleteGroupBy(MaltevalGroupBy.ArcDepth) self.assertFalse( self._hasParameterSelf(MaltevalParameters.GroupBy) ) def testAddMetricNonExisting(self): self.assertRaises(AttributeError, self.malevalCmd.addMetric, NOT_EXISTING) def testDeleteMetrics(self): self.malevalCmd.deleteMetric(NOT_EXISTING)
print "uas ", malteval.uas() print "las ", malteval.las() # let's do it more explicit way maltevalXmlCommands = MaltevalXmlCommands() maltevalXmlCommands.addMetric(MaltevalMetric.UAS) malteval.setCommand(maltevalXmlCommands) res = malteval.execute() print res print "Parsed :", malteval.tryParse(res) # let's do something more sophisticated maltevalXmlCommands = MaltevalXmlCommands() maltevalXmlCommands.addMetric(MaltevalMetric.UAS) maltevalXmlCommands.addMetric(MaltevalMetric.LAS) maltevalXmlCommands.addGroupBy(MaltevalGroupBy.Deprel) maltevalXmlCommands.addGroupBy(MaltevalGroupBy.Postag) malteval.setCommand(maltevalXmlCommands) res = malteval.execute() print res # let's see how to modify manually commands maltevalXmlCommands = MaltevalXmlCommands() maltevalXmlCommands.addMetric(MaltevalMetric.LAS) maltevalXmlCommands.addGroupBy(MaltevalGroupBy.Deprel) # sometimes you must add something manually maltevalXmlCommands.addValue(MaltevalParameters.ExcludePdeprels, "apobl|app") # this is example of so called complex group by which is # used rarely but can be specified maltevalXmlCommands.addGroupBy("Cpostag@-1") # see if it looks good