April 24, 2020
A JavaScript Transitive Verb Detector
Unsurprisingly, language, texts, and literature play a central role in my coding. On many occasions – for instance, with my iambic pentameter generator and my rhyming anapest generator, just to name two – one common issue has been that the code couldn’t separate transitive from intransitive verbs. Annoyed enough by that, I decided to code a JavaScript transitive verb detector.
Just in case you need a reminder, a transitive verb is one that can take one or more objects: “She drank water”. Conversely, an intransitive verb is one that cannot take an object: “He sneezed”. We can’t say “*He sneezed his nose” or something like that.
A JavaScript transitive verb detector is very useful when creating random text (as I often do). Without a transitive verb detector, some very peculiar random sentences can be formed, that are basically ungrammatical, like the one we saw above.
The best thing about this JavaScript transitive verb detector is that it’s fairly simple, too!
Making a JavaScript Transitive Verb Detector
Note: I’m explaining my methodology for teaching purposes, but the full code will be available at the end of this post, no worries!
As I started pondering on my strategy – that is, how to go about making a program that would detect transitive verbs – I had to first decide something.
Would it be easier to make a JavaScript transitive verb detector, or a JavaScript intransitive verb detector?
Believe it or not, I found the answer in… philosophy. After all, as I’ve said before on my post on learning how to learn, knowledge is “a vast network of interconnected disciplines, all supporting each other”. And so, I realized it was a matter of the burden of proof. It was much easier to detect something that existed, rather than something that didn’t, and so I decided it would be much easier to go with a program that detects whether a verb is transitive (rather than intransitive).
If a verb takes a noun, end of story.
So, how Can We Detect Transitive Verbs Using JavaScript?
Obviously enough, we would need to detect if a verb takes a noun. As I mentioned just above, if we can detect that a verb takes a noun, we’re halfway there – and now you got an earworm, I know.
As you might remember from past projects, I use the awesome RiTa library a lot, together with the equally excellent Datamuse API.
My idea in creating this JavaScript transitive verb detector was to
- take the random verb created by RiTa
- pass it to Datamuse to see words that often follow it. (e.g. if the verb was “drink”, Datamuse would return “wine”, “water”, “milk” – but also “some”, “heavily”, “more”, etc.)
- check how many of those words are nouns
Originally I thought that if no.3 returned no words, that would be enough. It turned out to be a bit more complicated than that, for two reasons:
- Datamuse sometimes returned very few or no results, if the verb in question was very rare.
- RiTa’s noun detector function proved to be a bit problematic, as it detects words that can be used as a noun.
And so, RiTa marked as nouns words like “can”. Well, “can”… can be a noun (“This is a can of worms”), but not in a sentence such as “I sneezed, can you bring me a tissue?”
Clearly, I needed to do more.
Ironing out Issues
I went around these issues by creating two arrays, which I called group1 and group2. After Datamuse returned the array of words usually following the verb, I used RiTa to separate the words into nouns and non-nouns. Words that RiTa considered nouns were added to group1, others to group2.
Then, I compared the array lengths. Although it’s not infallible, I discovered that if group1 was larger than group2, that was the end of story. But there were cases where a verb could be followed by many non-noun words, thus rendering group2 larger.
I decided to add an if-statement measuring the ratio between the two arrays. After some experimentation, I discovered that if group2 was 3.5 times (or more) larger than group1, it meant that the verb was likely intransitive.
JavaScript Transitive Verb Detector: the Code
Here’s an example to get you started.
Keep in mind that this is based on code I composed many years ago, when I didn’t know much about json objects, async functions, and whatnot. The code works, but it’s ugly and redundant. Feel free to use it, but also feel free to correct the sloppiness I’m too lazy to fix 😉
This should be pretty self-explanatory, especially if you read the previous sections. The code uses RiTa to create two random nouns and one random verb (lines 10-12). It then passes both nouns to Datamuse to return some matching adjectives (lines 14-17; notice the Datamuse function starting on line 39). After that, the code checks whether the verb is transitive or intransitive, as I described earlier in the post (lines 19-25; notice function starting on line 30). Lines 27-28 simply put the sentence together and show it to the viewer.
If you have a question, a comment, or an idea for improving this JavaScript transitive verb detector, feel free to leave a comment below.