Class: Bayes::TokenList

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(charset = nil) ⇒ TokenList

Returns a new instance of TokenList.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bayes.rb', line 47

def initialize(charset=nil)
  unless charset
    charset =
      case $KCODE
      when /^e/i
        CHARSET::EUC
      else
        CHARSET::UTF8
      end
  end
  @charset = charset
end

Instance Attribute Details

#charsetObject (readonly)

Returns the value of attribute charset.



45
46
47
# File 'lib/bayes.rb', line 45

def charset
  @charset
end

Instance Method Details

#_concatObject



60
# File 'lib/bayes.rb', line 60

alias _concat concat

#_pushObject



69
# File 'lib/bayes.rb', line 69

alias _push push

#add_host(host, prefix = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bayes.rb', line 78

def add_host(host, prefix=nil)
  if /^(?:\d{1,3}\.){3}\d{1,3}$/ =~ host
    while host.size>0
      push(host, prefix)
      host = host[/^(.*?)\.?\d+$/, 1]
    end
  else
    push(host, prefix)

    h = host
    while /^(.*?)[-_.](.*)$/=~h
      h = $2
      push($1, prefix)
      push(h, prefix)
    end
  end
  self
end

#add_mail_addr(addr, prefix = nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/bayes.rb', line 123

def add_mail_addr(addr, prefix=nil)
  push(addr, prefix)

  name, host = addr.split(/@/)
  return self if (name||"").empty?
  host ||= ""
  push(name, prefix)
  add_host(host, prefix)
  self
end

#add_message(message, prefix = nil) ⇒ Object



118
119
120
121
# File 'lib/bayes.rb', line 118

def add_message(message, prefix=nil)
  concat(message.scan(@charset::RE_MESSAGE_TOKEN), prefix)
  self
end

#add_url(url, prefix = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bayes.rb', line 97

def add_url(url, prefix=nil)
  if %r[^(?:https?|ftp)://(.*?)(?::\d+)?/(.*?)\/?(\?.*)?$] =~ url
    host, path = $1, $2

    add_host(host, prefix)

    if path.size>0
      push(path, prefix)

      p = path
      re = %r[^(.*)[-_./](.*?)$]
      while re=~p
        p = $1
        push($2, prefix)
        push(p, prefix)
      end
    end
  end
  self
end

#concat(array, prefix = nil) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/bayes.rb', line 61

def concat(array, prefix=nil)
  if prefix
    _concat(array.map{|i| "#{prefix} #{i.to_s}"})
  else
    _concat(array)
  end
end

#push(item, prefix = nil) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/bayes.rb', line 70

def push(item, prefix=nil)
  if prefix
    _push("#{prefix} #{item.to_s}")
  else
    _push(item)
  end
end