Determine sentence type: Difference between revisions

added Easylang
(Created Nim solution.)
(added Easylang)
 
(9 intermediate revisions by 5 users not shown)
Line 159:
S Serious.
N Neutral
</pre>
 
=={{header|Bruijn}}==
<syntaxhighlight lang="bruijn">
:import std/List .
:import std/Char .
 
determine [∅?0 '?' ([(0 =? '?' 'Q' (0 =? '.' 'S' (0 =? '!' 'E' 'N')))] _0)]
 
:test (determine empty) ('?')
:test (determine "hi there, how are you today?") ('Q')
:test (determine "I'd like to present to you the washing machine 9001.") ('S')
:test (determine "You have been nominated to win one of these!") ('E')
:test (determine "Just make sure you don't break it") ('N')
</syntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
 
#include <exception>
#include <iostream>
#include <string>
#include <vector>
 
char sentence_type(const std::string& sentence) {
if ( sentence.empty() ) {
throw std::invalid_argument("Cannot classify an empty sentence");
}
 
char result;
const char last_character = sentence.back();
switch (last_character) {
case '?': result = 'Q'; break;
case '.': result = 'S'; break;
case '!': result = 'E'; break;
default: result = 'N'; break;
};
return result;
}
 
int main() {
const std::vector<std::string> sentences = { "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" };
 
for ( const std::string& sentence : sentences ) {
std::cout << sentence << " -> " << sentence_type(sentence) << std::endl;
}
}
</syntaxhighlight>
<pre>
hi there, how are you today? -> Q
I'd like to present to you the washing machine 9001. -> S
You have been nominated to win one of these! -> E
Just make sure you don't break it -> N
</pre>
 
Line 267 ⟶ 323:
You have been nominated to win one of these! (E)
Just make sure you don''t break it (N)
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func$ stype s$ .
h$ = substr s$ len s$ 1
ind = strpos "?!." h$ mod1 4
return substr "QESN" ind 1
.
txt$[] = [ "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" ]
for t$ in txt$[]
print t$ & " -> " & stype t$
.
</syntaxhighlight>
{{out}}
<pre>
hi there, how are you today? -> Q
I'd like to present to you the washing machine 9001. -> S
You have been nominated to win one of these! -> E
Just make sure you don't break it -> N
</pre>
 
Line 454 ⟶ 530:
<pre>
Q|S|E|N
</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.util.List;
 
public final class DetermineSentenceType {
 
public static void main(String[] aArgs) {
List<String> sentences = List.of( "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" );
 
for ( String sentence : sentences ) {
System.out.println(sentence + " -> " + sentenceType(sentence));
}
}
private static char sentenceType(String aSentence) {
if ( aSentence.isEmpty() ) {
throw new IllegalArgumentException("Cannot classify an empty sentence");
}
final char lastCharacter = aSentence.charAt(aSentence.length() - 1);
return switch (lastCharacter) {
case '?' -> 'Q';
case '.' -> 'S';
case '!' -> 'E';
default -> 'N';
};
}
}
</syntaxhighlight>
{{ out }}
<pre>
hi there, how are you today? -> Q
I'd like to present to you the washing machine 9001. -> S
You have been nominated to win one of these! -> E
Just make sure you don't break it -> N
</pre>
 
Line 827 ⟶ 945:
 
The syntax highlighting here for Raku isn't the best. | S</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
see "working..." + nl
sType = []
sent = "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! How do you like this washing machine? Buy it, don't hesitate! You will be satisfied with it. Just make sure you don't break it"
 
ind = 1
while true
pos = substring(sent,"?",ind)
if pos > 0
add(sType,pos)
ind = pos+1
else
exit
ok
end
 
ind = 1
while true
pos = substring(sent,"!",ind)
if pos > 0
add(sType,pos)
ind = pos+1
else
exit
ok
end
 
ind = 1
while true
pos = substring(sent,".",ind)
if pos > 0
add(sType,pos)
ind = pos+1
else
exit
ok
end
 
if pos < len(sent)
neut = "N"
else
neut = ""
ok
 
sType = sort(sType)
 
text = ""
for n = 1 to len(sType)
if sent[sType[n]] = "?"
text = text + "Q" + "|"
ok
if sent[sType[n]] = "!"
text = text + "E" + "|"
ok
if sent[sType[n]] = "."
text = text + "S" + "|"
ok
next
see text + neut + nl
see "done..." + nl
 
func substring str,substr,n
newstr=right(str,len(str)-n+1)
nr = substr(newstr, substr)
if nr = 0
return 0
else
return n + nr -1
ok
</syntaxhighlight>
{{out}}
<pre>
working...
Q|S|E|Q|E|S|N
done...
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.8}}
≪ OVER SIZE → str seps len
≪ 1 CF { 0 } 1 len FOR j
IF seps str j DUP SUB POS THEN
j + IF j len == THEN 1 SF END
END NEXT
IF 1 FC? THEN len + END
{ } SWAP 1 OVER SIZE 1 - FOR j
str OVER j GETI 1 + ROT ROT
GET SUB ROT SWAP + SWAP
NEXT DROP
≫ ≫ ''''PARSE'''' STO
≪ "?!." SWAP OVER '''PARSE''' "QES" → seps sents types
≪ 1 sents SIZE FOR j
sents j GET seps
IF OVER DUP SIZE DUP SUB POS THEN
"=" types LAST DUP SUB +
ELSE "=N" END +
NEXT
≫ ≫ ''''STYPE'''' STO
 
"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" '''STYPE'''
{{out}}
<pre>
4: "hi there, how are you today?=Q"
3: " I'd like to present to you the washing machine 9001.=S"
2: " You have been nominated to win one of these!=E"
1: " Just make sure you don't break it=N"
</pre>
 
=={{header|V (Vlang)}}==
Line 861 ⟶ 1,089:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var sentenceType = Fn.new { |s|
if (s.count == 0) return ""
var types = []
Line 888 ⟶ 1,116:
{{libheader|Wren-iterate}}
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="ecmascriptwren">import "./pattern" for Pattern
import "./iterate" for Indexed
 
1,978

edits