Determine sentence type

Revision as of 22:37, 6 November 2021 by Tigerofdarkness (talk | contribs) (→‎{{header|ALGOL 68}}: Bug fix - doh!)

Use this sentence: "hi there, how are you today? I'd like to present to you the washing machine 9001. You have been nominated to win one of these! Just make sure you don't break it"

Determine sentence type is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task
Search for the last used punctuation in a sentence, and determine its type according to its punctuation
Output one of these letters
"E" (Exclamation!), "Q" (Question?), "S" (Serious.), "N" (Neutral)
Extra
Make your code able to determine multiple sentences


Don't leave any errors!


Other tasks related to string operations:
Metrics
Counting
Remove/replace
Anagrams/Derangements/shuffling
Find/Search/Determine
Formatting
Song lyrics/poems/Mad Libs/phrases
Tokenize
Sequences



ALGOL 68

Classifies an empty string as "". <lang algol68>BEGIN # determuine the type of a sentence by looking at the final punctuation #

   CHAR exclamation = "E"; # classification codes... #
   CHAR question    = "Q";
   CHAR serious     = "S";
   CHAR neutral     = "N";
   # returns the type(s) of the sentence(s) in s - exclamation, question,     #
   #                     serious or neutral; if there are multiple sentences  #
   #                     the types are separated by |                         #
   PROC classify = ( STRING s )STRING:
        BEGIN
           STRING result := "";
           BOOL pending neutral := FALSE; 
           FOR s pos FROM LWB s TO UPB s DO
               IF   pending neutral := FALSE;
                    CHAR c = s[ s pos ];
                    c = "?"
               THEN result +:= question    + "|"
               ELIF c = "!"
               THEN result +:= exclamation + "|"
               ELIF c = "."
               THEN result +:= serious     + "|"
               ELSE pending neutral := TRUE
               FI
           OD;
           IF   pending neutral
           THEN result +:= neutral + "|"
           FI;
           # if s was empty, then return an empty string, otherwise remove the final separator #
           IF result = "" THEN "" ELSE result[ LWB result : UPB result - 1 ] FI
        END # classify # ;
   # task test case #
   print( ( classify( "hi there, how are you today? I'd like to present to you the washing machine 9001. "
                    + "You have been nominated to win one of these! Just make sure you don't break it"
                    )
          , newline
          )
        )

END</lang>

Output:
Q|S|E|N

AutoHotkey

<lang autohotkey>Sentence := "hi there, how are you today? I'd like to present to you the washing machine 9001. You have been nominated to win one of these! Just make sure you don't break it" Msgbox, % SentenceType(Sentence)

SentenceType(Sentence) { Sentence := Trim(Sentence) Loop, Parse, Sentence, .?! { N := (!E && !Q && !S) , S := (InStr(SubStr(Sentence, InStr(Sentence, A_LoopField)+StrLen(A_LoopField), 3), ".")) , Q := (InStr(SubStr(Sentence, InStr(Sentence, A_LoopField)+StrLen(A_LoopField), 3), "?")) , E := (InStr(SubStr(Sentence, InStr(Sentence, A_LoopField)+StrLen(A_LoopField), 3), "!")) , type .= (E) ? ("E|") : ((Q) ? ("Q|") : ((S) ? ("S|") : "N|")) , D := SubStr(Sentence, InStr(Sentence, A_LoopField)+StrLen(A_LoopField), 3) } return (D = SubStr(Sentence, 1, 3)) ? RTrim(RTrim(type, "|"), "N|") : RTrim(type, "|") }</lang>

Output:
Q|S|E|N