예제 #1
0
def double(token):
    """Convert a single token into multiple (two) parsers."""
    trait1 = Trait(start=token.start, end=token.end)
    token1 = Token(DOUBLE_CROSS, group=copy(token.group))
    token1.group["units"] = token.group.get("units_1")
    token1.group["value"] = token.group.get("value_1")
    side1 = token.group.get("side_1")

    trait2 = Trait(start=token.start, end=token.end)
    token2 = Token(DOUBLE_CROSS, group=copy(token.group))
    token2.group["units"] = token.group.get("units_2")
    token2.group["value"] = token.group.get("value_2")
    side2 = token.group.get("side_2")

    if token1.group["units"] and not token2.group["units"]:
        token2.group["units"] = token1.group["units"]
    elif token2.group["units"] and not token1.group["units"]:
        token1.group["units"] = token2.group["units"]

    flag1 = as_value(token1, trait1, value_field="value")
    flag2 = as_value(token2, trait2, value_field="value")
    if not flag1 or not flag2:
        return None

    side1 = side1.lower().strip(punctuation) if side1 else None
    side2 = side2.lower().strip(punctuation) if side2 else None
    side1 = SIDES.get(side2) if not side1 else side1
    side2 = SIDES.get(side1) if not side2 else side2

    if side1:
        trait1.side = side1
    if side2:
        trait2.side = side2

    return [trait1, trait2]
 def test_cross_04(self):
     actual = self.cross_parser.parse("12x34x56")
     expect = [
         Token(VOCAB["cross_product"],
               span=(0, 5),
               group={"number": ["12", "34"]})
     ]
     self.assertEqual(actual, expect)
 def test_cross_03(self):
     actual = self.cross_parser.parse("9 12.3 x 45.6 8")
     expect = [
         Token(VOCAB["cross_product"],
               span=(2, 13),
               group={"number": ["12.3", "45.6"]})
     ]
     self.assertEqual(actual, expect)
 def test_fraction_01(self):
     actual = self.fraction_parser.parse("12/34")
     expect = [
         Token(
             VOCAB["fraction_product"],
             span=(0, 5),
             group={
                 "number": ["12", "34"],
                 "numerator": "12",
                 "denominator": "34"
             },
         )
     ]
     self.assertEqual(actual, expect)
 def test_cross_07(self):
     actual = self.cross_parser.parse("12.3mm")
     expect = [
         Token(
             VOCAB["cross_product"],
             span=(0, 6),
             group={
                 "number": "12.3",
                 "metric_len": "mm",
                 "len_units": "mm"
             },
         )
     ]
     self.assertEqual(actual, expect)
 def test_cross_06(self):
     actual = self.cross_parser.parse("3mmx1.5mm")
     expect = [
         Token(
             VOCAB["cross_product"],
             span=(0, 9),
             group={
                 "number": ["3", "1.5"],
                 "metric_len": ["mm", "mm"],
                 "len_units": ["mm", "mm"],
             },
         )
     ]
     self.assertEqual(actual, expect)
 def test_uuid_01(self):
     parser = Parser(VOCAB["uuid"])
     actual = parser.parse("ddf2d94a-0a49-11ea-a133-000000000004")
     expect = [Token(VOCAB["uuid"], span=(0, 36), group={})]
     self.assertEqual(actual, expect)