Pragmatic Tokenizer is a multilingual tokenizer to split a string into tokens.
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'
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'
:en
) or string (i.e. 'en'
)abbreviations
default = nil
stop_words
default = nil
contractions
default = nil
remove_stop_words
default = false
true
false
expand_contractions
default = false
true
false
filter_languages
default = nil
[:en, :de]
or ['en', 'de']
)punctuation
default = 'all'
:all
:semi
:none
:only
numbers
default = 'all'
:all
:semi
:none
:only
remove_emoji
default = false
true
false
remove_urls
default = false
true
false
remove_domains
default = false
true
false
remove_domains
default = false
true
false
clean
default = false
true
false
hashtags
default = :keep_original
:keep_original
:keep_and_clean
:remove
mentions
default = :keep_original
:keep_original
:keep_and_clean
:remove
classic_filter
default = false
true
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.
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.
Specs: Yes
Abbreviations: Yes
Stop Words: Yes
Contractions: Yes
Specs: No
Abbreviations: Yes
Stop Words: Yes
Contractions: No
Specs: More needed
Abbreviations: Yes
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: More needed
Abbreviations: Yes
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: More needed
Abbreviations: Yes
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: Yes
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: Yes
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: Yes
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: Yes
Stop Words: Yes
Contractions: Yes
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
Specs: No
Abbreviations: No
Stop Words: Yes
Contractions: No
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)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.