jtoy / crdict

A dictionary based off cedict written in RubyCocoa

This URL has Read+Write access

crdict / Controller.rb
100644 76 lines (63 sloc) 1.681 kb
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
require 'osx/cocoa'
require 'rubygems'
require 'jcode'
$KCODE='u'
require 'activesupport'
class Controller < OSX::NSObject
  attr_writer :results_table_view, :search_field
  
  def awakeFromNib
@results = []
@results_table_view.dataSource = self
@path = OSX::NSBundle.mainBundle.resourcePath.fileSystemRepresentation
# grep kCantonese Unihan.txt > unihancantonese.txt
@cantonese = File.readlines @path+"/unihancantonese.txt"
  end
 
  def dict
@dict ||= File.readlines @path+"/cedict_ts.u8"
  end
  
  def numberOfRowsInTableView view
    @results.size
  end
    
  def tableView_objectValueForTableColumn_row(view, column, index)
  #def tableView(view, objectValueForTableColumn:column, row:index)
    r = @results[index]
  r.send column.identifier
  end
  
  def search(sender)
q = @search_field.stringValue
puts "query: #{q}"
matches = q.blank? ? [] : dict.select{|x| x.match /#{q}/}
@results = []
sender.window.Title= "#{matches.size} results for #{q}"
matches.each do |x|
r= Result.new
data= /(\S+) (\S+) \[(.+)\] (.+)/.match(x)
#data = x.split(" ")
 
#data = []
puts 'qqq'
r.chinese = data[1]
r.english = data[4]
r.pinyin = data[3]
r.simplified = data[2]
hex = "U+"+("%x" % (data[1].unpack('U*').to_s.to_i)).upcase
jyutpin = @cantonese.detect{|x| x.match /#{Regexp.escape(hex)}/}
r.jyutpin = jyutpin.strip.split[2..-1].to_s if jyutpin
@results << r
end
@results_table_view.reloadData
  end
end
 
 
class Result
  attr_accessor :chinese, :english,:pinyin,:simplified,:jyutpin
end
 
class String
  def blank?
    nil? || strip == ""
  end
  
  def chinese?
    !english?
  end
  
  def english?
    match(/\D+/)
  end
  
end