Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Natural Language Processing with JRuby and OpenNLP

t6d
June 28, 2013

Natural Language Processing with JRuby and OpenNLP

t6d

June 28, 2013
Tweet

More Decks by t6d

Other Decks in Technology

Transcript

  1. Sharing Information Language Language is the most natural way to

    communicate with others. It is excellent for encoding information.
  2. Intelligent Machines Natural Language Processing With the help natural language

    processing methods, we enable machines to understand and process language.
  3. Part of Speech The part of speech or word class

    of a word denotes its syntactic function. Words can have multiple classes, e.g., ‘to fly’ (Verb) and ‘a fly’ (Noun).
  4. Word Stem The stem of a word is the part

    of the word that is common to all its derived variants. The stem of a word can be an artificial construct.
  5. JRuby Ruby is a very expressive language with excellent string

    processing capabilities. The JVM is a high performance platform with true multi-threading capabilities. Excellent java libraries for natural language processing exist.
  6. Machine Learning Based NLP Toolkit OpenNLP OpenNLP is solely based

    on machine learning methods. It uses maximum entropy classification to perform natural language processing tasks. http://opennlp.apache.org/
  7. Pre-Trained Models OpenNLP Maximum entropy classifiers have to be trained

    before they can be utilized. Pre-trained models can be downloaded from SourceForge: http://opennlp.sourceforge.net/ models-1.5/
  8. Three Steps OpenNLP 1. Load an existing model or create

    a new one from your own training data. 2. Initialize the classifier using this model as input. 3. Perform the actual classification task.
  9. The Gems OpenNLP Minimal wrapper around the original OpenNLP implementation:

    • Automatic conversion between Ruby and Java datatypes • Unified Interface Separate Gems for English and German model files.
  10. String → Sequence of Logical Units Segmentation The problem of

    segmentation is concerned with splitting a text into a sequence of logical units. Different instances of this problem exist.
  11. Text → Sentences Sentence Detection Sentence detection is the process

    of segmenting a text into sentences. The problem is harder than it looks: • Ruby is awesome. Ruby is great! • “Stop it!”, Mr. Smith shouted across the yard. He was clearly angry.
  12. Text → Sentences m = OpenNLP::English.sentence_detection_model d = OpenNLP::SentenceDetector.new(m) r

    = d.process <<-TEXT Ruby is awesome. Ruby is great! TEXT r[0] # => "Ruby is awesome." r[1] # => "Ruby is great!" Sentence Detection
  13. Sentence → Words Tokenization Tokenization is the task of detecting

    word boundaries. Challenges: • Languages with no visual representation of word boundaries • Multiple separators
  14. String → Linguistic Units m = OpenNLP::English.tokenization_model t = OpenNLP::Tokenizer.new(m)

    r = t.process("I shot an elephant in my pajamas.") r # => ["I", "shot", "an", "elephant", "in", "my", "pajamas", "."] Tokenization
  15. Tokens → Tags Part-of-Speech Tagging Part-of-Speech tagging is concerned with

    identifying a word’s class in a given context. A common format for representing Part-of-Speech tags is the Penn Treebank tag set.
  16. Tokens → Tags m = OpenNLP::English.pos_tagging_model t = OpenNLP::POSTagger.new(m) r

    = t.process(%w[Ruby is awesome]) r[0] # => NNP r[1] # => VBZ r[2] # => JJ Part-of-Speech Tagging
  17. Inflected word → Word stem Stemming Stemming is the processes

    of applying a set of rules to remove morphological suffixes. Porter’s stemmer is probably the most popular stemmer.
  18. Tokens → Names | Locations | … Named Entity Recognition

    Named entities are noun phrases that refer to individuals, organizations, locations, etc. Named Entity Recognition is concerned with identifying named entities in a given text.
  19. Tokens → Names | Locations | … tokens = %w[This

    summer EuRuKo comes to Athens for two days on the 28th and 29th of June .] m = OpenNLP::Models. named_entity_recognition_model(:location) f = OpenNLP::NameFinder.new(m) ranges = f.process(tokens) ranges.map { |r| tokens[r] } # => ["Athens"] Named Entity Recognition
  20. Properties of NLP Task NLP tasks can o en be

    expressed as a sequence of steps that is executed linearly. Hence, we can use processing pipelines to solve NLP problems.
  21. Processing Pipelines A processing pipeline is a set so ware

    components connected in series. The output of one component is the input of the next one.
  22. t6d/composable_operations Composable Operations A flexible Ruby implementation of a processing

    pipeline: • Operation represents a single processing component. • ComposedOperation represents a processing pipeline, but can also be used as a component in an other pipeline.
  23. Definition require 'composable_operations' include ComposableOperations class PreProcessing < ComposedOperation use

    SentenceDetection use Tokenization use POSTagging end Pre-Processing Pipeline
  24. Sentence Detection Component require 'opennlp' require 'opennlp-english' require 'opennlp-german' require

    'composable_operations' include ComposableOperations class SentenceDetection < Operation processes :text property :language, default: :en, converts: :to_sym, required: true, accepts: [:en, :de] def execute detector = OpenNLP::SentenceDetector.new(model) detector.process(text) end protected def model case language when :en OpenNLP::English.sentence_detection_model when :de OpenNLP::German.sentence_detection_model end end end Pre-Processing Pipeline
  25. Tokenization Component require 'opennlp' require 'opennlp-english' require 'opennlp-german' require 'composable_operations'

    include ComposableOperations class Tokenization < Operation processes :sentences property :language, default: :en, converts: :to_sym, required: true, accepts: [:en, :de] def execute tokenizer = OpenNLP::Tokenizer.new(model) Array(sentences).map do |sentence| tokenizer.process(sentence) end end protected def model # ... end end Pre-Processing Pipeline
  26. POS Tagging Component require 'opennlp' require 'opennlp-english' require 'opennlp-german' require

    'composable_operations' include ComposableOperations class POSTagging < Operation processes :sentences property :language, default: :en, converts: :to_sym, required: true, accepts: [:en, :de] def execute tagger = OpenNLP::POSTagger.new(model) sentences.map.with_index do |sent, sent_idx| tags = tagger.process(sent) tags.map.with_index do |tag, tkn_idx| [sentences[sent_idx][tkn_idx], tag] end end end protected def model # ... end end Pre-Processing Pipeline
  27. Execution PreProcessing.perform("Ruby is awesome. Ruby is great!") # Returns: #

    # [ # [ # ["Ruby", "NNP"], # ["is", "VBZ"], # ["awesome", "JJ"], # [".", "."] # ], # [ # ["Ruby", "NNP"], # ["is", "VBZ"], # ["great", "JJ"], # ["!", "."] # ] # ] Pre-Processing Pipeline
  28. TextRank TextRank is a graph-based algorithm heavily inspired by Google’s

    PageRank algorithm. The algorithm was published by Rada Mihalcea and Paul Tarau: http:// acl.ldc.upenn.edu/acl2004/emnlp/ pdf/Mihalcea.pdf
  29. Keyword Extraction Pipeline Preprocessing Sentence Detection, Tokenization, POS Tagging, Normalization

    through Stemming, Token Filtering Cooccurrence Calculation Coocurrence Graph Construction Text Rank Calculation Sorting and Extracting Nodes 1 2 3 4 5
  30. class KeywordRanking < ComposedOperation use PreProcessingPipeline, filter: [/^NN/, /^JJ/] use

    CooccurrenceCalculation use CooccurrenceGraphConstruction use PageRankCalculation use NodeSortingAndExtraction end KeywordRanking.perform(...) Keyword Extraction Pipeline
  31. The code can be found on Github: https://github.com/t6d/keyword_extractor Be nice,

    it’s just some demo code – not for use in production. ;) Code
  32. Natural Language Processing with JRuby and OpenNLP by Konstantin Tennhard

    GitHub: t6d Twitter: t6d Code can be found on GitHub: * http://github.com/t6d/opennlp * http://github.com/t6d/opennlp-english * http://github.com/t6d/opennlp-german * http://github.com/t6d/opennlp-examples * http://github.com/t6d/keyword_extractor * http://github.com/t6d/composable_operations * http://github.com/t6d/smart_properties Any questions? Feel free to approach me anytime throughout the conference or send me a tweet, if that’s what you prefer. Summary
  33. _____ _ _ _ _ _ _ ______ |_ _|

    | | | / \ | \ | | |/ / ___| | | | |_| | / _ \ | \| | ' /\___ \ | | | _ |/ ___ \| |\ | . \ ___) | |_| |_| |_/_/ \_\_| \_|_|\_\____/ Summary