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"]
|
||||
|
||||
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
|
||||
# Empty string at end of list is required so the group always exists in the resulting match
|
||||
+ "|".join(prefixes + [""])
|
||||
+ ")(?P<unit>"
|
||||
# Group all units together in a capture group.
|
||||
+ "|".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
|
||||
@@ -50,6 +52,8 @@ class Unit(SpanToken):
|
||||
unit: str
|
||||
|
||||
def __init__(self, match: re.Match):
|
||||
print("pattern", self.pattern)
|
||||
|
||||
self.value = match.group("value")
|
||||
self.prefix = match.group("prefix") or ""
|
||||
self.unit = match.group("unit")
|
||||
|
||||
Reference in New Issue
Block a user