Determine sentence type: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: Reinterpreted N tag as 'not punctuated')
(→‎{{header|Wren}}: Added an alternative version.)
Line 526: Line 526:
<pre>
<pre>
Q|S|E|N
Q|S|E|N
</pre>
<br>
{{libheader|Wren-pattern}}
{{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 though an inevitably incomplete list of abbreviations.
<lang ecmascript>import "./pattern" for Pattern
import "./trait" for Indexed

var map = { "?": "Q", "!": "E", ".": "S", "": "N" }
var p = Pattern.new("[? |! |. ]")
var paras = [
"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",
"hi there, how are you on St.David's day (isn't it a holiday yet?), Mr.Smith? I'd like to present to you (well someone has to win one!) the washing machine 900.1. You have been nominated by Capt.Johnson('?') to win one of these! Just make sure you (or Mrs.Smith) don't break it. By the way, what the heck is an exclamatory question!?"
]

for (para in paras) {
para = para.trim()
var sentences = p.splitAll(para)
var endings = p.findAll(para).map { |m| m.text[0] }.toList
var lastChar = sentences[-1][-1]
if ("?!.".contains(lastChar)) {
endings.add(lastChar)
sentences[-1] = sentences[-1][0...-1]
} else {
endings.add("")
}
for (se in Indexed.new(sentences)) {
var ix = se.index
var sentence = se.value
System.print("%(map[endings[ix]]) <- %(sentence + endings[ix])")
}
System.print()
}</lang>

{{out}}
<pre>
Q <- hi there, how are you today?
S <- I'd like to present to you the washing machine 9001.
E <- You have been nominated to win one of these!
N <- Just make sure you don't break it

Q <- hi there, how are you on St.David's day (isn't it a holiday yet?), Mr.Smith?
S <- I'd like to present to you (well someone has to win one!) the washing machine 900.1.
E <- You have been nominated by Capt.Johnson('?') to win one of these!
S <- Just make sure you (or Mrs.Smith) don't break it.
Q <- By the way, what the heck is an exclamatory question!?
</pre>
</pre>