Exemplo n.º 1
0
def typed(token):
    """Convert single value tokens into a result."""
    trait = Trait(start=token.start, end=token.end)
    trait.notation = token.group["notation"]
    trait.value = to_positive_int(token.group["value1"])
    trait.value += to_positive_int(token.group.get("value2"))
    return trait
Exemplo n.º 2
0
def fraction(token):
    """Handle fractional values like 10 3/8 inches."""
    trait = Trait(start=token.start, end=token.end)
    trait.units = token.group.get("units")
    trait.units_inferred = not bool(trait.units)
    whole = to_positive_float(token.group.get("whole", "0"))
    numerator = to_positive_int(token.group["numerator"])
    denominator = to_positive_int(token.group["denominator"])
    try:
        trait.value = whole + Fraction(numerator, denominator)
    except TypeError:
        print(f"Fraction error: {numerator} / {denominator}")
        return None
    if trait.units:
        trait.value = convert_units(trait.value, trait.units)
    add_flags(token, trait)
    return trait
def convert_count(token):
    """Convert parsed tokens into a result."""
    trait = Trait(start=token.start, end=token.end)

    trait.value = to_positive_int(token.group.get("value"))
    count1 = to_positive_int(token.group.get("count1"))
    count2 = to_positive_int(token.group.get("count2"))
    side1 = SUB.get(token.group.get("side1", " ").lower()[0], "side1")
    side2 = SUB.get(token.group.get("side2", " ").lower()[0], "side2")

    if not trait.value:
        trait.value = count1 + count2

    if count1 or side1 != "side1":
        setattr(trait, side1, count1)

    if count2 or side2 != "side2":
        setattr(trait, side2, count2)

    return trait if all(x < 1000 for x in as_list(trait.value)) else None
Exemplo n.º 4
0
def each_side(token):
    """Count the found embryo."""
    trait = Trait(start=token.start, end=token.end)

    if token.group.get("subcount"):
        count = to_positive_int(token.group["subcount"])
        trait.value = count + count
        trait.left = count
        trait.right = count

    return trait
Exemplo n.º 5
0
def shorthand_length(token, measurement=""):
    """Handle shorthand length notation like 11-22-33-44:55g."""
    trait = Trait(start=token.start, end=token.end)
    trait.value = to_positive_float(token.group.get(measurement))
    if not trait.value:
        return None
    trait.units = "mm_shorthand"
    trait.units_inferred = False
    trait.is_shorthand = True
    flag = measurement.split("_")[1]
    flag = f"estimated_{flag}"
    trait.is_flag_in_token(token, flag, rename="estimated_value")
    return trait
Exemplo n.º 6
0
def convert(token):
    """Convert parsed tokens into a result."""
    trait = Trait(start=token.start, end=token.end)

    if token.group.get("total"):
        trait.value = to_positive_int(token.group["total"])

    if token.group.get("subcount"):
        trait.value = sum(
            to_positive_int(c) for c in as_list(token.group["subcount"]))

    if token.group.get("subcount") and token.group.get("sub"):
        for count, sub in zip(as_list(token.group["subcount"]),
                              as_list(token.group.get("sub"))):
            count = "1" if count == "!" else count
            sub = SUB.get(sub[0].lower(), sub)
            setattr(trait, sub, to_positive_int(count))

    elif token.group.get("side"):
        side = token.group["side"].lower()
        trait.side = SUB.get(side, side)

    return trait if all(x < 1000 for x in as_list(trait.value)) else None
Exemplo n.º 7
0
def compound(token):
    """Handle a pattern like: 4 ft 9 in."""
    trait = Trait(start=token.start, end=token.end)
    trait.units = [token.group["feet"], token.group["inches"]]
    trait.units_inferred = False
    trait.is_flag_missing(token, "key", rename="ambiguous_key")
    fts = convert_units(to_positive_float(token.group["ft"]), "ft")
    ins = [
        convert_units(to_positive_float(i), "in")
        for i in as_list(token.group["in"])
    ]
    value = [round(fts + i, 2) for i in ins]
    trait.value = squash(value)
    add_flags(token, trait)
    return trait
Exemplo n.º 8
0
def compound(token):
    """Convert a compound weight like: 2 lbs. 3.1 - 4.5 oz."""
    trait = Trait(start=token.start, end=token.end)
    trait.units = [token.group["pounds"], token.group["ounces"]]
    trait.units_inferred = False
    trait.is_flag_missing(token, "key", rename="ambiguous_key")
    lbs = convert_units(to_positive_float(token.group["lbs"]), "lbs")
    ozs = [
        convert_units(to_positive_float(oz), "ozs")
        for oz in as_list(token.group["ozs"])
    ]
    value = [round(lbs + oz, 2) for oz in ozs]
    trait.value = squash(value)
    add_flags(token, trait)
    return trait
Exemplo n.º 9
0
def convert(token):
    """Convert single value tokens into a result."""
    value = token.group.get("value")

    if not value:
        return None

    trait = Trait(start=token.start, end=token.end)
    trait.value = to_positive_int(value)

    if trait.value > 100:
        return None

    if token.group.get("notation"):
        trait.notation = token.group["notation"]

    return trait
def convert_count(token):
    """Convert parsed tokens into a result."""
    trait = Trait(start=token.start, end=token.end)

    value = to_positive_int(token.group.get("value"))
    count1 = to_positive_int(token.group.get("count1"))
    count2 = to_positive_int(token.group.get("count2"))

    if not value:
        value = count1 + count2

    if value >= 1000:
        return None

    trait.value = "present" if value > 0 else "absent"

    return trait
Exemplo n.º 11
0
def convert_many(token):
    """Convert several values."""
    values = token.group["value"]
    units = as_list(token.group.get("len_units", []))

    traits = []
    for i, value in enumerate(values):
        trait = Trait(start=token.start, end=token.end)
        if i < len(units):
            trait.units = units[i]
            trait.units_inferred = False
        else:
            trait.units = units[-1] if units else None
            trait.units_inferred = True
        trait.value = convert_units.convert_units(to_positive_float(value),
                                                  trait.units)
        if trait.value > TOO_BIG:
            continue
        add_flags(token, trait)
        traits.append(trait)
    return traits