Skip to content
Snippets Groups Projects
Commit 97cacab8 authored by Benedikt's avatar Benedikt
Browse files

added closestMatch suggestion

parent 17d3e100
No related branches found
No related tags found
No related merge requests found
Pipeline #31470 passed
from dataclasses import dataclass from dataclasses import dataclass
import re import re
import warnings import warnings
import difflib
class dsiParser: class dsiParser:
"""Parser to parse D-SI unit string into a tree """Parser to parse D-SI unit string into a tree
...@@ -256,7 +257,31 @@ def _warn(message: str, warningClass): ...@@ -256,7 +257,31 @@ def _warn(message: str, warningClass):
""" """
warnings.warn(message, warningClass) warnings.warn(message, warningClass)
return message return message
def _getClosestStrAndType(unkownStr):
"""returns the closest string and type of the given string
Args:
unkownStr (str): string to be compared
Returns:
str: closest string
str: type of closest string
"""
closestStr = difflib.get_close_matches(unkownStr, _dsiPrefixesLatex.keys(), n=1)
if len(closestStr) == 0:
closestStr = difflib.get_close_matches(unkownStr, _dsiUnitsLatex.keys(), n=1)
if len(closestStr) == 0:
closestStr = difflib.get_close_matches(unkownStr, _dsiKeyWords.keys(), n=1)
if len(closestStr) == 0:
return (unkownStr, 'unknown')
else:
return (closestStr[0], 'keyWord')
else:
return (closestStr[0], 'unit')
else:
return (closestStr[0], 'prefix')
# mapping D-SI prefixes to latex # mapping D-SI prefixes to latex
_dsiPrefixesLatex = { _dsiPrefixesLatex = {
'deca': r'\mathrm{da}', 'deca': r'\mathrm{da}',
...@@ -330,4 +355,7 @@ _dsiUnitsLatex = { ...@@ -330,4 +355,7 @@ _dsiUnitsLatex = {
'bel': r'\mathrm{B}', 'bel': r'\mathrm{B}',
'decibel': r'\mathrm{dB}' 'decibel': r'\mathrm{dB}'
} }
\ No newline at end of file _dsiKeyWords = {
'tothe': r'\tothe',
'per': r'\per'}
\ No newline at end of file
import pytest import pytest
from dsiParser import dsiParser, _node from dsiParser import dsiParser, _node, _getClosestStrAndType
p = dsiParser() p = dsiParser()
...@@ -100,3 +100,11 @@ def test_wrappers(): ...@@ -100,3 +100,11 @@ def test_wrappers():
assert parserWrapper.parse(r'\metre').toLatex() == r'&\mathrm{m}&' assert parserWrapper.parse(r'\metre').toLatex() == r'&\mathrm{m}&'
parserFull = dsiParser(latexDefaultWrapper='@', latexDefaultPrefix=r'\mathrm{Prefix}', latexDefaultSuffix=r'\mathrm{Suffix}') parserFull = dsiParser(latexDefaultWrapper='@', latexDefaultPrefix=r'\mathrm{Prefix}', latexDefaultSuffix=r'\mathrm{Suffix}')
assert parserFull.parse(r'\metre').toLatex() == r'@\mathrm{Prefix}\mathrm{m}\mathrm{Suffix}@' assert parserFull.parse(r'\metre').toLatex() == r'@\mathrm{Prefix}\mathrm{m}\mathrm{Suffix}@'
def test_getClosestMatch():
closestMatch = _getClosestStrAndType('\kiilo')
assert closestMatch == ('kilo','prefix')
closestMatch = _getClosestStrAndType('\mettre')
assert closestMatch == ('metre','unit')
closestMatch = _getClosestStrAndType(r'\ttothe')
assert closestMatch == ('tothe', 'keyWord')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment