By Using an INI Table of Legal Terms, We Construct Instant Hotstrings with the AutoHotkey Input Command
I recently received questions about using AutoHotkey for professional terminology in medical transcription. While features in many word processors such as Microsoft Word allow the building of special auto-correct dictionaries, they only work within Word. Since AutoHotkey works anywhere on a Windows computer, it’s only natural to want your own specialized technical lexicon. However, for the newbie, the question turns into “Where do I start?”
In the past number of blogs, I’ve introduced data-driven AutoHotkey scripts: “Write Less Code with Database Driven Apps (AutoHotkey Script Design)” and “Building a Lookup Table with an INI File (AutoHotkey Reference Tip).” The fact that the script does not need to embed all the details within the code creates a huge advantage when writing new apps. All the particulars remain in the data table.
AutoHotkey does not include built-in support for complex relational databases but it does support simple tables with a limited search capability via INI file commands. In the next few blogs, I plan to outline how to create a useful AutoHotkey data table for use with special purpose applications. I’ll start with a basic message box (MsgBox) which displays the information, next, insert technical terms into text editing fields with temporary Hotstring acronyms, then create a pop-up menu for inserting terms into documents, and, finally, generate a GUI window which can do it all—all from the same data table.
I while back I extracted a number (over 300) of common legal phrases from Wikipedia (List of Latin legal terms). At the time, I intended to write a blog about why lawyers should use AutoHotkey. With the recent inquiries about Medical jargon, I decided to use those legal phrases as a basis for demonstrating how you can create your own useful career tools with specialized data.
Why Use a Data Table?
Originally, I placed the legal terms I extracted from Wikipedia in a series of Hotstrings for replacing the English translation with the Latin equivalents. That made a pretty long script and I had to make any changes in the script. While it worked well, it wasn’t easy to add new features.
By placing the data in a readily accessible file, I plan to demonstrate how to design any number of individual apps (using various tools) from the same data table. By depositing all of the data in a file, I can now eliminate an untold number of lines of code while making the data adaptable for other applications.
Create a Data Table with an INI File
Technically, in the world of database management, we are not creating a database but, rather, a table which includes any number of records. Each record (usually one line in a file) contains fields of related data. The information in every field in that record (or row) connects in some manner to each corresponding field.
For example, I have three pieces of information about each legal Latin term: the English translation, the Latin phrase, and the “real” definition (e.g. written law, lex scripta, and “Law that specifically codifies something, as opposed to common law or customary law,” respectively). That means I need at least three fields in my database (one for each piece of data). In addition, I plan to add Hotstring acronyms to cue each data record which adds a fourth data field.
While not intended to act as a data table in the manner that I use it, the AutoHotkey tools for INI files work well for these types of applications—in particular, the ability to search on a key term. The format only allows for one search key in an INI file which in my table setup corresponds with the first field on the left side of the equal sign (the Hotstring acronym):
Hotstring acronym=English translation|Latin phrase|Definition
Field 1 ⇒ Hotstring acronym =
Field 2 ⇒ English translation |
Field 3 ⇒ Latin phrase |
Field 4 ⇒ Definition
Notice that I partitioned the fields on the right side of the equal sign with the vertical line character ( | ). Using a non-conventional symbol as a field separator (delimiter) makes it more efficient to pull out data when writing scripts.
You can add any number of additional fields by inserting more partitions by including more vertical lines but all data fields must appear in same order—although they do not need to all contain the same number of fields. To indicate a blank data item in a particular field inside the record, the delimiting mark must appear twice (e.g. ||).
Adding Data to the INI Table
You can edit an INI file with any text editor. That means you can manually add data with cut-and-paste techniques. I used a Regular Expression and Ryan’s RegEx Tester to cull the initial data from the Wikipedia page but a studious use of code deletion and text editor search-and-replace techniques with the Web page source code can get you there. Place the INI Section name at the top of the file (e.g. [InputKey], as shown in the image below). The image offers a peek at the LegalInput.ini file open in Notepad which contains over 300 records highlighting Latin legal phrases.
To place the acronym data in the first search key field, I wrote another Regular Expression which extracted four related letters from the English translation, then inserted the four-letter key at the beginning of each record followed by the equal sign. You could also open the INI file in any text editor and insert your own acronyms one at a time—followed by an equal sign. I deliberately used four letters as acronym keys since I need a certain level of consistency when writing the scripts which extract the data.
Note: The RegEx added a number of duplicates to the file so I must review the entries and select different unique (hopefully easy-to-remember) letter combinations. (In a future blog, I’ll take a look at using the Sort command to identify duplicates.)
In the first application, I plan to type the four-letter code to call up the related data from the file. Ultimately, people using this type of script will want to use a series of letters (acronyms) which make sense to them. The over 300 records in this INI table represents a substantial number of codes for memorization. The more sense the acronyms make to the user, the more useful the script.
You might find amassing your data table your most difficult task. Although time-consuming, you can build the INI file by hand. I used a Regular Expression to get started, but if you’re not comfortable with RegEx techniques, then you will need to improvise. In the past, I’ve used a combination of exporting data from other programs and targeted search-and-replace techniques to get my data table started. After that, I usually edit the INI table directly.
Tip: Even copying a key section directly from the Web page can work. For example, highlighting all of the key legal terms in the “List of Latin Legal Terms” Wikipedia page, then using copy-and-paste to insert the text into Windows Notepad can offer a good start. You can then replace all the tab characters (copy the blank space between the terms and use the Notepad search-and-replace to change each to the | delimiter).
Note: If you’re not familiar with writing Regular Expressions which can extract data from various sources, they can appear mysterious and confusing. It requires studying the source of the data to identifying the text cues needed to cull the target data. I generally use Ryan’s RegEx Tester to form my RegEx code. For more information about Regular Expressions, see my book Beginner’s Guide to Using Regular Expressions in AutoHotkey.
To make it easy, you may find it preferable to slowly build your data table by starting with only those terms you most need, then adding more as you encounter new requirements or opportunities. (You can even write a short AutoHotkey script for directly adding new records to your table.) This longer-term approach not only makes the process less tedious but simplifies learning your acronyms.
Remember, the search key must appear on the left side of the equal sign and the remaining fields on the right side must be separated by a delimiter (e.g. |).
In the next few blogs, I look at various methods for extracting data from the table, reviewing the data, and inserting data into documents.
Accessing the Data with AutoHotkey
Hotstrings work best for inserting replacement words and phrases into documents. You’ll find it much easier to remember a four-letter Hotstring acronym than various Hotkey combinations. You can select Hotstrings which directly relate to the target phrases. However, you can’t set up Hotstrings in an AutoHotkey script on-the-fly. AutoHotkey processes all Hotstring setups before running any commands. That means we can’t use a data file to setup standard Hotstrings.
Note: We could write a separate script which generates a traditional Hotstring file for inclusion (e.g. #Include HotstringFile.ahk) at runtime, but it must process the data separately from the main script. Plus, after each change to the data file, you must rerun the Hotstring file writing script, then reload the main script.
In this situation, the AutoHotkey Input command offers a much better alternative to standard Hotstrings for a couple of reasons:
- Since you must set up traditional Hotstrings in advance of running any operating code, you can’t create data-driven Hotstrings within the same script. You can with the Input command.
- Even when working with hundreds of temporary Hotstrings set up with the Input command, they won’t interfere with any other running traditional Hotstrings—even if identical. Plus, you can even use an acronym which corresponds to a real word without activating any unwanted Hotstring replacement.
- Characters typed during an active Input session do not embed in any document—unless you want them to appear.
This initial script only uses about 20 lines of code. Compare that with hundreds of lines of code it would require to set up Hotstrings from our data table of legal terms.
How the Input Command Works
When you activate the Input command, AutoHotkey stops—waiting for you to enter characters from your keyboard. The script continues marking time until you enter a specified string found in a list, hit an end character or the command times out. Ideally, you enter an acceptable acronym and AutoHotkey looks up the correct replacement for insertion into the document.
In this first example, AutoHotkey generates the list of acceptable Hotstring values from the acronym keys found in the data table—placing them in a single variable (Input_String). That list variable appears as a parameter in the Input command.
After launching the Input command, if AutoHotkey finds the typed characters in the acronym variable, it looks up the code in the LegalInput.ini file and displays a MsgBox containing the associated information (as shown in the image above).We do this by accessing the data table file twice: first; to create the list of acronyms for the Input command, and, second, to retrieve the results for any valid acronym. The first step which creates the acronym list occurs in the auto-execute portion of the script:
Loop, Read, LegalInput.ini { If A_Index = 1 ; Skip the Section name line Continue Input_Array := StrSplit(A_LoopReadLine, "=") Input_String := Input_String . Input_Array[1] . "," }
The Loop, Read command accesses the INI file one line at a time. This allows us to parse the acronym from the remainder of the record by using the equal sign (=) as a delimiter. The variable Input_String builds the list of Hotstring acronyms for inclusion in the Input command:
Input , OutputVar, L4 T10, , %Input_String%
The variable OutputVar contains the typed keyboard characters. the L4 option sets the max string length to four characters. The T10 option gives the user ten seconds to enter a code before it times out.
If the typed string matches an entry in Input_String, AutoHotkey sets ErrorLevel to Match. If so, we use the IniRead command to locate the appropriate record:
If (ErrorLevel = "Match") { IniRead, LegalTerm, LegalInput.ini, InputKey, %OutputVar% Legal_Array := StrSplit(LegalTerm, "|") MsgBox, , Hotstring %OutputVar%, % Legal_Array[1] . "`r`r" . Legal_Array[2] . "`r`r" . Legal_Array[3] }
After accessing the matched LegalInput.ini record, AutoHotkey uses the StrSplit() function to parses the returned variable LegalTerm into its fields delimited by the vertical line character ( | ) and places the results into the array Legal_Array.
The script then uses MsgBox to display the results.
These snippets of code represent a basic approach to retrieving data for an INI table. From here, you’ll find it simple to add code for inserting the Latin phrase into any document:
SendInput, % Legal_Array[2]
The First Legalese Script
You can copy this first script below:
Loop, Read, LegalInput.ini { If A_Index = 1 Continue Input_Array := StrSplit(A_LoopReadLine, "=") Input_String := Input_String . Input_Array[1] . "," } Return ^!o:: SplashTextOn,100, 40, Enter legal code!, Waiting for Input! Input , OutputVar, L4 T10, , %Input_String% SplashTextOff If (ErrorLevel = "Match") { IniRead, LegalTerm, LegalInput.ini, InputKey, %OutputVar% Legal_Array := StrSplit(LegalTerm, "|") MsgBox, , Hotstring %OutputVar%, % Legal_Array[1] . "`r`r" . Legal_Array[2] . "`r`r" . Legal_Array[3] } Else MsgBox No match! %ErrorLevel% %OutputVar% Return
The Hotkey combination CTRL+ALT+O activates the Input search.
I’ve posted the initial INI file LegalInput.ini for copying or download. You’ll need this data table for the above snippet to work properly.
Next time, I’ll reconfigure the script to insert either the Latin phrase or English translation. Later, I’ll show you how to create a menu of Latin phrases from the first few letters of the phrase. Finally, I’ll demonstrate how to put the data table in a ListView GUI control for multiple uses.
This post was proofread by Grammarly
(Any other mistakes are all mine.)
(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)
[…] next step in “Input Command Creates Temporary Hotstrings from Data Table (AutoHotkey INI File Technique)” which add more access to Latin legal […]
LikeLike
[…] few weeks back I demonstrated how to build an INI data table for driving AutoHotkey scripts. I used the INI file format (LegalInput.ini) because it includes one […]
LikeLike