/keyword_prospector

Search for keywords in text and create hyperlinks

Primary LanguageRubyMIT LicenseMIT

keyword_prospector

Source code:

RDoc:

DESCRIPTION:

KeywordProspector was built to provide contextual linking in Ruby applications. It can be used to link keywords with associated URL’s in text, and has very fast performance even for very high numbers of keyword/URL pairs. Use the KeywordLinker object for linking keywords in text.

KeywordProspector can also be used to associate keywords in text with arbitrary output objects, so it has been used for associating geocode objects with neighborhood names to determine map locations to associate with text, and could even be used to implement profanity filters or basic keyword-based text classifiers. Use the KeywordProspector object for associating arbitrary objects with keywords.

KeywordProspector has an implementation of the Aho-Corasick algorithm as its core engine, which enables it to match keywords in O(n) time, where n is the length of the text being matched, regardless of the number of keywords being matched.

FEATURES:

The core KeywordProspector engine has the following properties:

  • Once a tree is built, matching against the tree is O(n) where n is the length of your text, regardless of how mane keywords you have in the tree.

  • Arbitrary output objects can be associated with any keyword or set of keywords.

KeywordLinker can be used to create links to designated url’s:

  • You can specify a single keyword to associate with a url.

  • You can specify an array of keywords to associate with a url.

  • Each keyword or group of keywords will be linked only once to the url provided.

  • Hyperlinks are not created within existing hyperlinks.

  • Hyperlinks are not generated anywhere they would be illegal in HTML, such as within attribute values.

USAGE:

Note: Because I want my README file to work on github, I’ve replaced all the less than and greater than signs with ‘[’ and ‘]’ respectively. KeywordLinker does produce valid HTML.

Use KeywordLinker to create links in HTML text. KeywordLinker will link only the first alternative that appears in text:

require 'keyword_linker'

linker = KeywordLinker.new
linker.add_url("http://www.latimes.com", ["L.A. Times", "Los Angeles Times"])
linker.link_text("'L.A. Times' or 'Los Angeles Times'?")

OUTPUT: "'[a href=\"http://www.latimes.com\"]L.A. Times[/a]' or 'Los Angeles Times'?"

linker.link_text("'Los Angeles Times' or 'L.A. Times'?")

OUTPUT: "'[a href=\"http://www.latimes.com\"]Los Angeles Times[/a]' or 'L.A. Times'?"

You can provide html options when adding url’s:

linker = KeywordLinker.new
linker.add_url("http://www.latimes.com", "Los Angeles Times",
                :title => "Visit the Los Angeles Times")
linker.add_url("http://www.google.com", ["Google", "The Google"],
                :title => "Go check it out on The Google!")

linker.link_text("Do you prefer The Google or the Los Angeles Times?")

OUTPUT: "Do you prefer [a href=\"http://www.google.com\" title=\"Go check it out on The Google!\"]The Google[/a] or the [a href=\"http://www.latimes.com\" title=\"Visit the Los Angeles Times\"]Los Angeles Times[/a]?"

DEPENDENCIES:

  • Hpricot

INSTALL:

sudo gem install keyword_prospector

AUTHOR:

Alf Mikula [amikula@gmail.com]

ACKNOWLEDGEMENTS:

Thanks to David Johnson for telling me about the Aho-Corasick algorithm, and for providing the original Aho-Corasick Ruby implementation.

LICENSE:

(The MIT License)

Copyright © 2008 Los Angeles Times

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.