Class: CharDet::UniversalDetector

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUniversalDetector

Returns a new instance of UniversalDetector.



40
41
42
43
44
45
46
# File 'lib/rchardet/universaldetector.rb', line 40

def initialize
  @highBitDetector = /[\x80-\xFF]/n
  @escDetector = /(\033|\~\{)/n
  @escCharSetProber = nil
  @charSetProbers = []
  reset()
end

Instance Attribute Details

#doneObject (readonly)

Returns the value of attribute done.



38
39
40
# File 'lib/rchardet/universaldetector.rb', line 38

def done
  @done
end

#resultObject (readonly)

Returns the value of attribute result.



38
39
40
# File 'lib/rchardet/universaldetector.rb', line 38

def result
  @result
end

Instance Method Details

#closeObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rchardet/universaldetector.rb', line 135

def close
  return if @done
  if !@gotData
    $stderr << "no data received!\n" if $debug
    return
  end
  @done = true

  if @inputState == EPureAscii
    @result = {'encoding' => 'ascii', 'confidence' => 1.0}
    return @result
  end

  if @inputState == EHighbyte
    confidences = {}
    @charSetProbers.each{ |prober| confidences[prober] = prober.get_confidence }
    maxProber = @charSetProbers.max{ |a,b| confidences[a] <=> confidences[b] }
    if maxProber and maxProber.get_confidence > MINIMUM_THRESHOLD
      @result = {'encoding' =>  maxProber.get_charset_name(),
        'confidence' =>  maxProber.get_confidence()}
      return @result
    end
  end

  if $debug
    $stderr << "no probers hit minimum threshhold\n" if $debug
    for prober in @charSetProbers[0].probers
      next if !prober
      $stderr << "#{prober.get_charset_name} confidence = #{prober.get_confidence}\n" if $debug
    end
  end
end

#feed(aBuf) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rchardet/universaldetector.rb', line 63

def feed(aBuf)
  return if @done

  aLen = aBuf.length
  return if aLen == 0

  if !@gotData
    # If the data starts with BOM, we know it is UTF
    if aBuf[0, 3] == "\xEF\xBB\xBF"
      # EF BB BF  UTF-8 with BOM
      @result = {'encoding' => "UTF-8", 'confidence' => 1.0}
    elsif aBuf[0, 4] == "\xFF\xFE\x00\x00"
      # FF FE 00 00  UTF-32, little-endian BOM
      @result = {'encoding' => "UTF-32LE", 'confidence' => 1.0}
    elsif aBuf[0, 4] == "\x00\x00\xFE\xFF"
      # 00 00 FE FF  UTF-32, big-endian BOM
      @result = {'encoding' => "UTF-32BE", 'confidence' => 1.0}
    elsif aBuf[0, 4] == "\xFE\xFF\x00\x00"
      # FE FF 00 00  UCS-4, unusual octet order BOM (3412)
      @result = {'encoding' => "X-ISO-10646-UCS-4-3412", 'confidence' => 1.0}
    elsif aBuf[0, 4] == "\x00\x00\xFF\xFE"
      # 00 00 FF FE  UCS-4, unusual octet order BOM (2143)
      @result = {'encoding' =>  "X-ISO-10646-UCS-4-2143", 'confidence' =>  1.0}
    elsif aBuf[0, 2] == "\xFF\xFE"
      # FF FE  UTF-16, little endian BOM
      @result = {'encoding' =>  "UTF-16LE", 'confidence' =>  1.0}
    elsif aBuf[0, 2] == "\xFE\xFF"
      # FE FF  UTF-16, big endian BOM
      @result = {'encoding' =>  "UTF-16BE", 'confidence' =>  1.0}
    end
  end

  @gotData = true
  if @result['encoding'] and (@result['confidence'] > 0.0)
    @done = true
    return
  end
  if @inputState == EPureAscii
    if @highBitDetector =~ (aBuf)
      @inputState = EHighbyte
    elsif (@inputState == EPureAscii) and @escDetector =~ (@lastChar + aBuf)
      @inputState = EEscAscii
    end
  end

  @lastChar = aBuf[-1, 1]
  if @inputState == EEscAscii
    if !@escCharSetProber
      @escCharSetProber = EscCharSetProber.new()
    end
    if @escCharSetProber.feed(aBuf) == EFoundIt
      @result = {'encoding' =>  @escCharSetProber.get_charset_name(),
        'confidence' =>  @escCharSetProber.get_confidence()
      }
      @done = true
    end
  elsif @inputState == EHighbyte
    if @charSetProbers.nil? || @charSetProbers.empty?
      @charSetProbers = [MBCSGroupProber.new(), SBCSGroupProber.new(), Latin1Prober.new()]
    end
    for prober in @charSetProbers
      if prober.feed(aBuf) == EFoundIt
        @result = {'encoding' =>  prober.get_charset_name(),
          'confidence' =>  prober.get_confidence()}
        @done = true
        break
      end
    end
  end

end

#resetObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rchardet/universaldetector.rb', line 48

def reset
  @result = {'encoding' => nil, 'confidence' => 0.0}
  @done = false
  @start = true
  @gotData = false
  @inputState = EPureAscii
  @lastChar = ''
  if @escCharSetProber
    @escCharSetProber.reset()
  end
  for prober in @charSetProbers
    prober.reset()
  end
end