[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Search string for occurneces of words stored in array

John Butler

4/30/2008 2:01:00 PM

Hi,

I have a sentence "This is my test sentence" and an array["is", "the",
"my"] and what i need to do is find the occurence of any of thearray
words in the sentence.

I have this working in a loop but i was wondering is there a way to do
it using one of rubys string methods.

Its sililar to the include method but searching for multiple words not
just one.

"This is my test sentence".include?("This") returns true

but i want something like

"This is my test sentence".include?("This", "is", "my")

anyone got a nice way to do this? I only need to find if one of the
words occure and then i exit.

JB
--
Posted via http://www.ruby-....

14 Answers

Phillip Gawlowski

4/30/2008 2:09:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

John Butler wrote:
| Hi,
|
| I have a sentence "This is my test sentence" and an array["is", "the",
| "my"] and what i need to do is find the occurence of any of thearray
| words in the sentence.
|
| I have this working in a loop but i was wondering is there a way to do
| it using one of rubys string methods.
|
| Its sililar to the include method but searching for multiple words not
| just one.
|
| "This is my test sentence".include?("This") returns true
|
| but i want something like
|
| "This is my test sentence".include?("This", "is", "my")
|
| anyone got a nice way to do this? I only need to find if one of the
| words occure and then i exit.
|
| JB

How about '["is", "the", "my"].each'?

I.e.:

["is", "the", "my"].each do |word|
~ break if "the test sentence'.include? word
end

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.bl...

~ - You know you've been hacking too long when...
...you dream that your SO and yourself are icons in a GUI and you can't
get close to each other because the window manager demands minimum space
between icons...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkgYfXsACgkQbtAgaoJTgL8swgCfW6ixKWKPo2HT8CQzGFeDcaNu
w6sAnRTk5hihGfh0hZMRBiCiOHEceZpA
=JpPT
-----END PGP SIGNATURE-----

David A. Black

4/30/2008 2:15:00 PM

0

Hi --

On Wed, 30 Apr 2008, John Butler wrote:

> Hi,
>
> I have a sentence "This is my test sentence" and an array["is", "the",
> "my"] and what i need to do is find the occurence of any of thearray
> words in the sentence.
>
> I have this working in a loop but i was wondering is there a way to do
> it using one of rubys string methods.
>
> Its sililar to the include method but searching for multiple words not
> just one.
>
> "This is my test sentence".include?("This") returns true
>
> but i want something like
>
> "This is my test sentence".include?("This", "is", "my")
>
> anyone got a nice way to do this? I only need to find if one of the
> words occure and then i exit.


You could use any?

irb(main):001:0> words = %w{ This is my }
=> ["This", "is", "my"]
irb(main):002:0> sentence = "This is my test sentence"
=> "This is my test sentence"
irb(main):003:0> words.any? {|word| sentence.include?(word) }
=> true
irb(main):004:0> sentence = "Hi"
=> "Hi"
irb(main):005:0> words.any? {|word| sentence.include?(word) }
=> false

Another possibility:

irb(main):009:0> sentence = "This is my test sentence"
=> "This is my test sentence"
irb(main):010:0> re = Regexp.new(words.join('|'))
=> /This|is|my/
irb(main):011:0> sentence =~ re
=> 0


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Jens Wille

4/30/2008 2:18:00 PM

0

Phillip Gawlowski [2008-04-30 16:09]:
> John Butler wrote:
> | Hi,
> |
> | I have a sentence "This is my test sentence" and an array["is", "the",
> | "my"] and what i need to do is find the occurence of any of thearray
> | words in the sentence.
> |
> | I have this working in a loop but i was wondering is there a way to do
> | it using one of rubys string methods.
> |
> | Its sililar to the include method but searching for multiple words not
> | just one.
> |
> | "This is my test sentence".include?("This") returns true
> |
> | but i want something like
> |
> | "This is my test sentence".include?("This", "is", "my")
> |
> | anyone got a nice way to do this? I only need to find if one of the
> | words occure and then i exit.
> |
> | JB
>
> How about '["is", "the", "my"].each'?
>
> I.e.:
>
> ["is", "the", "my"].each do |word|
> ~ break if "the test sentence'.include? word
> end
i'd prefer Enumerable#any?:

sentence, words = "This is my test sentence", ["This", "is", "my"]
words.any? { |word| sentence.include?(word) }

or Regexp:

sentence =~ Regexp.union(*words)

cheers
jens

--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
Kunsthistorisches Institut der Universität zu Köln
Albertus-Magnus-Platz, D-50923 Köln
Tel.: +49 (0)221 470-6668, E-Mail: jens.wille@uni-koeln.de
http://www.prometheus-bild...

Jens Wille

4/30/2008 2:26:00 PM

0

ok, i withdraw my post. david's just quicker... ;-)

Jens Wille [2008-04-30 16:18]:
> sentence =~ Regexp.union(*words)
one addition regarding the regexp, though. in case words may contain
special characters, it's safer to escape them first:

sentence =~ Regexp.union(*words.map { |word| Regexp.escape(word) })

cheers
jens

Ken Bloom

4/30/2008 2:27:00 PM

0

On Wed, 30 Apr 2008 09:01:11 -0500, John Butler wrote:

> Hi,
>
> I have a sentence "This is my test sentence" and an array["is", "the",
> "my"] and what i need to do is find the occurence of any of thearray
> words in the sentence.
>
> I have this working in a loop but i was wondering is there a way to do
> it using one of rubys string methods.
>
> Its sililar to the include method but searching for multiple words not
> just one.
>
> "This is my test sentence".include?("This") returns true
>
> but i want something like
>
> "This is my test sentence".include?("This", "is", "my")
>
> anyone got a nice way to do this? I only need to find if one of the
> words occure and then i exit.
>
> JB

Ruby quiz #103: the DictionaryMatcher
http://www.rubyquiz.com/qu...

You may need to do "This is my test sentence".split.any?{...} if it has
to specifically be on words. Note that
"I am running home".include? "run"
returns true, as does "abc def".include? "c d"

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

David A. Black

4/30/2008 2:29:00 PM

0

Hi --

On Wed, 30 Apr 2008, Jens Wille wrote:

> ok, i withdraw my post. david's just quicker... ;-)

Yeah, but yours is cooler because you remembered Regexp.union :-)

> Jens Wille [2008-04-30 16:18]:
>> sentence =~ Regexp.union(*words)
> one addition regarding the regexp, though. in case words may contain
> special characters, it's safer to escape them first:
>
> sentence =~ Regexp.union(*words.map { |word| Regexp.escape(word) })

It actually does it for you:

Regexp.union("a",".b")
=> /a|\.b/


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Jens Wille

4/30/2008 2:34:00 PM

0

David A. Black [2008-04-30 16:29]:
>> Jens Wille [2008-04-30 16:18]:
>>> sentence =~ Regexp.union(*words)
>> one addition regarding the regexp, though. in case words may
>> contain special characters, it's safer to escape them first:
>>
>> sentence =~ Regexp.union(*words.map { |word| Regexp.escape(word) })
> It actually does it for you:
>
> Regexp.union("a",".b") => /a|\.b/
ha, didn't know that ;-) thank you!

Roger Pack

4/30/2008 3:05:00 PM

0

I'd write my own
class String
def includes_all? array
# stuff
end
end
> "This is my test sentence".includes_all?("This", "is", "my")
--
Posted via http://www.ruby-....

Robert Klemme

4/30/2008 9:30:00 PM

0

On 30.04.2008 16:18, Jens Wille wrote:
> Phillip Gawlowski [2008-04-30 16:09]:
>> John Butler wrote:
>> | Hi,
>> |
>> | I have a sentence "This is my test sentence" and an array["is", "the",
>> | "my"] and what i need to do is find the occurence of any of thearray
>> | words in the sentence.
>> |
>> | I have this working in a loop but i was wondering is there a way to do
>> | it using one of rubys string methods.
>> |
>> | Its sililar to the include method but searching for multiple words not
>> | just one.
>> |
>> | "This is my test sentence".include?("This") returns true
>> |
>> | but i want something like
>> |
>> | "This is my test sentence".include?("This", "is", "my")
>> |
>> | anyone got a nice way to do this? I only need to find if one of the
>> | words occure and then i exit.
>> |
>> | JB
>>
>> How about '["is", "the", "my"].each'?
>>
>> I.e.:
>>
>> ["is", "the", "my"].each do |word|
>> ~ break if "the test sentence'.include? word
>> end
> i'd prefer Enumerable#any?:
>
> sentence, words = "This is my test sentence", ["This", "is", "my"]
> words.any? { |word| sentence.include?(word) }

I'd rather do it the other way round, i.e. iterate over the sentence and
test words since the sentence is potentially longer:

irb(main):001:0> require 'enumerator'
=> true
irb(main):002:0> require 'set'
=> true
irb(main):003:0> words = %w{This is my}.to_set
=> #<Set: {"my", "This", "is"}>
irb(main):004:0> "This is my test sentence".to_enum(:scan,/\w+/).any?
{|w| words.include? w}
=> true
irb(main):005:0>

Kind regards

robert

David A. Black

4/30/2008 9:40:00 PM

0

Hi --

On Thu, 1 May 2008, Robert Klemme wrote:

> On 30.04.2008 16:18, Jens Wille wrote:
>> Phillip Gawlowski [2008-04-30 16:09]:
>>> John Butler wrote:
>>> | Hi,
>>> |
>>> | I have a sentence "This is my test sentence" and an array["is", "the",
>>> | "my"] and what i need to do is find the occurence of any of thearray
>>> | words in the sentence.
>>> |
>>> | I have this working in a loop but i was wondering is there a way to do
>>> | it using one of rubys string methods.
>>> |
>>> | Its sililar to the include method but searching for multiple words not
>>> | just one.
>>> |
>>> | "This is my test sentence".include?("This") returns true
>>> |
>>> | but i want something like
>>> |
>>> | "This is my test sentence".include?("This", "is", "my")
>>> |
>>> | anyone got a nice way to do this? I only need to find if one of the
>>> | words occure and then i exit.
>>> |
>>> | JB
>>>
>>> How about '["is", "the", "my"].each'?
>>>
>>> I.e.:
>>>
>>> ["is", "the", "my"].each do |word|
>>> ~ break if "the test sentence'.include? word
>>> end
>> i'd prefer Enumerable#any?:
>>
>> sentence, words = "This is my test sentence", ["This", "is", "my"]
>> words.any? { |word| sentence.include?(word) }
>
> I'd rather do it the other way round, i.e. iterate over the sentence and test
> words since the sentence is potentially longer:
>
> irb(main):001:0> require 'enumerator'
> => true
> irb(main):002:0> require 'set'
> => true
> irb(main):003:0> words = %w{This is my}.to_set
> => #<Set: {"my", "This", "is"}>
> irb(main):004:0> "This is my test sentence".to_enum(:scan,/\w+/).any? {|w|
> words.include? w}
> => true
> irb(main):005:0>

Is there any reason not to just do:

"This is my test sentence".scan(/\w+/).any? {|w| words.include? w }


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!