fix: improve unit recognition
This commit is contained in:
+6
-2
@@ -35,14 +35,16 @@ class Unit(SpanToken):
|
|||||||
prefixes = ["p", "n", "μ", "m", "k", "M", "G"]
|
prefixes = ["p", "n", "μ", "m", "k", "M", "G"]
|
||||||
|
|
||||||
pattern = re.compile(
|
pattern = re.compile(
|
||||||
r"(?P<value>[0-9,]+) ?(?P<prefix>"
|
# Check for word boundary at start of value ensures only numeric values are caught.
|
||||||
|
r"\b(?P<value>[0-9,]+) ?(?P<prefix>"
|
||||||
# Group all prefixes together in a capture group
|
# Group all prefixes together in a capture group
|
||||||
# Empty string at end of list is required so the group always exists in the resulting match
|
# Empty string at end of list is required so the group always exists in the resulting match
|
||||||
+ "|".join(prefixes + [""])
|
+ "|".join(prefixes + [""])
|
||||||
+ ")(?P<unit>"
|
+ ")(?P<unit>"
|
||||||
# Group all units together in a capture group.
|
# Group all units together in a capture group.
|
||||||
+ "|".join(units)
|
+ "|".join(units)
|
||||||
+ r")(?!\w)"
|
# Word boundary check at end of unit to ensure proper handling of units like A, Ah etc.
|
||||||
|
+ r")\b"
|
||||||
)
|
)
|
||||||
|
|
||||||
value: str
|
value: str
|
||||||
@@ -50,6 +52,8 @@ class Unit(SpanToken):
|
|||||||
unit: str
|
unit: str
|
||||||
|
|
||||||
def __init__(self, match: re.Match):
|
def __init__(self, match: re.Match):
|
||||||
|
print("pattern", self.pattern)
|
||||||
|
|
||||||
self.value = match.group("value")
|
self.value = match.group("value")
|
||||||
self.prefix = match.group("prefix") or ""
|
self.prefix = match.group("prefix") or ""
|
||||||
self.unit = match.group("unit")
|
self.unit = match.group("unit")
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ def test_basic_units_html():
|
|||||||
"0,75 kW": "0,75 kW",
|
"0,75 kW": "0,75 kW",
|
||||||
"33 A": "33 A",
|
"33 A": "33 A",
|
||||||
"1 Ah": "1 Ah", # Ah can be a tricky case if the RegEx is wrong, as A might match before Ah, thus rendering the output with an "A" unit
|
"1 Ah": "1 Ah", # Ah can be a tricky case if the RegEx is wrong, as A might match before Ah, thus rendering the output with an "A" unit
|
||||||
|
"10 A/mm²": "10 A/mm²",
|
||||||
|
"DL9MJ": "DL9MJ", # Ensure other strings, like callsings, aren‘t mistaken for units. ;)
|
||||||
|
"Ein Satz mit 200 V.": "Ein Satz mit 200 V.", # Support units before sentence ends.
|
||||||
}
|
}
|
||||||
|
|
||||||
for input, output in assertions.items():
|
for input, output in assertions.items():
|
||||||
|
|||||||
Reference in New Issue
Block a user