~ubuntu-branches/ubuntu/trusty/hyperestraier/trusty-proposed

« back to all changes in this revision

Viewing changes to lab/relwords.cgi

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2006-11-14 05:28:32 UTC
  • mfrom: (2.1.4 feisty)
  • Revision ID: james.westby@ubuntu.com-20061114052832-0lzqzcefn8mt4yqe
Tags: 1.4.9-1.1
* Non-maintainer upload.
* High-urgency upload for RC bugfix.
* Set HOME=$(CURDIR)/junkhome when building, otherwise the package build
  will incorrectly look for headers there -- and fail when the directory
  exists and is unreadable, as happens sometimes on sudo-using
  autobuilders!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/ruby
 
2
 
 
3
#================================================================
 
4
# relwords.cgi
 
5
# CGI script to dump related words
 
6
#================================================================
 
7
 
 
8
 
 
9
# library requirement
 
10
require "cgi"
 
11
require "villa"
 
12
 
 
13
 
 
14
# constants
 
15
DBNAME = "relwords.qdb"
 
16
 
 
17
 
 
18
# get parameters
 
19
scriptname = ENV["SCRIPT_NAME"]
 
20
scriptname = $0 unless scriptname
 
21
scriptname = scriptname.gsub(/.*\//, "")
 
22
cgi = CGI::new
 
23
word = cgi["word"]
 
24
word.downcase!
 
25
word.strip!
 
26
 
 
27
 
 
28
# show HTTP headers
 
29
printf("Content-Type: text/html; charset=UTF-8\r\n\r\n")
 
30
 
 
31
 
 
32
# show page header
 
33
printf("%s", <<__EOF
 
34
<?xml version="1.0" encoding="UTF-8"?>
 
35
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
36
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
37
<head>
 
38
<meta http-equiv="Content-Language" content="en" />
 
39
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 
40
<meta http-equiv="Content-Style-Type" content="text/css" />
 
41
<link rel="contents" href="./" />
 
42
<title>Related Word Extractor</title>
 
43
<style type="text/css">html { margin: 0em 0em; padding 0em 0em; background: #eeeeee none; }
 
44
body { margin: 1em 2em; padding 0em 0em;
 
45
  background: #eeeeee none; color: #444444;
 
46
  font-style: normal; font-weight: normal; }
 
47
h1 { margin-top: 1.8em; margin-bottom: 1.3em; font-weight: bold; font-size: larger; }
 
48
div.myform { margin: 1em 1em; }
 
49
p { margin: 1em 1em; }
 
50
a { color: #0022aa; text-decoration: none; font-weight: bold; }
 
51
a:hover,a:focus { color: #0033ee; text-decoration: underline; }
 
52
a.head { color: #111111; text-decoration: none; }
 
53
strong { color: #000000; }
 
54
ul { line-height: 150%; }
 
55
</style>
 
56
</head>
 
57
<body>
 
58
<hr />
 
59
__EOF
 
60
);
 
61
 
 
62
 
 
63
# show input form
 
64
escword = CGI::escapeHTML(word)
 
65
printf("%s", <<__EOF
 
66
<form method="get" action="#{scriptname}">
 
67
<div class="myform">Word:
 
68
<input type="text" name="word" value="#{escword}" size="32" />
 
69
<input type="submit" value="Extract" />
 
70
</div>
 
71
</form>
 
72
<hr />
 
73
__EOF
 
74
);
 
75
 
 
76
 
 
77
# show result
 
78
if word.length > 0
 
79
  sum = 0
 
80
  allkwd = {}
 
81
  begin
 
82
    db = Villa::new(DBNAME)
 
83
    terms = word.split(/[ \t]/)
 
84
    terms.each do |term|
 
85
      next if term.length < 1
 
86
      begin
 
87
        line = db.get(term)
 
88
      rescue
 
89
        line = "0"
 
90
      end
 
91
      fields = line.split(/\t/)
 
92
      num = fields.shift.to_i
 
93
      sum = sum + num
 
94
      diam = 32 / ((num + 8) ** 0.6)
 
95
      for i in 0...fields.length
 
96
        next if i % 2 > 0
 
97
        key = fields[i]
 
98
        val = fields[i+1].to_i * diam
 
99
        cur = allkwd[key]
 
100
        allkwd[key] = cur ? cur + val : val
 
101
      end
 
102
    end
 
103
  rescue
 
104
  ensure
 
105
    db.close if db
 
106
  end
 
107
  if sum > 0 && allkwd.size > 0
 
108
    escword = CGI::escapeHTML(word)
 
109
    printf("<p>Related words of \"<strong id=\"origword\">%s</strong>\"" + \
 
110
           " (<span id=\"orignum\">%d</span>).</p>\n", escword, sum)
 
111
    scores = []
 
112
    allkwd.each do |key, val|
 
113
      scores.push([key, val])
 
114
    end
 
115
    scores.sort! do |a, b|
 
116
      b[1] - a[1]
 
117
    end
 
118
    printf("<ul>\n")
 
119
    i = 0
 
120
    scores.each do |elem|
 
121
      key = elem[0]
 
122
      val = elem[1]
 
123
      printf("<li><a href=\"%s?word=%s\" id=\"relword%04d\">%s</a>" + \
 
124
             " (<span id=\"relnum%04d\">%d</span>)</li>\n", scriptname, CGI::escape(key),
 
125
             i / 2 + 1, CGI::escapeHTML(key), i / 2 + 1, val)
 
126
      i += 1
 
127
      break if i >= 32
 
128
    end
 
129
    printf("</ul>\n")
 
130
  else
 
131
    printf("<p>There is no related word for \"<strong id=\"origword\">%s</strong>\".</p>\n",
 
132
           escword)
 
133
    printf("<p>\"%s\" could not be opened.</p>", DBNAME) unless db
 
134
  end
 
135
else
 
136
  printf("<p>Input a source word of relation.</p>\n")
 
137
end
 
138
printf("<hr />\n");
 
139
 
 
140
 
 
141
# show page footer
 
142
printf("%s", <<__EOF
 
143
</body>
 
144
</html>
 
145
__EOF
 
146
)
 
147
 
 
148
 
 
149
# exit normally
 
150
exit(0)
 
151
 
 
152
 
 
153
 
 
154
# END OF FILE