Determine sentence type: Difference between revisions

Content added Content deleted
(Added XPL0 example.)
m (syntax highlighting fixup automation)
Line 24: Line 24:
{{trans|Go}}
{{trans|Go}}


<lang 11l>F sentenceType(s)
<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))</lang>
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 "".
<lang algol68>BEGIN # determuine the type of a sentence by looking at the final punctuation #
<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</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 96: Line 96:


=={{header|AutoHotkey}}==
=={{header|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"
<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, "|")
}</lang>
}</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}}==
<lang clu>% This iterator takes a string and yields one of 'E', 'Q',
<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 </lang>
end start_up </syntaxhighlight>
{{out}}
{{out}}
<pre>QSEN</pre>
<pre>QSEN</pre>


=={{header|Epoxy}}==
=={{header|Epoxy}}==
<lang epoxy>const SentenceTypes: {
<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")</lang>
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}}
<lang factor>USING: combinators io kernel regexp sequences sets splitting
<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</lang>
"Are you sure?!?! How can you know?" show</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 330: Line 330:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>function sentype( byref s as string ) as string
<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</lang>
wend</syntaxhighlight>
{{out}}<pre>Q
{{out}}<pre>Q
S
S
Line 368: Line 368:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}
<lang go>package main
<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))
}</lang>
}</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</lang>
s | sentenceTypes</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 442: Line 442:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>const text = """
<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
</lang>{{out}}
</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}}==
<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"
<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</lang>
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}}==
<lang perl>use strict;
<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' }
}
}
}</lang>
}</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}}==
<!--<lang Phix>(phixonline)-->
<!--<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 528: Line 528:


=={{header|Python}}==
=={{header|Python}}==
<lang python>import re
<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]))
</lang>{{out}}
</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:
<lang python>'''Grouping and tagging by final character of string'''
<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()</lang>
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 perl6>use Lingua::EN::Sentence;
<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' };
}
}
}</lang>
}</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}}
<lang vlang>fn sentence_type(s string) string {
<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))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 748: Line 748:


=={{header|Wren}}==
=={{header|Wren}}==
<lang ecmascript>var sentenceType = Fn.new { |s|
<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))</lang>
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.
<lang ecmascript>import "./pattern" for Pattern
<syntaxhighlight lang="ecmascript">import "./pattern" for Pattern
import "./trait" for Indexed
import "./trait" for Indexed


Line 802: Line 802:
}
}
System.print()
System.print()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 819: Line 819:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>include xpllib; \for StrLen
<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, ^|);
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}