# Automated spell-checking in Django projects

Source: https://djangotv.com/videos/djangocon-europe/2018/djangocon-2018-automated-spell-checking-in-django-projects/
Date: 2018-05-24
Speakers: Speaker 1, Jakob Schnell, Speaker 3, Speaker 4

## Summary

Jakob Schnell explains how to add automated spell-checking to Django projects. Sphinx documentation can be checked with sphinxcontrib-spelling and integrated into CI, while application text is harder to find in scattered Python, HTML, and CSS files; Django’s gettext translation workflow collects these strings into PO files. His PoTypo tool combines the Babel/PO translation format with PyEnchant to check source and translated strings, with configuration for languages, custom word lists, ignored languages, phrases, edge-case words, URLs, HTML, and Python format strings. He presents it as a young, still-evolving project and notes that it does not solve spell-checking arbitrary untranslated strings or identifiers.

## Transcript

### [00:00] Introduction and Motivation

**Speaker 1:** [00:06] Our next speaker will be Jacob Schnell and he will be talking about automated spell checking.

**Jakob Schnell:** [00:38] All right.

**Speaker 1:** [00:39] Okay, please welcome Jakob.

**Jakob Schnell:** [00:47] Perfect. Hello everybody, my name is Jakob. Some of you might know me by my nickname Kubi. I'm a maths and computer science student currently on an Erasmus year in Milan in Italy, but originally studying at the University of Heidelberg. This is my first talk at any DjangoCon or PyCon or whatsoever. So I thought as an introduction it might be fair to Just have a few words about why I am here. And it all starts back in October last year. I was sitting in a database class I think and compared to Katie's talk from earlier it was boring at least to me and so I decided to do something worthy of my time

[01:33] And a friend of me, Rafael, that has just introduced me, has a project called PreTix that you might know that you all bought your ticket from. And so I decided to start hacking on pretex. And I fired up the program and after a few seconds I discovered a typos somewhere in the code and I was like wow great cool I can just search for this wrong word and I can fix it and I can submit a PR and it will be great and I will be doing something useful and I will be doing good stuff with my time. And so I did that and that worked fairly well. And then I figured maybe there are more typos than just this one. And So I fired up a spell checker, I think a spell. And spoiler alert, there were more typos, and so I fixed them all, or all of them I could find.

[02:23] And I asked whether maybe Automated spell checking would be something that PreTix would like or that the Django world could use. And Rafael's answer was yes of course, please do so. And now you might be wondering, well, but I am typing my text and I am typing good and perfectly and here are some examples of wrong words. That that that were fixed, that are to be found in or that were found in the pretext project. And I think since all the text that we write is written by us, by humans, we can all agree on the fact that it will contain errors And a friend of mine even goes as far as saying whenever you publish anything, be it a book or a thesis or an article or whatsoever, once it is published, once you handed it in, you can open your thesis, your book at whatever page and the first thing you will find.

[03:19] is a typo and this will never change. Oh yeah, the audience agrees with me. Cool. And for most of these typos it's not too bad because most of us are just so used to reading all the time that they will just skip those typos. But if you're not an English native speaker or if you have trouble with the language altogether, then you might stumble across some of these typos and every now and then There might be typos that wouldn't even be well let's say code of conduct compliant. For example I encountered a typo in the word account where the second C and the O were switched. And that's why it's not on the slide. And yes, so obviously

### [04:05] Documentation Spell-Checking

**Jakob Schnell:** [04:05] we need spell checking because we ca we can't find those errors all by ourselves. Some of them will slip past us and Computers are just better at doing so. In general, there are two places where we need spell checking in a Django project one first is the documentation and the second is the code itself or the user interface that we provide And the code is the much more complicated part, and I'll come to that in about five minutes, I think. So I first want to talk about documentation. For documentation, spell checking is rather easy. That is because a All of your documentation is usually in one place.

[04:52] Right? You have one or one folder called docs and it contains all of your all of your documentation. It's usually large text files that are easily checkable. Usually you only provide documentation in one language, so you only need one spell checker for that language. And In the Python world documentation is sorry usually done with Sphinx, with a Sphinx project, package, whatever. And for Sphinx, fortunately spell checking already does exist. And it's a package called SphinxContript. spelling And the usage is it was written by Dac Alman, the usage is quite simple. You fire up strings minus build with a directive. Oh nice, I can look at here, not there.

[05:37] You fire it up via things minus build minus b spelling, then you put all your other options that you need, and then you specify in what folder your output should be and usually that will be something like underscore build slash spelling. The requirements are fairly easy. You can just install SphinxControl-spelling via pip and it needs PyEnchant, which I will talk about in a few minutes. Those are readily available. You can just add those two lines to your requirements. txt file or whatever. If you are using Travis, you can easily implement the Sphinx build thingy by just adding the enchant package that is needed for Pi enchant

[06:23] in your Travis setup and you can generally easily integrate it into your CI by just checking whether after the build is done the file output. txt in the directory underscore build slash spelling does exist And if it does, then you have some errors and you should go fix them. And if it does not, then everything is fine and you can just move on. So this is this is fairly straightforward. This is nice. It works just fine. It has been working for PreTix for I don't know let's say four or five months now and it's great it needs a bit of configuration of course. First of all your Sphinx config in conf. py needs to have sphinx config. spelling as an extension

[07:08] Then you should specify a spelling language, which I guess will be En underscore US for most of our applications. Since your application will probably contain some words that are valid for your application but not for the English language in general. For example in the Pretex project, this is pretexts as a word itself. It will obviously come up in the documentation, but it is not a word to be found in an English dictionary, I think. You need a so-called word list that just lists all these words that are not found anywhere in a dictionary but in your documentation and that are correctly spelled. And you can just specify a word list via the spelling underscore word underscore list

### [07:54] Spell-Checking Application Text

**Jakob Schnell:** [07:54] underscore file name directive. And if you want, you can ask the sphinxcontript. spelling to show suggestions for things that you misspelled, which makes it even easier for you to fix all those spellings. And this is easy and nice and works great. For code, everything gets a little bit more complicated. This is because in general wherever you interact the second part where you interact with your users apart from documentation or manuals or handbooks or whatsoever is your user interface either Usually in some kind of a graphical user interface or text user interface.

[08:40] And actually most GUIs contain a lot of text. And if you want to fix all the errors that are there, then you would have to parse Python code, you would have to parse HTML code, you'd have to pars CSS probably and extract all the strings and then it's unclear what strings should be translated and what ching strings shouldn't be translated. And to be fair I have no clue on how you would do this. But there is one thing that makes it very easy for us as a developer to indeed check all those strings and that thing is translation. Once your project scales beyond a certain point, you probably want to translate it in another language. For example, for myself, I am German, so English is not my mother tongue

[09:29] So I would probably develop my application in English since it should be accessible to more than just the German and German-speaking population. But I would probably want to have a German translation because it is easier for me and for my probably for my use cases to work with the product. It is easier for me to sell it to other people and so I want that translation. Even if you are an English native speaker then you might still want translation because you want to sell your product to other people. You might want to translate it into Spanish, which is a which is a language that many people speak. And once we have translation, it makes it quite easy for us. To automate our spell checking. Because translation gathers all those strings and there are there

### [10:15] PoTypo Overview

**Jakob Schnell:** [10:15] is a system that does this for us that I will present in a minute. And since we now have all of our strings gathered in one place, we can check them for spelling errors rather easily. And basically what the project I want to present to you today is, is it combines the PO library for translation and PyNchant for spell checking into one program called Po typo. And of course it's not just those two components, but it's also a lot of sweat and tears and work and glue code wrapped around stuff. To bring this all together. So let's talk about the two components that go together. The first is GetText. GetText is a localization and internationalization

### [11:02] Gettext and PO Files

**Jakob Schnell:** [11:02] system That is, I think, the standard for translation, for internationalization, for localization of things that handle text. And it is based on so-called. po files. This is an example of such a file It has some metadata in the top like the creation date, the date of the last revision, whoever was the last translator, and most importantly what language we are translating into in the language tag That is written up there and then some more metadata and the top. And then it has a bunch of entries that all basically look like the last three lines here. The first is where that where does this text

[11:47] appear in my project, for example in this sample PO file. po file, the first message would appear in the file junglecon slash talks slash intro. pi dot pie in line one. And every entry has a message ID that is the string that is there in your code, which would be hello DjangoCon, I am so happy to be here. I truly am. And the second would be the message string, which is the translation of that code into the language that is given above And the usage of this system in general is fairly simple as well. You can just import pget text and ugText from Django. utils. translation, not T9N, but otherwise it wouldn't fit.

[12:33] And you would for example have the two statements print underscore bra open brackets my name is name closing brackets And the underscore is then the call to you get text, and you would have, for example, a call like print pget text sorbox sizes 20. And if you now let getText do its thing on the files and gather all those message IDs for you, then this would render as something like the following. po file you see we have the file jjangocon slash talks slash poetypo. p dot py. And in the third line we have the my name is name in curly brackets message ID and the comment that we gave before this, which is leave name as is the code will handle it, gets automatically drawn into the.

[13:21] po file it adds that this is the Python brace format, which is a thing that tells getText that this is special or the thing in curly brackets is special. It shouldn't handle this. And you can add a message string or a translation for this. In German this would be Ich heiße name. And what the pgetText thing does is the first string is a context for your application. So usually a translator wouldn't would n I don't I don't think you would be able to handle the word venti, but once you add the context, oh it's about Starbucks sizes, then of course the translation or the meaning is large and it translates to growth in German. So this is fairly easy or if fairly simple to handle.

### [14:06] Spell-Checking Engines and PyEnchant

**Jakob Schnell:** [14:06] We can use the PO library to extract the message IDs and the message strings and then we can check them separately for errors. And since we have a base language given we know what our message ID ID's language is and since we have a language tag in our. In both of these languages quite easily. The second part that goes into PoTypo is a pie enchant. And if you're working with spell checking then you will probably have encountered some of those words. And I will try to give a short overview of on what they are. And it all starts with iSpel, which is a spell checker that is really old.

[14:52] It was written in 1971, if I'm not mistaken. Originally for the English language it works quite well. Even today it is sort of the de facto standard, but along came UTF-8 and with that well ice bell wasn't really able to handle that, so They so a spell was created. And to this day I think ice spell is the best spell checker for the English language When the OpenOffice Office project came along, they wrote they implemented their own spell checker, MySpell, as part of their Abby Word processor. And that has worked that has replaced ASPL as a spell checker in OpenOffice. And later Hunt Spell was developed originally for the Hungarian language.

[15:39] And it has by now replaced my spell as a spell checker. And from my point of view, A spell is the spell checker that you want if you spell check English text, and a hand spell is the spell checker that you want to use. whenever you check other European languages and for non European languages I'm very sorry but I have no idea. I don't encounter them frequently. In general this the those this mass of spell checkers produces kind of a problem because we would have to implement them all or we would have to handle them all and they all work. Sort of the same because they all take text and then they tell you what is wrong in the text, but they also all work sort of differently. And that is why

[16:25] the enchant project exists. The lib enchant is a library that wraps all of those spell checkers and provides you as a developer an API. With which you can easily check all the things and it handles all the specifications of the spell checkers for you and even if they don't implement some functionality but others do enchant will try to emulate the functionality that other spellcheckers have for you and it is very convenient as developer to just have an one framework that you need to know and you don't need to Interface with a spell or an hand spell or Finnish spell checkers or H spell for the Hebrew language or whatnot.

[17:10] There is a list on Wikipedia. Go look it up. It's huge And there exists a Py enchant written by Ryan Kelly, which is currently unfortunately unmaintained, which wraps the Libanchant and gives you Python bindings. So it's quite nice to interface with py enchant. Alright, these are the two main components that go into Po typo. And I will now talk about how you would use PoTypo in your project and how you would set this up, but first I will have a short drink with some water somewhere here

### [18:07] PoTypo Setup and Configuration

**Jakob Schnell:** [18:07] Mais Alright. The usage of Po typo is fairly simple. You just fire up a Po typo in your directory where your Setup. cfg lives. I will talk about this in a second. The requirements are fairly simple. Po typo itself is installable via pip install PoTypo and You of course need Py and Chant and maybe the PO library as a as requirements for Po typo, but if you use translation you have the pot the PO library installed anyways You will then have to

[18:52] have packages for every language that you want to check your spelling in. So for a German and English project, you would install something like a spell minus en for English dictionaries for a spell, and you would install something like my spell minus d minus d e. four dictionaries for the German language or actually you would install Hanspel but I haven't checked whether Hanspel is as good as my spell it should be but I don't know So again it is fairly easy, it is fairly easy to use. And the configuration is compliant with the setup So you have one part where you configure Po typo and your setup. cfg where you also configure Flake 8

[19:37] and other Django and Python thingies And you first of course specify a default language, which is the language that everything will be translated from. So it's basically the language of your message IDs in your. po file Then you specify where your local where your. po files live. Usually they will live in a locals directory like Django-project slash locale or somewhere. And Po typo handles the finding of all those dot Po files by itself. Currently it just assumes that you follow the structure that is given in the left that I will explain in a second,

[20:25] but it may add some magic to find your. po files automatically. Then of course you can specify languages for which you do not want PoTypo to fail or to report any errors. For example, if you are just in the process of translating your application into Danish Then you might of course want PoTypo to report what errors there are in the translation into Danish, but you do not want these errors to break your continuous integration process or to break your Travis build. And this is why the no-fail directive exists. And then of course, as before, we need a word list for words that are present in our application that are not present in an English dictionary.

[21:11] And you have basically two ways of handling these, of implementing this. The first is you can put them all in a wordless directory. If you do so, you should specify the WL underscore dir variable and you should give the directory that your word lists live in. And if you have that, then they should be named language tag. txt, which is the example on the right here You would have on in your Django project you would have a folder word list and then for every language you would have a file d e. txt en. txt and so on and so forth If you don't like this, then you can also put them in various places in the locales d directory And usually in your locals directory you have one directory for every language that you're translating into, one directory for Danish, one

[22:00] directory for German, and so on and so forth. And in those directories you have directory lc underscore messages and in that directory live your. po files. And you can just put your wordlist. txt, in this case called wordlist. txt, in this LC messages folder or in the folder above, in the folder for German or for Danish or for whatsoever. And for your base language, for your default language, you would just put a file called wordlist. txt into your into your locale folder. Those are the two ways that wordless are currently handled. If you have any other ideas or if you think wow this is Not as nice as I wanted, I want this another way.

### [22:47] Word Lists and Edge Cases

**Jakob Schnell:** [22:47] Please come talk to me and we can figure something out. But for now this works quite well Now there are a few exceptions to simply using word lists or to just spell checking. And the first are what I call edge case words. And these are words that contain punctuation or that contain a mix of numbers and letters. This is because of the way that a pie enchant works It basically splits your text by a white space and then strips punctuation from your text. And so the word add-ons would be split into a part adds and a part ons. And well add is an English word, onz

[23:34] is not, or not really, and it would report an error for that. Or for example for the For translate. pretex. eu it would report an error because it would split it into translate pretext and eu And it probably won't be able to handle those. And so all those words that contain punctuation, all the words that contain a mix of numbers and letters, like for example two hundred and fourteenth, which is a part of an address I think, you can specify them. And then it will just skip those words across all of your text. And you would not want to add them to your word list because again they are not the parts the single parts are not really words MT940 is not really an English word so you would not want it into your in your English word list

[24:25] But it's correct, it should be spelled that way, and so we just tell ProTypo please skip this word. And the next thing that is Somewhat different is what I called phrases. And a phrase is something that is present in your text, although it doesn't technically belong. For example, ticketing powered by is a text that might be used in a German application. And German people probably would understand it. Because a ticket in English is a ticket in German, so ticketing works and powered by well by and the German word by are quite similar and also powered would be understandable. So you can You can use the phrase ticketing powered by in a German in a German text.

[25:11] But neither the word ticketing nor the word powered nor the word by are German words so you'd have to you would have to put them in your word list if you do not want them to pop up as errors, which is bad because by might be a misspelling of the German word bi, which is written B-E-I. Because you are typing English and German parallel. And so you want this to be an error, but you do not want the phrase ticketing powered by itself to be an error so this is also handled by phrases and those are The two the two main things that you have to filter out when you are checking for your code. If you encounter any other things where neither HK's words nor phrases are enough for you to

[25:58] spell check your application in the way that you want to. I'm very happy to see more edge cases and to find a way to work around them. And then last but not least you might have some HTML code in your in your code somewhere that will be chunked out by an HTML chunker, which is provided by the enchant project. And then you have some filters that filter the Python braced format from before or that filter URLs or that filter HTML from your code and those are fairly easy to use you might write your own it's it's quite nice. And this is the complete feature set of PoTypo to this point. The project is

### [26:44] Project Status and Resources

**Jakob Schnell:** [26:44] Is not that old. It's quite new. It's still working project working progress. But it is already used in one application which is pre-tix once Raphael is done organizing this conference and gets around merging pull requests which I totally cannot blame him for. If you have any wishes on features that you think you might use if you want to use this project, you are very welcome to open issues and we can discuss about it. If you think, oh wow, this is something that I can use then please do so. Please come to me, please talk to me. I will help very gladly help you set all of this up. As I said, this is still work in progress. There might be things that change. This is

[27:31] my contact information, also where you can find the slides. and the GitHub repository. And before I finish, I want to thank three entities. First and foremost, I want to thank Rafael for his many times that he has provided help for me whilst going through the process of writing a Python project and publishing it and so on and so forth. He has been super helpful and is in general an awesome person. Second, I would like to thank Matthias Vogelgesang for the Metropolis beamer theme that I have used to create this presentation. And third, I want to thank you. As my audience for your time and for your attention. If you have any feedback, please go to the pre -talk system, click on this talk to the schedule, click on this talk, and give me some feedback.

[28:17] And you have questions, please go. Thank you.

### [28:33] Questions

**Speaker 3:** [28:33] Hello. I have one question regarding the documentation. Do you have actual ex example of what kind of documentation we can validate and run the library to the spell to run the spell checking

**Jakob Schnell:** [28:52] I don't know if I got your question right, but any kind of documentation basically that is that is using Sphinx to document its stuff, you can use the SphinxContript. spelling project to spell check your documentation. Does this answer your question?

**Speaker 3:** [29:10] Yeah, kind of. Okay. And another one is you showed actually how to spell check the strings representation that we show to the user. Can we use this on the sp

**Jakob Schnell:** [29:27] As I said, if you do not have a translation, so if you're All of the strings that should be spell checked are scattered throughout your project in Python files and HTML files and CSS files. Then I don't know on how to how to spell check those. If you want to use to translate your project into any other languages, then you would probably use the get text system and in that case you have a.po file and you can use PoTypo to spell check. But in the other case, unfortunately I have no idea on how to do this

**Speaker 3:** [29:57] Okay, thank you.

**Jakob Schnell:** [29:58] Welcome.

**Speaker 1:** [29:59] Next question from the microphone on the back.

**Speaker 4:** [30:09] Frustrations for my reviewers. I think I will have plenty of need for that. But one of the things I'm a bit unclear is doc strings. So if you use it for automated documentation of your code. Clearly all your class names, your function names, all this stuff is not spelled according to regular syntax rules. Would I have to maintain all these things in the edge case list or is there a different procedure to handle Doc strings and function names and class names and the weird spelling.

**Jakob Schnell:** [30:42] Well class names and function names are spelled correctly by default I would say. Because you really can't cannot I don't know spell them wrong.

**Speaker 4:** [30:56] But for example one of the projects I had, I had the misfortune to name the controller with one L, which is quite annoying. And Of course this was something that was not just a mistake I made when naming the class, but all in all the documentation that I wrote referring to this very class. So obviously once spotted it's easy to rectify, but It would so how would I how would I approach this particular problem?

**Jakob Schnell:** [31:20] Okay. The ThingsContra. spelling project does not check it only checks for errors in the pros that you write to document your process and not in function names or anything else. If it in c if you use the word controller for example with only one L in pros to refer to a controller then it will of course tell you this is wrong, but it doesn't check function names or I don't know methods or whatever.

**Speaker 4:** [31:47] Thank you.

**Jakob Schnell:** [31:47] You're welcome.

**Speaker 1:** [31:49] Wonderful. If we don't have any further questions, thank you very much, Jakob, for the talk.
