Jump to content

Determine sentence type: Difference between revisions

(Added 11l)
Line 370:
<pre>
Q|S|E|N
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
The following parses sentences with embedded quotations naively,
so that for example the sentence "He asked 'How are you?'." results in: Q S
<lang jq>
# Input: a string
# Output: a stream of sentence type indicators
def sentenceTypes:
def trim: sub("^ +";"") | sub(" +$";"");
def parse:
capture("(?<s>[^?!.]*)(?<p>[?!.])(?<remainder>.*)" )
// {p:"", remainder:""};
def encode:
if . == "?" then "Q"
elif . == "!" then "E"
elif . == "." then "S"
else "N"
end;
trim
| select(length>0)
| parse
| (.p | encode), (.remainder | sentenceTypes);
 
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>
{{out}}
<pre>
Q
S
E
N
</pre>
 
2,472

edits

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