Class: TactfulTokenizer::Frag

Inherits:
Object
  • Object
show all
Defined in:
lib/tactful_tokenizer.rb

Overview

A fragment is a potential sentence, but is based only on the existence of a period. The text “Here in the U.S. Senate we prefer to devour our friends.” will be split into “Here in the U.S.” and “Senate we prefer to devour our friends.”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(orig = '') ⇒ Frag

Create a new fragment.



193
194
195
196
197
# File 'lib/tactful_tokenizer.rb', line 193

def initialize(orig='')
    @orig = orig
    clean(orig)
    @next, @pred, @features = nil, nil, nil
end

Instance Attribute Details

#cleanedObject

orig = The original text of the fragment. next = The next word following the fragment. cleaned = Array of the fragment’s words after cleaning. pred = Probability that the fragment is a sentence. features = Array of the fragment’s features.



190
191
192
# File 'lib/tactful_tokenizer.rb', line 190

def cleaned
  @cleaned
end

#featuresObject

orig = The original text of the fragment. next = The next word following the fragment. cleaned = Array of the fragment’s words after cleaning. pred = Probability that the fragment is a sentence. features = Array of the fragment’s features.



190
191
192
# File 'lib/tactful_tokenizer.rb', line 190

def features
  @features
end

#nextObject

orig = The original text of the fragment. next = The next word following the fragment. cleaned = Array of the fragment’s words after cleaning. pred = Probability that the fragment is a sentence. features = Array of the fragment’s features.



190
191
192
# File 'lib/tactful_tokenizer.rb', line 190

def next
  @next
end

#origObject

orig = The original text of the fragment. next = The next word following the fragment. cleaned = Array of the fragment’s words after cleaning. pred = Probability that the fragment is a sentence. features = Array of the fragment’s features.



190
191
192
# File 'lib/tactful_tokenizer.rb', line 190

def orig
  @orig
end

#predObject

orig = The original text of the fragment. next = The next word following the fragment. cleaned = Array of the fragment’s words after cleaning. pred = Probability that the fragment is a sentence. features = Array of the fragment’s features.



190
191
192
# File 'lib/tactful_tokenizer.rb', line 190

def pred
  @pred
end

Instance Method Details

#clean(s) ⇒ Object

Normalizes numbers and discards ambiguous punctuation. And then splits into an array, because realistically only the last and first words are ever accessed.



201
202
203
204
205
206
207
208
# File 'lib/tactful_tokenizer.rb', line 201

def clean(s)
    @cleaned = String.new(s)
    tokenize(@cleaned)
    @cleaned.gsub!(/[.,\d]*\d/, '<NUM>')
    @cleaned.gsub!(/[^a-zA-Z0-9,.;:<>\-'\/$% ]/, '')
    @cleaned.gsub!('--', ' ')
    @cleaned = @cleaned.split
end