Determine sentence type: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Typo.) |
Not a robot (talk | contribs) (Add CLU) |
||
Line 86: | Line 86: | ||
{{out}} |
{{out}} |
||
<pre>Q|S|E|N</pre> |
<pre>Q|S|E|N</pre> |
||
=={{header|CLU}}== |
|||
<lang clu>% This iterator takes a string and yields one of 'E', 'Q', |
|||
% 'S' or 'N' for every sentence found. |
|||
% Because sentences are separated by punctuation, only the |
|||
% last one can be 'N'. |
|||
sentence_types = iter (s: string) yields (char) |
|||
own punct: string := "!?." % relevant character classes |
|||
own space: string := " \t\n" |
|||
own types: string := "EQS" % sentence type characters |
|||
prev_punct: bool := false % whether the previous character was punctuation |
|||
last_punct: int := 0 % index of last punctuation character encountered |
|||
sentence: bool := true % whether there are words since the last punctuation |
|||
for c: char in string$chars(s) do |
|||
pu: int := string$indexc(c, punct) |
|||
sp: int := string$indexc(c, space) |
|||
if pu ~= 0 then |
|||
prev_punct := true |
|||
last_punct := pu |
|||
elseif sp ~= 0 then |
|||
if prev_punct then |
|||
% a space after punctuation means a sentence has ended here |
|||
yield(types[last_punct]) |
|||
sentence := false |
|||
end |
|||
prev_punct := false |
|||
sentence := false |
|||
else |
|||
sentence := true |
|||
end |
|||
end |
|||
% handle the last sentence |
|||
if prev_punct then yield(types[last_punct]) |
|||
elseif sentence then yield('N') |
|||
end |
|||
end sentence_types |
|||
% Test |
|||
start_up = proc () |
|||
po: stream := stream$primary_output() |
|||
test: string := |
|||
"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 the type of each sentence |
|||
for c: char in sentence_types(test) do |
|||
stream$putc(po, c) |
|||
end |
|||
end start_up </lang> |
|||
{{out}} |
|||
<pre>QSEN</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |