Class: CharDet::Latin1Prober

Inherits:
CharSetProber show all
Defined in:
lib/rchardet/latin1prober.rb

Instance Attribute Summary

Attributes inherited from CharSetProber

#active

Instance Method Summary collapse

Methods inherited from CharSetProber

#filter_high_bit_only, #filter_with_english_letters, #filter_without_english_letters, #get_state

Constructor Details

#initializeLatin1Prober

Returns a new instance of Latin1Prober.



95
96
97
98
# File 'lib/rchardet/latin1prober.rb', line 95

def initialize
  super
  reset()
end

Instance Method Details

#feed(aBuf) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rchardet/latin1prober.rb', line 110

def feed(aBuf)
  aBuf = filter_with_english_letters(aBuf)
  aBuf.each_byte do |b|
    c = b.chr
    charClass = Latin1_CharToClass[c.bytes.first]
    freq = Latin1ClassModel[(@lastCharClass * CLASS_NUM) + charClass]
    if freq == 0
      @state = ENotMe
      break
    end
    @freqCounter[freq] += 1
    @lastCharClass = charClass
  end

  return get_state()
end

#get_charset_nameObject



106
107
108
# File 'lib/rchardet/latin1prober.rb', line 106

def get_charset_name
  return "windows-1252"
end

#get_confidenceObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rchardet/latin1prober.rb', line 127

def get_confidence
  if get_state() == ENotMe
    return 0.01
  end

  total = @freqCounter.inject{|a,b| a+b} 
  if total < 0.01
    confidence = 0.0
  else
    confidence = (@freqCounter[3] / total) - (@freqCounter[1] * 20.0 / total)
  end
  if confidence < 0.0
    confidence = 0.0
  end
  # lower the confidence of latin1 so that other more accurate detector 
  # can take priority.
  confidence = confidence * 0.5
  return confidence
end

#resetObject



100
101
102
103
104
# File 'lib/rchardet/latin1prober.rb', line 100

def reset
  @lastCharClass = OTH
  @freqCounter = [0] * FREQ_CAT_NUM
  super
end