# Extract keywords from string

**URL:** <https://www.ruby-forum.com/t/extract-keywords-from-string/174263>\
**Category:** Rails\
**Created:** [August 24, 2009, 3:04am UTC](https://www.ruby-forum.com/t/extract-keywords-from-string/174263 "2009-08-24T03:04:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [August 24, 2009, 3:04am UTC](https://www.ruby-forum.com/t/extract-keywords-from-string/174263/1 "2009-08-24T03:04:58Z")

</div>

hi -

i have strings that i need to extract keywords from. the string might  
have html tags, urls, etc. i need to extract the keywords from the  
string. i imagine i’m not the first guy to have to tackle this  
problem. is there a gem i can use or anyone have any ideas how to  
approach this?

thanks,  
dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [August 24, 2009, 4:44am UTC](https://www.ruby-forum.com/t/extract-keywords-from-string/174263/2 "2009-08-24T04:44:13Z")

</div>

If you are doing html parsing, you’ll want to look into hpricot.

> **[Using Hpricot](http://juixe.com/techknow/index.php/2008/05/19/using-hpricot/)**
>
> Hpricot is a HTML parser for the Ruby programming language. With Hpricot you can scan and scape a HTML document. To illustrate how to use Hpricot i’ll write a list the code of a short script …

There are a few parsers out there, I’ve written a couple myself.

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [August 24, 2009, 4:46am UTC](https://www.ruby-forum.com/t/extract-keywords-from-string/174263/3 "2009-08-24T04:46:14Z")

</div>

Quoting dino d. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> hi -
> 
> i have strings that i need to extract keywords from. the string might  
> have html tags, urls, etc. i need to extract the keywords from the  
> string. i imagine i’m not the first guy to have to tackle this  
> problem. is there a gem i can use or anyone have any ideas how to  
> approach this?

More detail needed about the keywords. The simple case is keywords  
regardless  
of context, separated by whitespace.

KEYWORDS = %{if else then end case when do def}

str = “if true then false else true end”  
str.split.find\_all{|s| KEYWORDS.include?(s)}

irb(main):006:0\> KEYWORDS = %{if else then end case when do def}  
=\> “if else then end case when do def”  
irb(main):007:0\> str = “if true then false else true end”  
=\> “if true then false else true end”  
irb(main):008:0\> str.split.find\_all{|s| KEYWORDS.include?(s)}  
=\> [“if”, “then”, “else”, “end”]  
irb(main):009:0\>

If you need to exclude keywords inside strings, URLs, etc. the solution  
is  
more complex.

HTH,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [August 24, 2009, 7:28am UTC](https://www.ruby-forum.com/t/extract-keywords-from-string/174263/4 "2009-08-24T07:28:20Z")

</div>

On Aug 23, 2009, at 7:44 PM, Alpha B. wrote:

> If you are doing html parsing, you’ll want to look into hpricot.
> 
> [http://juixe.com/techknow/index.php/2008/05/19/using-hpricot/](http://juixe.com/techknow/index.php/2008/05/19/using-hpricot/)
> 
> ## There are a few parsers out there, I’ve written a couple myself.

Many people are leaning toward Nokogiri (read:  
[http://nokogiri.rubyforge.org/nokogiri/Nokogiri.html](http://nokogiri.rubyforge.org/nokogiri/Nokogiri.html))  
.

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [August 24, 2009, 4:53am UTC](https://www.ruby-forum.com/t/extract-keywords-from-string/174263/5 "2009-08-24T04:53:15Z")

</div>

Jeff-

thanks for the reply. i can deal with context in a different method,  
in your solution, i still grab “” and “test.” and “&wow\*&&” as  
keywords. i want to send this method a string, and get an array of  
letter-only words returned. if you have context ideas, i’d love to  
hear those too, but the first step is just harvesting only character  
words from strings.

thanks,  
dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [August 25, 2009, 4:15am UTC](https://www.ruby-forum.com/t/extract-keywords-from-string/174263/6 "2009-08-25T04:15:23Z")

</div>

On Aug 24, 12:48 am, “s.ross” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Many people are leaning toward Nokogiri (read:[http://nokogiri.rubyforge.org/nokogiri/Nokogiri.html](http://nokogiri.rubyforge.org/nokogiri/Nokogiri.html))  
> .

Agreed. With the disappearance of \_why, the future of hpricot is  
uncertain.
