Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
python_DSI_Parser
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Benedikt
python_DSI_Parser
Commits
97cacab8
Commit
97cacab8
authored
1 year ago
by
Benedikt
Browse files
Options
Downloads
Patches
Plain Diff
added closestMatch suggestion
parent
17d3e100
No related branches found
No related tags found
No related merge requests found
Pipeline
#31470
passed
1 year ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/dsiParser.py
+30
-2
30 additions, 2 deletions
src/dsiParser.py
tests/test_main.py
+9
-1
9 additions, 1 deletion
tests/test_main.py
with
39 additions
and
3 deletions
src/dsiParser.py
+
30
−
2
View file @
97cacab8
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
This diff is collapsed.
Click to expand it.
tests/test_main.py
+
9
−
1
View file @
97cacab8
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
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment