[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Extracting vowels and consonants using regular expression

Dondi

2/2/2008 2:59:00 PM

I am trying to parse a string and extract all vowels and consonants
into two separate substrings. However, I can't get my solution to
work. Any pointers are appreciated. Here is the approach I am using:

1) Extended the String Class with the following methods:

class String
def vowels
self.scan(/[aeiou]|(?![aeiou])y(?![aeiou])/i)
end

def consonants
self.scan(/![aeiou]|(?=[aeiou])y(?=[aeiou])/i)
end
end

2) Invoke the methods:

test_paragraph = "Mary had a little lamb"
@vowel_sub_str = test_paragraph.vowels
@consonant_sub_str = test_paragraph.consonants

However, the result is just two empty strings. I believe the problem
is in the regular expression, but I can't figure out just where. Any
ideas/pointers are appreciated.

Thanks.
11 Answers

Phlip

2/2/2008 3:07:00 PM

0

Dondi wrote:

> I am trying to parse a string and extract all vowels and consonants
> into two separate substrings. However, I can't get my solution to
> work. Any pointers are appreciated. Here is the approach I am using:
>
> 1) Extended the String Class with the following methods:

Your attempt has two procedural problems. Your "invoke" part has no
assertions, such as "assert_equal". They would preserve your experiments, to
help you maintain this code. And you have prematurely put your scan inside
the String class. You should start with a much simpler regular expression,
in a test case, and only "productize" it after it works.

Beyond that, I'm not sure what scan does, but what does this experiment
return?

self.scan(/([aeiou]|(?![aeiou])y(?![aeiou]))/i)

p $1

I always trap regular expression results in their magic $1 variables!

--
Phlip


Stefano Crocco

2/2/2008 3:14:00 PM

0

Alle Saturday 02 February 2008, Dondi ha scritto:
> I am trying to parse a string and extract all vowels and consonants
> into two separate substrings. However, I can't get my solution to
> work. Any pointers are appreciated. Here is the approach I am using:
>
> 1) Extended the String Class with the following methods:
>
> class String
> def vowels
> self.scan(/[aeiou]|(?![aeiou])y(?![aeiou])/i)
> end
>
> def consonants
> self.scan(/![aeiou]|(?=[aeiou])y(?=[aeiou])/i)
> end
> end
>
> 2) Invoke the methods:
>
> test_paragraph = "Mary had a little lamb"
> @vowel_sub_str = test_paragraph.vowels
> @consonant_sub_str = test_paragraph.consonants
>
> However, the result is just two empty strings. I believe the problem
> is in the regular expression, but I can't figure out just where. Any
> ideas/pointers are appreciated.
>
> Thanks.

A simple (but I fear not too efficient) way is:

class String

def vowels
gsub(/[^aeiou]/, '')
end

def consonants
gsub(/[aeiou]/, '')
end

end

Stefano


Robert Dober

2/2/2008 5:39:00 PM

0

>
> A simple (but I fear not too efficient) way is:
Not sure if OP excluded non letters from the input
>
> class String
>
> def vowels
> gsub(/[^aeiou]/, '')
> end
>
> def consonants
> gsub(/[aeiou]/, '')
> end
>
> end
>
> Stefano
>
>
>
This shall do it in one run, quite complicated code, interested if
something more elegant can be found.
class String
def v_c
each_char.inject(["",""]){ |r, c|
case c
when /[aeiouy]/i
[r.first << c, r.last ]
when /[^a-z]/i
r
else
[r.first, r.last << c ]
end
}
end
end

HTH
Robert

--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Corey Haines

2/2/2008 6:28:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Elegant? I don't know, but I kind of like it. :)

"corey".split(//).partition { |x| "aeiouAEIOU"[x] }

returns

=> [["o", "e"], ["c", "r", "y"]]

irb(main):006:0> "corey haines".split(//).partition { |x| "aeiouAEIOU"[x] }
=> [["o", "e", "a", "i", "e"], ["c", "r", "y", " ", "h", "n", "s"]]

you might want to skip spaces


-Corey



On Feb 2, 2008 12:38 PM, Robert Dober <robert.dober@gmail.com> wrote:

> >
> > A simple (but I fear not too efficient) way is:
> Not sure if OP excluded non letters from the input
> >
> > class String
> >
> > def vowels
> > gsub(/[^aeiou]/, '')
> > end
> >
> > def consonants
> > gsub(/[aeiou]/, '')
> > end
> >
> > end
> >
> > Stefano
> >
> >
> >
> This shall do it in one run, quite complicated code, interested if
> something more elegant can be found.
> class String
> def v_c
> each_char.inject(["",""]){ |r, c|
> case c
> when /[aeiouy]/i
> [r.first << c, r.last ]
> when /[^a-z]/i
> r
> else
> [r.first, r.last << c ]
> end
> }
> end
> end
>
> HTH
> Robert
>
> --
> http://ruby-smalltalk.blo...
>
> ---
> Whereof one cannot speak, thereof one must be silent.
> Ludwig Wittgenstein
>
>


--
http://www.corey...
The Internet's Premiere source of information about Corey Haines

Greg P

2/2/2008 7:57:00 PM

0

As in Dondi, Michele?

--
Gerry Ford

"The apple was really a peach."
-- Allison Dunn on the garden of eden
"Dondi" <Donovan.Dillon@gmail.com> wrote in message
news:20aa4fb8-85e2-4e81-8bb5-2ab5c604398d@p69g2000hsa.googlegroups.com...
>I am trying to parse a string and extract all vowels and consonants
> into two separate substrings. However, I can't get my solution to
> work. Any pointers are appreciated. Here is the approach I am using:
>
> 1) Extended the String Class with the following methods:
>
> class String
> def vowels
> self.scan(/[aeiou]|(?![aeiou])y(?![aeiou])/i)
> end
>
> def consonants
> self.scan(/![aeiou]|(?=[aeiou])y(?=[aeiou])/i)
> end
> end
>
> 2) Invoke the methods:
>
> test_paragraph = "Mary had a little lamb"
> @vowel_sub_str = test_paragraph.vowels
> @consonant_sub_str = test_paragraph.consonants
>
> However, the result is just two empty strings. I believe the problem
> is in the regular expression, but I can't figure out just where. Any
> ideas/pointers are appreciated.
>
> Thanks.


Robert Dober

2/2/2008 10:05:00 PM

0

On Feb 2, 2008 7:27 PM, Corey Haines <coreyhaines@gmail.com> wrote:
> Elegant? I don't know, but I kind of like it. :)
>
> "corey".split(//).partition { |x| "aeiouAEIOU"[x] }
>
> returns
>
> => [["o", "e"], ["c", "r", "y"]]
>
> irb(main):006:0> "corey haines".split(//).partition { |x| "aeiouAEIOU"[x] }
> => [["o", "e", "a", "i", "e"], ["c", "r", "y", " ", "h", "n", "s"]]
>
> you might want to skip spaces
>
>
> -Corey
Excellent Corey, allow me to make it fit *any* string, I still have
not seen any statement that we are treating letters only ;)

"cor4ey ha2ines".scan(/[a-z]/i).join.each_char.partition { |x| "aeiouAEIOU"[x] }

Robert
---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Robert Dober

2/2/2008 10:08:00 PM

0

>
> "cor4ey ha2ines".scan(/[a-z]/i).join.each_char.partition { |x| "aeiouAEIOU"[x] }
and some golfing

{ |x| /[aeiou]/i =~ x }

R.

Corey Haines

2/2/2008 10:11:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Sweet! I like pulling the numbers out, but I get this in irb

irb(main):001:0> "cor4ey ha2ines".scan(/[a-z]/i).join.each_char.partition {
|x| "aeiouAEIOU"[x] }
NoMethodError: undefined method `each_char' for "coreyhaines":String

Why not use split(//) instead of each_char?


On Feb 2, 2008 5:04 PM, Robert Dober <robert.dober@gmail.com> wrote:

> On Feb 2, 2008 7:27 PM, Corey Haines <coreyhaines@gmail.com> wrote:
> > Elegant? I don't know, but I kind of like it. :)
> >
> > "corey".split(//).partition { |x| "aeiouAEIOU"[x] }
> >
> > returns
> >
> > => [["o", "e"], ["c", "r", "y"]]
> >
> > irb(main):006:0> "corey haines".split(//).partition { |x|
> "aeiouAEIOU"[x] }
> > => [["o", "e", "a", "i", "e"], ["c", "r", "y", " ", "h", "n", "s"]]
> >
> > you might want to skip spaces
> >
> >
> > -Corey
> Excellent Corey, allow me to make it fit *any* string, I still have
> not seen any statement that we are treating letters only ;)
>
> "cor4ey ha2ines".scan(/[a-z]/i).join.each_char.partition { |x|
> "aeiouAEIOU"[x] }
>
> Robert
> ---
> Whereof one cannot speak, thereof one must be silent.
> Ludwig Wittgenstein
>
>


--
http://www.corey...
The Internet's Premiere source of information about Corey Haines

Corey Haines

2/2/2008 10:12:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

:) Cool. I wasn't sure exactly how to do that. Now I do! And, in the words
of one of my personal favorite philosophers, "Knowing is half the battle."

On Feb 2, 2008 5:07 PM, Robert Dober <robert.dober@gmail.com> wrote:

> >
> > "cor4ey ha2ines".scan(/[a-z]/i).join.each_char.partition { |x|
> "aeiouAEIOU"[x] }
> and some golfing
>
> { |x| /[aeiou]/i =~ x }
>
> R.
>
>


--
http://www.corey...
The Internet's Premiere source of information about Corey Haines

Robert Dober

2/2/2008 10:17:00 PM

0

Oh sorry using 1.9 excluseviley now...
Robert