diasks2 / pragmatic_tokenizer

A multilingual tokenizer to split a string into tokens
MIT License
90 stars 11 forks source link

Pragmatic Tokenizer

Gem Version Build Status License

Pragmatic Tokenizer is a multilingual tokenizer to split a string into tokens.

Installation

Add this line to your application's Gemfile:

Ruby

gem install pragmatic_tokenizer

Ruby on Rails
Add this line to your application’s Gemfile:

gem 'pragmatic_tokenizer'

Usage

Example Usage

text = "\"I said, 'what're you? Crazy?'\" said Sandowsky. \"I can't afford to do that.\""

PragmaticTokenizer::Tokenizer.new.tokenize(text)
# => ["\"", "i", "said", ",", "'", "what're", "you", "?", "crazy", "?", "'", "\"", "said", "sandowsky", ".", "\"", "i", "can't", "afford", "to", "do", "that", ".", "\""]

# You can pass many different options to #initialize:
options = {
  language:            :en, # the language of the string you are tokenizing
  abbreviations:       ['a.b', 'a'], # a user-supplied array of abbreviations (downcased with ending period removed)
  stop_words:          ['is', 'the'], # a user-supplied array of stop words (downcased)
  remove_stop_words:   true, # remove stop words
  contractions:        { "i'm" => "i am" }, # a user-supplied hash of contractions (key is the contracted form; value is the expanded form - both the key and value should be downcased)
  expand_contractions: true, # (i.e. ["isn't"] will change to two tokens ["is", "not"])
  filter_languages:    [:en, :de], # process abbreviations, contractions and stop words for this array of languages
  punctuation:         :none, # see below for more details
  numbers:             :none, # see below for more details
  remove_emoji:        :true, # remove any emoji tokens
  remove_urls:         :true, # remove any urls
  remove_emails:       :true, # remove any emails
  remove_domains:      :true, # remove any domains
  hashtags:            :keep_and_clean, # remove the hastag prefix
  mentions:            :keep_and_clean, # remove the @ prefix
  clean:               true, # remove some special characters
  classic_filter:      true, # removes dots from acronyms and 's from the end of tokens
  downcase:            false, # do not downcase tokens
  minimum_length:      3, # remove any tokens less than 3 characters
  long_word_split:     10 # split tokens longer than 10 characters at hypens or underscores
}

Options

language

default = 'en'


abbreviations

default = nil


stop_words

default = nil


contractions

default = nil


remove_stop_words

default = false


expand_contractions

default = false


filter_languages

default = nil


punctuation

default = 'all'


numbers

default = 'all'


remove_emoji

default = false


remove_urls

default = false


remove_domains

default = false


remove_domains

default = false


clean

default = false


hashtags

default = :keep_original


mentions

default = :keep_original


classic_filter

default = false


downcase

default = true


minimum_length

default = 0
The minimum number of characters a token should be.


long_word_split

default = nil
The number of characters after which a token should be split at hypens or underscores.

Language Support

The following lists the current level of support for different languages. Pull requests or help for any languages that are not fully supported would be greatly appreciated.

N.B. - contractions might not be applicable for all languages below - in that case the CONTRACTIONS hash should stay empty.

English

Specs: Yes
Abbreviations: Yes
Stop Words: Yes
Contractions: Yes

Arabic

Specs: No
Abbreviations: Yes
Stop Words: Yes
Contractions: No

Bulgarian

Specs: More needed
Abbreviations: Yes
Stop Words: Yes
Contractions: No

Catalan

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Czech

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Danish

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Deutsch

Specs: More needed
Abbreviations: Yes
Stop Words: Yes
Contractions: No

Finnish

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

French

Specs: More needed
Abbreviations: Yes
Stop Words: Yes
Contractions: No

Greek

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Indonesian

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Italian

Specs: No
Abbreviations: Yes
Stop Words: Yes
Contractions: No

Latvian

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Norwegian

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Persian

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Polish

Specs: No
Abbreviations: Yes
Stop Words: Yes
Contractions: No

Portuguese

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Romanian

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Russian

Specs: No
Abbreviations: Yes
Stop Words: Yes
Contractions: No

Slovak

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Spanish

Specs: No
Abbreviations: Yes
Stop Words: Yes
Contractions: Yes

Swedish

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Turkish

Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No

Resources

Contributing

  1. Fork it ( https://github.com/diasks2/pragmatic_tokenizer/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

The MIT License (MIT)

Copyright (c) 2016 Kevin S. Dias

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.