summaryrefslogtreecommitdiff
path: root/sources/wiki/extract_words.rb
blob: c1c5279aaff88c25561e35da2bdff8578b411c05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env ruby
#------------------------------------------------------------------------------
#
#  extract_words.rb [DIR]
#
#------------------------------------------------------------------------------
#
#  Extracts words from wiki pages into their own table for full-text search.
#
#------------------------------------------------------------------------------
#
#  Copyright (C) 2013  Jochen Topf <jochen@remote.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#------------------------------------------------------------------------------

require 'sqlite3'

#------------------------------------------------------------------------------

class Words

    def initialize
        @words = Hash.new
    end

    def add(key, value, lang, word)
        entry = [key, value]
        if @words[word]
            @words[word] << entry
        else
            @words[word] = [entry]
        end
    end

    # Remove words that appear too often
    def cleanup
        @words.delete_if do |k, v|
            v.size >= 10
        end
    end

    def invert
        @kvw = []
        @words.each do |word, entries|
            entries.each do |entry|
                key = entry[0]
                value = entry[1] || ''
                @kvw << [key, value, word]
            end
        end
    end

    def dump
        lastkey = ''
        lastvalue = ''
        words = []
        @kvw.sort.uniq.each do |key, value, word|
            if key != lastkey || value != lastvalue
                yield lastkey, lastvalue, words.join(',')
                words = []
                lastkey = key
                lastvalue = value
            else
                words << word
            end
        end
        yield lastkey, lastvalue, words.join(',')
    end

end

#------------------------------------------------------------------------------

class WordExtractor

    def initialize(words)
        @words = words
    end

    def interested_in(word, key, value, lang)
        # not interested in very short words
        return false if word.size <= 2

        # digits make for bad words
        return false if word =~ /\d/

#        # not interested if word == key or == value
#        key.downcase!
#        value.downcase! unless value.nil?
#        return false if word == key || word == value

        return true
    end

    def parse(key, value, lang, text)
        words = text.scan(/\w+/).sort.uniq
        words.each do |word|
            word.downcase!
            if interested_in(word, key, value, lang)
                @words.add(key, value, lang, word)
            end
        end
    end

end

#------------------------------------------------------------------------------

dir = ARGV[0] || '.'
db = SQLite3::Database.new(dir + '/taginfo-wiki.db')
db.results_as_hash = true

#------------------------------------------------------------------------------

words = Words.new
we = WordExtractor.new(words)

db.execute("SELECT * FROM wikipages") do |row|
#    puts "key=#{ row['key'] } value=#{ row['value'] } lang=#{ row['lang'] }"
    we.parse(row['key'], row['value'], row['lang'], row['body'])
end

words.cleanup
words.invert

#words.dump do |key, value, words|
#    puts "#{key}=#{value}: #{words}"
#end

db.transaction do |db|
    words.dump do |key, value, words|
        db.execute('INSERT INTO words (key, value, words) VALUES (?, ?, ?)', key, value, words)
    end
end


#-- THE END -------------------------------------------------------------------