Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

A few grammar checks for strings

by Lady_Aleena (Chaplain)
on Feb 04, 2013 at 23:20 UTC ( #1017027=CUFP: print w/ replies, xml ) Need Help??

I was digging around some old scripts in my play directory and found a script I started writing for form inputs for my site. It checks for eight little grammar problems or typographical errors, some of which I am guilty of every so often. If you can think of any more which would fit the format, please let me know.

You can take out WORDLIST and just use join if you do not feel like installing Lingua::EN::Inflect (my favorite module). If you do not want to use the last until, just take out the returns in correct_grammar.

I do not know how cool this is, but I think it is a bit cute.

#!/usr/bin/perl use strict; use warnings; use Lingua::EN::Inflect qw(WORDLIST); local $\ = "\n"; print "Enter string: "; my $string = <>; chomp($string); my @conjunctions_list = qw(and but or nor); my @prepositions_list = qw( aboard about above across after against along amid among an +ti around as at before behind below beneath beside besides between beyond bu +t by concerning considering despite down during except excepting excluding following for from in inside into like minus near of off on onto opposite outside over past per plus regarding round save since than through to toward towards under underneath unlike until up upon versus via with within without ); sub correct_grammar { my ($string_to_check) = shift; my $CAPS = join('|',"A".."Z"); my $conjunctions = join('|',@conjunctions_list); my $prepositions = join('|',@prepositions_list); my @errors; push @errors, "have not used punctuation" if ($string_to_check +!~ /(?:\.|\!|\?|\,|\;)/); push @errors, "have not spaced your words out" if ($string_to_check +!~ /\s/); push @errors, "have not capitalized anything" if ($string_to_check +!~ /(?:$CAPS)/); push @errors, "have two capital letters in a row in your text" if ($ +string_to_check =~ /(?:$CAPS)(?:$CAPS)/); push @errors, "started with a conjunction" if ($string_to_check +=~ /^(?:$conjunctions) /i); push @errors, "ended with a preposition" if ($string_to_check +=~ / (?:$prepositions)(|\W)$/i); push @errors, "have a number which should be written out" if ($strin +g_to_check =~ / \d\d /); push @errors, "misspelled 'the'" if ($string_to_check +=~ / teh /i); if (scalar(@errors) > 0) { print "It appears you ".WORDLIST(@errors,{conj => 'and'}).'.'; return 0; } else { print "Good job, and thank you!"; return 1; } } until (correct_grammar($string)) { $string = <>; chomp($string); }

Have fun with it!

Have a cookie and a very nice day!
Lady Aleena

Comment on A few grammar checks for strings
Select or Download Code
Re: A few grammar checks for strings
by Not_a_Number (Parson) on Feb 05, 2013 at 00:42 UTC

    Hmm...

    All of the following sentences are perfectly grammatical in English:

    • It was on the BBC website.
    • Yes!
    • Can you get through?
    • Welcome aboard!
    • And God said 'Let there be light.'

    Yet all are rejected by your code.

    Back to the drawing board?

      Hello Not_a_Number. The script is very basic; so, as you have seen, there are a few problems. I hope you liked the concept in general.

      Have a cookie and a very nice day!
      Lady Aleena

      Them rules are doin me fucken heid in!
      /IrvineWelsh

      SCNR :)

Re: A few grammar checks for strings
by ww (Bishop) on Feb 05, 2013 at 02:12 UTC
    Many of the words in the so-called $prepositions list are NOT prepositions so the var name is poorly chosen; the more so since your code also faults the words' use at the end of a sentence. But (oops, I started a sentence with a preposition. Uh, no, it's a conjunction. That's still contrary to some of the so-called "best standards" but.... ah well.) let's look first at some of those words used first in a sentence:
    • About 30% of all code is garbage.
    • Beyond the hills lies the ocean.
    • Following the calves were the wolves.

    The last can be faulted for not using clearer construction, but passes muster for any strict test of grammar. The second is easy enough to defend at poetic, at worst and the first is a legitmate use of the passive voice to stress the asserted percentage.

    As to ending sentences with your "prepositions," try this:
    • "The bowl is 9 inches across."

      Hello ww. I looked up "but" and found it is a conjunction and a preposition. For your last sentence, "The bowl has a nine inch radius." :)

      Have a cookie and a very nice day!
      Lady Aleena
        Caught me out! And -- for the edification of future readers -- in addition to conj. and prep., "but" can also be or 'be used as':
        • a noun "I'll take no but as an answer"
        • a pronoun "no one but has his fault..." (or, depending on your ed. of Shakespeare, "nobody but..."
        • an adverb "That's but a little tree."
        That should have been either 4.5 inch of radius or 9 inch of diameter ("nine inch *across*").
Re: A few grammar checks for strings
by Anonymous Monk on Feb 05, 2013 at 12:57 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1017027]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-09-22 09:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (185 votes), past polls