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