|
| 1 | +import re |
| 2 | + |
| 3 | +from flowmapper.flow import Flow |
| 4 | + |
| 5 | +ROMAN_NUMERAL_PATTERN = re.compile(r"\b\(?[ivx]+[\+-]?\)?\s*$", flags=re.IGNORECASE) |
| 6 | +PARENTHESES_PATTERN = re.compile(r"\([1-9]+[\+-]?\)\s*$") |
| 7 | + |
| 8 | + |
| 9 | +def has_roman_numeral_at_end(text: str) -> bool: |
| 10 | + """ |
| 11 | + Check if a string ends with a roman numeral. |
| 12 | +
|
| 13 | + Args: |
| 14 | + text (str): The string to check |
| 15 | +
|
| 16 | + Returns: |
| 17 | + bool: True if the string ends with a roman numeral, False otherwise |
| 18 | +
|
| 19 | + """ |
| 20 | + return bool(ROMAN_NUMERAL_PATTERN.search(text)) |
| 21 | + |
| 22 | + |
| 23 | +def has_number_pattern_at_end(text: str) -> bool: |
| 24 | + """ |
| 25 | + Check if a string ends with a pattern like "(2+)". |
| 26 | +
|
| 27 | + Args: |
| 28 | + text (str): The string to check |
| 29 | +
|
| 30 | + Returns: |
| 31 | + bool: True if the string ends with the number pattern, False otherwise |
| 32 | +
|
| 33 | + """ |
| 34 | + return bool(PARENTHESES_PATTERN.search(text)) |
| 35 | + |
| 36 | + |
| 37 | +def match_identical_names_in_preferred_synonyms( |
| 38 | + s: Flow, t: Flow, comment: str = "Identical preferred synonyms" |
| 39 | +): |
| 40 | + if t.synonyms and s.name in t.synonyms and s.context == t.context: |
| 41 | + if s.name.normalized in t.name.normalized and ( |
| 42 | + has_roman_numeral_at_end(t.name.normalized) |
| 43 | + or has_number_pattern_at_end(t.name.normalized) |
| 44 | + ): |
| 45 | + return {"comment": comment} |
| 46 | + elif s.synonyms and t.name in s.synonyms and s.context == t.context: |
| 47 | + if t.name.normalized in s.name.normalized and ( |
| 48 | + has_roman_numeral_at_end(s.name.normalized) |
| 49 | + or has_number_pattern_at_end(s.name.normalized) |
| 50 | + ): |
| 51 | + return {"comment": comment} |
| 52 | + |
| 53 | + |
| 54 | +def match_identical_names_in_synonyms( |
| 55 | + s: Flow, t: Flow, comment: str = "Identical synonyms" |
| 56 | +): |
| 57 | + if (t.synonyms and s.name in t.synonyms and s.context == t.context) or ( |
| 58 | + s.synonyms and t.name in s.synonyms and s.context == t.context |
| 59 | + ): |
| 60 | + return {"comment": comment} |
0 commit comments