Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #49202

Re: Parse a sentence

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: Parse a sentence
Date Sun, 17 Jul 2016 10:24:59 -0000 (UTC)
Organization A noiseless patient Spider
Lines 62
Message-ID <nmfmdq$eoc$1@dont-email.me> (permalink)
References <nmapgl$n77$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=iso-8859-1
Injection-Date Sun, 17 Jul 2016 10:24:59 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="be22b3089a241276b6e4a5effd95e736"; logging-data="15116"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/73SNLiyMqHSkADNJTOdO"
User-Agent XanaNews/1.18.1.6
Cancel-Lock sha1:cl1wx5Rva/e/rwgQa+BiT2uJFJI=
Xref csiph.com comp.lang.forth:49202

Show key headers only | View raw


WJ wrote:

> Ruby:
> 
> result = "  Alas poor Yorick!?  I knew him well  , Horatio .  ".
>   scan(/\w+|[^\w\s]/)
>  ===>
> ["Alas", "poor", "Yorick", "!", "?", "I", "knew", "him", "well",
>  ",", "Horatio", "."]
> 
> result.join("|")
>  ===>
> "Alas|poor|Yorick|!|?|I|knew|him|well|,|Horatio|."
> 
> 
> Ocaml:
> 
> #load "str.cma";;
> 
> let parse_sentence string =
>   List.fold_right
>     (fun el accum ->
>       match el with
>           Str.Text s -> s::accum
>         | Str.Delim " " -> accum
>         | Str.Delim s -> s::accum)
>     (Str.full_split (Str.regexp "[^a-zA-Z]") string)
>     [] ;;
> 
> let result = parse_sentence
>   "  Alas poor Yorick!?  I knew him well  , Horatio .  " ;;
>  ===>
> ["Alas"; "poor"; "Yorick"; "!"; "?"; "I"; "knew"; "him"; "well";
>  ","; "Horatio"; "."]
> 
> String.concat "|" result ;;
>  ===>
> "Alas|poor|Yorick|!|?|I|knew|him|well|,|Horatio|."
> 
> 
> ANS Forth?

R:

str.scan = function(rx,str, ignore.case=FALSE, fixed=FALSE){
  m = gregexpr(rx, str, ignore.case=ignore.case, fixed=fixed, perl=TRUE)
  regmatches(str, m)[[1]]}

result = str.scan(
  "[a-z]+|[,.!?]",
  "  Alas poor Yorick!?  I knew him well  , Horatio .  ",
  ignore=TRUE)

result
 ===>
 [1] "Alas"    "poor"    "Yorick"  "!"       "?"       "I"       "knew"
 [8] "him"     "well"    ","       "Horatio" "."

paste(result, collapse="|")
 ===>
[1] "Alas|poor|Yorick|!|?|I|knew|him|well|,|Horatio|."

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Parse a sentence "WJ" <w_a_x_man@yahoo.com> - 2016-07-15 13:47 +0000
  Re: Parse a sentence François <francois.ganza@wanadoo.fr> - 2016-07-15 07:10 -0700
  Re: Parse a sentence Richard Todd <richard.wesley.todd@gmail.com> - 2016-07-15 09:35 -0700
  Parse a sentence Mark Wills <markwills1970@gmail.com> - 2016-07-16 03:03 -0700
  Re: Parse a sentence "WJ" <w_a_x_man@yahoo.com> - 2016-07-17 10:24 +0000
  Re: Parse a sentence endlessboomcapitalism@gmail.com - 2016-07-17 21:19 -0700

csiph-web