Jump to content

Determine sentence type: Difference between revisions

m
syntax highlighting fixup automation
(Added XPL0 example.)
m (syntax highlighting fixup automation)
Line 24:
{{trans|Go}}
 
<langsyntaxhighlight lang="11l">F sentenceType(s)
I s.empty
R ‘’
Line 43:
 
V s = ‘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’
print(sentenceType(s))</langsyntaxhighlight>
 
{{out}}
Line 52:
=={{header|ALGOL 68}}==
Classifies an empty string as "".
<langsyntaxhighlight lang="algol68">BEGIN # determuine the type of a sentence by looking at the final punctuation #
CHAR exclamation = "E"; # classification codes... #
CHAR question = "Q";
Line 89:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 96:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight 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)
 
Line 111:
}
return (D = SubStr(Sentence, 1, 3)) ? RTrim(RTrim(type, "|"), "N|") : RTrim(type, "|")
}</langsyntaxhighlight>
 
{{out}}
<pre>Q|S|E|N</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f DETERMINE_SENTENCE_TYPE.AWK
BEGIN {
Line 147:
sentence = ""
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 162:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% This iterator takes a string and yields one of 'E', 'Q',
% 'S' or 'N' for every sentence found.
% Because sentences are separated by punctuation, only the
Line 214:
stream$putc(po, c)
end
end start_up </langsyntaxhighlight>
{{out}}
<pre>QSEN</pre>
 
=={{header|Epoxy}}==
<langsyntaxhighlight lang="epoxy">const SentenceTypes: {
["?"]:"Q",
["."]:"S",
Line 243:
cls
 
GetSentences("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")</langsyntaxhighlight>
{{out}}
<pre>
Line 255:
This program attempts to prevent common abbreviations from ending sentences early. It also tries to handle parenthesized sentences and implements an additional type for exclamatory questions (EQ).
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: combinators io kernel regexp sequences sets splitting
wrap.strings ;
 
Line 312:
"Hello, Mr. Anderson!" show
nl
"Are you sure?!?! How can you know?" show</langsyntaxhighlight>
{{out}}
<pre>
Line 330:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function sentype( byref s as string ) as string
'determines the sentence type of the first sentence in the string
'returns "E" for an exclamation, "Q" for a question, "S" for serious
Line 359:
while len(spam)>0
print sentype(spam)
wend</langsyntaxhighlight>
{{out}}<pre>Q
S
Line 368:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 398:
s := "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"
fmt.Println(sentenceType(s))
}</langsyntaxhighlight>
 
{{out}}
Line 411:
The following parses sentences with embedded quotations naively,
so that for example the sentence "He asked 'How are you?'." results in: Q S
<syntaxhighlight lang="jq">
<lang jq>
# Input: a string
# Output: a stream of sentence type indicators
Line 432:
def s: "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";
 
s | sentenceTypes</langsyntaxhighlight>
{{out}}
<pre>
Line 442:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const text = """
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"""
Line 454:
println(rpad(parsed[i] * parsed[i + 1], 52), " ==> ", haspunctotype(parsed[i + 1]))
end
</langsyntaxhighlight>{{out}}
<pre>
Hi there, how are you today? ==> Q
Line 463:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">text = "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"
p2t = { [""]="N", ["."]="S", ["!"]="E", ["?"]="Q" }
for s, p in text:gmatch("%s*([^%!%?%.]+)([%!%?%.]?)") do
print(s..p..": "..p2t[p])
end</langsyntaxhighlight>
{{out}}
<pre>hi there, how are you today?: Q
Line 475:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 501:
else { say 'N' }
}
}</langsyntaxhighlight>
{{out}}
<pre>hi there, how are you today?| Q
Line 511:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">`hi there, how are you today? I'd like to present
Line 521:
<span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'|'</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">w</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 528:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import re
 
txt = """
Line 543:
for i in range(0, len(pars)-1, 2):
print((pars[i] + pars[i + 1]).ljust(54), "==>", haspunctotype(pars[i + 1]))
</langsyntaxhighlight>{{out}}
<pre>
Hi there, how are you today? ==> Q
Line 553:
 
Or for more generality, and an alternative to hand-crafted regular expressions:
<langsyntaxhighlight lang="python">'''Grouping and tagging by final character of string'''
 
from functools import reduce
Line 666:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>('E', ['You have been nominated to win one of these!', "But perhaps substance isn't the goal!"])
Line 675:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Sentence;
 
my $paragraph = q:to/PARAGRAPH/;
Line 697:
default { 'N' };
}
}</langsyntaxhighlight>
{{out}}
<pre>hi there, how are you today? | Q
Line 717:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">fn sentence_type(s string) string {
if s.len == 0 {
return ""
Line 740:
s := "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"
println(sentence_type(s))
}</langsyntaxhighlight>
 
{{out}}
Line 748:
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">var sentenceType = Fn.new { |s|
if (s.count == 0) return ""
var types = []
Line 765:
 
var s = "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"
System.print(sentenceType.call(s))</langsyntaxhighlight>
 
{{out}}
Line 775:
{{libheader|Wren-trait}}
The following alternative version takes the simplistic view that (unless they end the final sentence of the paragraph) '''?''', '''!''' or '''.''' will only end a sentence if they're immediately followed by a space. This of course is nonsense, given the way English is written nowadays, but it's probably an improvement on the first version without the need to search through an inevitably incomplete list of abbreviations.
<langsyntaxhighlight lang="ecmascript">import "./pattern" for Pattern
import "./trait" for Indexed
 
Line 802:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 819:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include xpllib; \for StrLen
int Sentence, N, Len;
char Str;
Line 836:
if N < 3 then ChOut(0, ^|);
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.