#! /usr/bin/ruby #================================================================ # relwords.cgi # CGI script to dump related words #================================================================ # library requirement require "cgi" require "villa" # constants DBNAME = "relwords.qdb" # get parameters scriptname = ENV["SCRIPT_NAME"] scriptname = $0 unless scriptname scriptname = scriptname.gsub(/.*\//, "") cgi = CGI::new word = cgi["word"] word.downcase! word.strip! # show HTTP headers printf("Content-Type: text/html; charset=UTF-8\r\n\r\n") # show page header printf("%s", <<__EOF Related Word Extractor
__EOF ); # show input form escword = CGI::escapeHTML(word) printf("%s", <<__EOF
Word:

__EOF ); # show result if word.length > 0 sum = 0 allkwd = {} begin db = Villa::new(DBNAME) terms = word.split(/[ \t]/) terms.each do |term| next if term.length < 1 begin line = db.get(term) rescue line = "0" end fields = line.split(/\t/) num = fields.shift.to_i sum = sum + num diam = 32 / ((num + 8) ** 0.6) for i in 0...fields.length next if i % 2 > 0 key = fields[i] val = fields[i+1].to_i * diam cur = allkwd[key] allkwd[key] = cur ? cur + val : val end end rescue ensure db.close if db end if sum > 0 && allkwd.size > 0 escword = CGI::escapeHTML(word) printf("

Related words of \"%s\"" + \ " (%d).

\n", escword, sum) scores = [] allkwd.each do |key, val| scores.push([key, val]) end scores.sort! do |a, b| b[1] - a[1] end printf("\n") else printf("

There is no related word for \"%s\".

\n", escword) printf("

\"%s\" could not be opened.

", DBNAME) unless db end else printf("

Input a source word of relation.

\n") end printf("
\n"); # show page footer printf("%s", <<__EOF __EOF ) # exit normally exit(0) # END OF FILE