Skip to content

Commit cf1607b

Browse files
committed
Support of adding dictionary words from Bibtex files
1 parent 17c7243 commit cf1607b

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

bibtex_loader.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import re
3+
import bibtexparser
4+
5+
def split_bibtex_name(name):
6+
"""
7+
Split a Bibtex name, which is two words seperated by a number.
8+
"""
9+
10+
# map any digit to space
11+
mytable = str.maketrans("0123456789", " ")
12+
new_name = name.translate(mytable)
13+
14+
# split by space
15+
words = new_name.split()
16+
return words
17+
18+
19+
def add_bibtex(enchant_dict, filename, verbose=False):
20+
"""Update ``enchant_dict`` spell checking dictionary with names
21+
from ``filename``, a Bibtex file."""
22+
23+
if verbose:
24+
print(f"Bibtex file: {filename}")
25+
26+
with open(filename, "rt", encoding="utf-8") as biblatex_file:
27+
bib_database = bibtexparser.load(biblatex_file)
28+
29+
for k in bib_database.get_entry_dict().keys():
30+
words = split_bibtex_name(k)
31+
for w in words:
32+
enchant_dict.add(w)
33+
if verbose:
34+
print("Added Bibtex word:", w)

comment_spell_check.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
import argparse
2626
import re
2727
from pathlib import Path
28+
from importlib.metadata import version, PackageNotFoundError
2829

2930
from enchant.checker import SpellChecker
3031
from enchant.tokenize import EmailFilter, URLFilter
3132
from enchant import Dict
3233

3334
from comment_parser import comment_parser
3435

35-
from importlib.metadata import version, PackageNotFoundError
36+
import bibtex_loader
3637

3738
__version__ = "unknown"
3839

@@ -378,6 +379,13 @@ def parse_args():
378379
help="Set file mime type. File name suffix will be ignored.",
379380
)
380381

382+
parser.add_argument(
383+
"--bibtex",
384+
action="append",
385+
dest="bibtex",
386+
help="Bibtex file to load for additional dictionary words.",
387+
)
388+
381389
parser.add_argument("--version", action="version", version=f"{__version__}")
382390

383391
args = parser.parse_args()
@@ -431,6 +439,11 @@ def main():
431439
for d in args.dict:
432440
add_dict(sitk_dict, d, any([args.brief, output_lvl >= 0]))
433441

442+
if args.bibtex is not None:
443+
for bib in args.bibtex:
444+
bibtex_loader.add_bibtex(sitk_dict, bib, any([args.brief, output_lvl >= 0]))
445+
446+
434447
spell_checker = SpellChecker(sitk_dict, filters=[EmailFilter, URLFilter])
435448

436449
file_list = []

tests/bibtest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
# lowekamp2013design
4+
# yaniv2018simpleitk
5+
# ibanez2003itk
6+
# avants2014insight
7+
# yushkevich2017itk
8+
9+
print("Hello World")

tests/itk.bib

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
@article{lowekamp2013design,
2+
title={The design of SimpleITK},
3+
author={Lowekamp, Bradley C and Chen, David T and Ib{\'a}{\~n}ez, Luis and Blezek, Daniel},
4+
journal={Frontiers in neuroinformatics},
5+
volume={7},
6+
pages={45},
7+
year={2013},
8+
publisher={Frontiers Media SA}
9+
}
10+
11+
@article{yaniv2018simpleitk,
12+
title={SimpleITK image-analysis notebooks: a collaborative environment for education and reproducible research},
13+
author={Yaniv, Ziv and Lowekamp, Bradley C and Johnson, Hans J and Beare, Richard},
14+
journal={Journal of digital imaging},
15+
volume={31},
16+
number={3},
17+
pages={290--303},
18+
year={2018},
19+
publisher={Springer}
20+
}
21+
22+
@misc{ibanez2003itk,
23+
title={The ITK software guide},
24+
author={Ibanez, Luis and Schroeder, Will and Ng, Lydia and Cates, Josh and others},
25+
year={2003},
26+
publisher={Kitware, Incorporated Clifton Park, New York}
27+
}
28+
29+
@article{avants2014insight,
30+
title={The Insight ToolKit image registration framework},
31+
author={Avants, Brian B and Tustison, Nicholas J and Stauffer, Michael and Song, Gang and Wu, Baohua and Gee, James C},
32+
journal={Frontiers in neuroinformatics},
33+
volume={8},
34+
pages={44},
35+
year={2014},
36+
publisher={Frontiers Media SA}
37+
}
38+
39+
@article{yushkevich2017itk,
40+
title={ITK-SNAP: an intractive medical image segmentation tool to meet the need for expert-guided segmentation of complex medical images},
41+
author={Yushkevich, Paul A and Gerig, Guido},
42+
journal={IEEE pulse},
43+
volume={8},
44+
number={4},
45+
pages={54--57},
46+
year={2017},
47+
publisher={IEEE}
48+
}

tests/test_comment_spell_check.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,17 @@ def test_version(self):
8181
self.assertNotEqual(
8282
version_string, "unknown", "version string contains 'unknown'"
8383
)
84+
85+
def test_bibtex(self):
86+
"""Bibtext test"""
87+
runresult = subprocess.run(
88+
[
89+
"python",
90+
"comment_spell_check.py",
91+
"--bibtex",
92+
"tests/itk.bib",
93+
"tests/bibtest.py",
94+
],
95+
stdout=subprocess.PIPE,
96+
)
97+
self.assertEqual(runresult.returncode, 0, runresult.stdout)

0 commit comments

Comments
 (0)