Home For Fiction – Blog

for thinking people

There are no ads, nor any corporate masters
How to show support


April 24, 2020

A JavaScript Transitive Verb Detector

Programming

javascript, language, programming, transitive verbs

5 comments

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!

JavaScript transitive verb detector
A JavaScript transitive verb detector helps us determine the difference between “I see clouds” and “I fly”. Parenthetically, the code superimposed on the image is the actual code used (see the end of this post)

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

  1. take the random verb created by RiTa
  2. 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.)
  3. 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:

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?”

RiTa also, rather inexplicably, returned true (i.e. marking as nouns) words such as “in” or “out”.

Clearly, I needed to do more.

home for fiction

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.

5 Comments

  1. I like your appeal to philosophy; makes sense.

    Wouldn’t it have been easier to search a dictionary which already has definitions which include transitive/intransitive – made by humans? Just curious.

    1. Chris🚩 Chris

      I actually spent some time searching for one, but unfortunately couldn’t find any – not in JavaScript at least. The RiTa library I use can generate verbs in present or past tense, but doesn’t have a way to differentiate between transitive and intransitive verbs. In theory, it should be easy to implement but hard to execute – it would require a human to go through the entire dictionary of RiTa (tens of thousands of words) and mark each verb as transitive or intransitive. Thanks for your comment!

      1. Well, you seem to have come up with a good substitute – I’m surprised you can’t find an annotated dictionary that includes transitive/intransitive (and leaves the ones that are both to human interaction – there can’t be that many of them).

        Clever program.

  2. leon h leon h

    This is very good. Is it possible to use the tool online? Is it available anywhere?

    1. Chris🚩 Chris

      Thanks for your comment. I haven’t prepared any ready tool, where e.g. you can input text and receive an analysis of whether the verb would be transitive or intransitive. That would likely require more work than what I’d be ready to put in at this point. Of course, I might do that in the future πŸ™‚


Punning Walrus shrugging

Comments are closed for posts older than 90 days