def parse_fraction(self, data): """ finds and confidence scores a fraction :param data: data we're testing :type data: str :return: results if this proper type :rtype: ParseResult """ is_fraction, value = self.is_fraction(data) if is_fraction: fraction_confidence = 100 # for every character in the data other than the /, we lower # the confidence a bit. Fractions are USUALLY short for char in data: if char != "/": fraction_confidence -= 3 # if the fraction isn't solve-able, we lower the confidence # significantly it might "technically" be a fraction made up # of roman numerals, etc. if EquationParser in self.config.enabled_modules: eqp = EquationParser(self.config) if not eqp.solve_equation(eqp.auto_float(data)): fraction_confidence -= 40 return self.result("Fraction", fraction_confidence, value)
def parse_fraction(self, data): """finds and confidence scores a fraction""" is_fraction, value = self.is_fraction(data) if is_fraction: fraction_confidence = 100 # for every character in the data other than the /, we lower # the confidence a bit. Fractions are USUALLY short for char in data: if char != "/": fraction_confidence -= 3 # if the fraction isn't solve-able, we lower the confidence # significantly it might "technically" be a fraction made up # of roman numerals, etc. if EquationParser in self.config.enabled_modules: eqp = EquationParser(self.config) if not eqp.solve_equation(eqp.auto_float(data)): fraction_confidence -= 40 return self.result("Fraction", fraction_confidence, value)
def setUp(self): self.ep = EquationParser(TestConfig)
class EquationParserTests(unittest.TestCase): """Unit Testing of the EquationParser""" ep = None def setUp(self): self.ep = EquationParser(TestConfig) def tearDown(self): registry.flush() self.ep = None def test_is_simple_equation(self): self.assertTrue(self.ep.is_simple_equation("5 * 5")) self.assertTrue(self.ep.is_simple_equation("5(2**5")) self.assertTrue(self.ep.is_simple_equation("5/4")) self.assertFalse(self.ep.is_simple_equation("5 x 5")) self.assertFalse(self.ep.is_simple_equation("3 DIVIDED BY 7")) self.assertFalse(self.ep.is_simple_equation("3 TIMES 7")) def test_is_text_equation(self): self.assertTrue(self.ep.is_text_equation("3 TIMES 7")) self.assertTrue(self.ep.is_text_equation("3 DIVIDED BY 7")) self.assertTrue(self.ep.is_text_equation("3 DIVIDEDBY 7")) self.assertTrue(self.ep.is_text_equation("3 PLUS 7")) self.assertTrue(self.ep.is_text_equation("3 MINUS 7")) self.assertTrue(self.ep.is_text_equation("3 SQUARED")) self.assertTrue(self.ep.is_text_equation("3 CUBED")) self.assertTrue(self.ep.is_text_equation("SQUARE ROOT OF 3")) self.assertFalse(self.ep.is_text_equation("3 quadrided 7")) self.assertFalse(self.ep.is_text_equation("yo momma")) self.assertFalse(self.ep.is_text_equation("5 X 7")) def test_auto_float(self): self.assertEqual(self.ep.auto_float("123.123"), "float(123.123)") self.assertEqual(self.ep.auto_float("123"), "float(123)") def test_auto_multiply(self): self.assertEqual(self.ep.auto_multiply("float(123.123)float(123.123)"), "float(123.123)*float(123.123)") self.assertEqual(self.ep.auto_multiply("float(123.123)(float(47))"), "float(123.123)*(float(47))") def test_check_for_safe_equation_string(self): self.assertTrue( self.ep.check_for_safe_equation_string( " math.sqrt float ( ) * + - / . 1 2 3 4 5 6 7 8 9 0 ")) self.assertFalse( self.ep.check_for_safe_equation_string( " foo math.sqrt float ( ) * + - / . 1 2 3 4 5 6 7 8 9 0 ")) def test_solve_equation(self): self.assertEqual(self.ep.solve_equation("float(5) * float(5)"), 25.0) self.assertFalse(self.ep.solve_equation("asdf * float(5)")) self.assertFalse(self.ep.solve_equation("math.sqrt * float(5)")) def test_calculate_confidence(self): self.assertEqual(self.ep.calculate_confidence("979-549-5150"), 80) self.assertEqual(self.ep.calculate_confidence("1-979-549-5150"), 70) self.assertEqual( self.ep.calculate_confidence("the square root of 1234"), 100) self.assertEqual( self.ep.calculate_confidence("Rain in spain is plain."), 100) def test_parseSimpleNumberYieldsNothing(self): count = 0 for _ in self.ep.parse('1234'): count += 1 self.assertEqual(count, 0) def test_parseEssentiallyEmptyStringYieldsNothing(self): count = 0 for _ in self.ep.parse('THE'): count += 1 self.assertEqual(count, 0) def test_parseSimpleEquationParseResult(self): count = 0 for result in self.ep.parse('5 * 5'): count += 1 self.assertEqual(result.subtype, 'Simple') self.assertEqual(result.result_value, 25) self.assertEqual(result.confidence, 100) self.assertEqual(count, 1) def test_parseTextEquationParseResult(self): count = 0 for result in self.ep.parse('3 TIMES 5'): count += 1 self.assertEqual(result.subtype, 'Text') self.assertEqual(result.result_value, 15) self.assertEqual(result.confidence, 100) self.assertEqual(count, 1) def test_parseNonParseableValueYieldsNothing(self): count = 0 for _ in self.ep.parse('This is not a text equation'): count += 1 self.assertEqual(count, 0)
class EquationParserTests(unittest.TestCase): """Unit Testing of the EquationParser""" ep = None def setUp(self): self.ep = EquationParser(TestConfig) def tearDown(self): registry.flush() self.ep = None def test_is_simple_equation(self): self.assertTrue(self.ep.is_simple_equation("5 * 5")) self.assertTrue(self.ep.is_simple_equation("5(2**5")) self.assertTrue(self.ep.is_simple_equation("5/4")) self.assertFalse(self.ep.is_simple_equation("5 x 5")) self.assertFalse(self.ep.is_simple_equation("3 DIVIDED BY 7")) self.assertFalse(self.ep.is_simple_equation("3 TIMES 7")) def test_is_text_equation(self): self.assertTrue(self.ep.is_text_equation("3 TIMES 7")) self.assertTrue(self.ep.is_text_equation("3 DIVIDED BY 7")) self.assertTrue(self.ep.is_text_equation("3 DIVIDEDBY 7")) self.assertTrue(self.ep.is_text_equation("3 PLUS 7")) self.assertTrue(self.ep.is_text_equation("3 MINUS 7")) self.assertTrue(self.ep.is_text_equation("3 SQUARED")) self.assertTrue(self.ep.is_text_equation("3 CUBED")) self.assertTrue(self.ep.is_text_equation("SQUARE ROOT OF 3")) self.assertFalse(self.ep.is_text_equation("3 quadrided 7")) self.assertFalse(self.ep.is_text_equation("yo momma")) self.assertFalse(self.ep.is_text_equation("5 X 7")) def test_auto_float(self): self.assertEqual(self.ep.auto_float("123.123"), "float(123.123)") self.assertEqual(self.ep.auto_float("123"), "float(123)") def test_auto_multiply(self): self.assertEqual( self.ep.auto_multiply("float(123.123)float(123.123)"), "float(123.123)*float(123.123)" ) self.assertEqual( self.ep.auto_multiply("float(123.123)(float(47))"), "float(123.123)*(float(47))" ) def test_check_for_safe_equation_string(self): self.assertTrue(self.ep.check_for_safe_equation_string( " math.sqrt float ( ) * + - / . 1 2 3 4 5 6 7 8 9 0 " )) self.assertFalse(self.ep.check_for_safe_equation_string( " foo math.sqrt float ( ) * + - / . 1 2 3 4 5 6 7 8 9 0 " )) def test_solve_equation(self): self.assertEqual(self.ep.solve_equation("float(5) * float(5)"), 25.0) self.assertFalse(self.ep.solve_equation("asdf * float(5)")) self.assertFalse(self.ep.solve_equation("math.sqrt * float(5)")) def test_calculate_confidence(self): self.assertEqual(self.ep.calculate_confidence("979-549-5150"), 80) self.assertEqual(self.ep.calculate_confidence("1-979-549-5150"), 70) self.assertEqual( self.ep.calculate_confidence("the square root of 1234"), 100 ) self.assertEqual( self.ep.calculate_confidence("Rain in spain is plain."), 100 ) def test_parseSimpleNumberYieldsNothing(self): count = 0 for _ in self.ep.parse('1234'): count += 1 self.assertEqual(count, 0) def test_parseEssentiallyEmptyStringYieldsNothing(self): count = 0 for _ in self.ep.parse('THE'): count += 1 self.assertEqual(count, 0) def test_parseSimpleEquationParseResult(self): count = 0 for result in self.ep.parse('5 * 5'): count += 1 self.assertEqual(result.subtype, 'Simple') self.assertEqual(result.result_value, 25) self.assertEqual(result.confidence, 100) self.assertEqual(count, 1) def test_parseTextEquationParseResult(self): count = 0 for result in self.ep.parse('3 TIMES 5'): count += 1 self.assertEqual(result.subtype, 'Text') self.assertEqual(result.result_value, 15) self.assertEqual(result.confidence, 100) self.assertEqual(count, 1) def test_parseNonParseableValueYieldsNothing(self): count = 0 for _ in self.ep.parse('This is not a text equation'): count += 1 self.assertEqual(count, 0)