Skip to content

Commit

Permalink
now with AR support
Browse files Browse the repository at this point in the history
  • Loading branch information
nallan committed Aug 12, 2013
1 parent 2519c8d commit 064b862
Show file tree
Hide file tree
Showing 9 changed files with 7,921 additions and 83 deletions.
32 changes: 26 additions & 6 deletions README.md
Expand Up @@ -6,17 +6,19 @@ A-B-Chi is a RubyGem aimed at making the process of sorting Chinese Traditional

I have been wroking in Taiwan for the last year on a CRM-type database and have learned quite a bit about Chinese Traditional characters and their attributes. after pulling my hair out over collation I decided that the best thing to do would be to make my own solution and as I notice that a lot of other people have similar problems I wanted to make this publically available.


There are a few extra functions that I plan to implement in the future which include

- Character to BoPoMoFo (pronunciation characters) and other various things - perhaps a ruby text function or two.
- Handling of English and Chinese Simplified Characters.
- Character to BoPoMoFo (pronunciation characters) and other various things - perhaps a ruby text function or two. (almost done)
- Handling of English and Chinese Simplified Characters. (almost done)
- Base everything around the utf-8 character codes rather than a list of characters.

I really hope this can help someone as I have lost some serious sleep working all this stuff out over a 10 month period!

Version
-

1.2.4
2.0.0
The new version of this gem can now handle Rails 4.0 Active Record Relation objects!

Further Info
-----------
Expand All @@ -26,6 +28,8 @@ Further Info

- You should also check out the Taiwan Government's official write-up on their standards which can be found here: http://www.cns11643.gov.tw/AIDB/encodings_en.do

- I broke the tests when I added in the ActiveRecord stuff...ill fix that in a few days!

This comment has been minimized.

Copy link
@1Bennell

1Bennell Jul 20, 2018

Installation


Installation
--------------
Expand All @@ -34,6 +38,13 @@ Installation
or for bundler add the following to your Gemfile
```gem 'a-b-chi'```

If you wish to use this on an ActiveRecord Relation, open the model this collection is concerned with and add

```include Chinese```

This must be done on a per model basis.


The sourcecode can be found at https://github.com/nallan/a-b-chi

Usage
Expand All @@ -45,8 +56,17 @@ next you must create a new instance of the ArrayChinese class and populate your
```arr = ArrayChinese.new
arr.push "李依恬", "李雲帆", "珍項小珍", "瑞芳", "李宗翰"```
finally run the sort method on this. both sort and sort! are supported
```arr.sort
arr.sort!```
```arr.sort OR arr.sort!```

When it comes to the ActiveDirectory stuff, simply get a collection

```@models = Model.all```

and then use the sort_chi method to arrange it phonetically

```@models.sort_chi('field1', 'field2' etc...) OR @models.sort_chi!('field1', 'field2' etc...)```



License
-
Expand Down
Binary file removed a-b-chi-1.2.4.gem
Binary file not shown.
10 changes: 5 additions & 5 deletions a-b-chi.gemspec
@@ -1,13 +1,13 @@
Gem::Specification.new do |s|
s.name = 'a-b-chi'
s.version = '1.2.4'
s.version = '2.0.0'
s.date = Time.now
s.summary = "Chinese Traditional Character Sort"
s.description = "A gem that allows for the user to sort Chinese Traditional characters like a human (phonetically) rather than like a robot (stroke count)"
s.summary = "Chinese Traditional Sorter and Manipulator"
s.description = "A convenient gem that will allow you to sort arrays and activerecord relations without needing to mess with set-months-ago database collation settings."
s.authors = ["Nathanial Allan"]
s.email = 'nathanial.allan@gmail.com'
s.files = ["Rakefile", "README.md", "lib/a-b-chi.rb", "lib/characters.txt", "lib/bopomofo.txt", "test/test_a-b-chi.rb"]
s.files = ["Rakefile", "README.md", "lib/a-b-chi.rb", "lib/chinese.rb", "lib/characters.txt", "lib/bopomofo.txt", "test/test_a-b-chi.rb", "spec/a-b-chi_spec.rb"]
s.homepage = 'http://rubygems.org/gems/a-b-chi'
s.license = 'MIT'
s.post_install_message = "ITM!"
#s.post_install_message = "ITM!"
end
39 changes: 18 additions & 21 deletions lib/a-b-chi.rb
@@ -1,8 +1,10 @@
#encoding:utf-8

require 'chinese'

class ArrayChinese < Array
alias array_sort sort
alias array_sort_by sort_by
alias array_sort_by! sort_by!
alias array_sort_by sort_by

def initialize
spec = Gem::Specification.find_by_name("a-b-chi")
Expand All @@ -11,28 +13,17 @@ def initialize
@characters = File.read("#{@gem_lib}/characters.txt").split("\n")
end

def sort(*args)
process_args(args)
def sort
self.array_sort_by { |sort_string| eval(get_sort_query) }
end

def sort!(*args)
def sort!
self.array_sort_by! { |sort_string| eval(get_sort_query) }
end

# def sort_by(*args)
# puts args.inspect
# self.array_sort_by! { |sort_string| eval(get_sort_query) }
# end

private

def process_args(*args)

end

def get_sort_query
# max_length = self.max_by(&:length).length
sort_query = '['
(0..self.max_by(&:length).length).each { |i| sort_query << " @characters.index(sort_string[#{i}])," }
sort_query.chop!
Expand All @@ -42,9 +33,8 @@ def get_sort_query

end


class StringChinese < String

def initialize
spec = Gem::Specification.find_by_name("a-b-chi")
gem_root = spec.gem_dir
Expand All @@ -53,9 +43,16 @@ def initialize
@bpmf = File.read("#{@gem_lib}/bopomofo.txt").split("\n")
end

def to_ruby_bpmf
bopomofoed = ''
(0..self.length).each { |i| bopomofoed << "self[#{i}](@bpmf.index(@characters.index[self[#{i}]]))"}
def to_ruby_unmarked
unmarked = ''
(0..(self.size-1)).each { |i| unmarked << "#{self[i]}(#{@bpmf[@characters.index(self[i])]})"}
return unmarked
end

end
def to_ruby_markup
markup = '<p style="writing-mode: tb-rl"><ruby>'
(0..(self.size-1)).each { |i| markup << "<rb>#{self[i]}</rb><rp>(</rp><rt>#{@bpmf[@characters.index(self[i])]}</rt><rp>)</rp>"}
markup << '</p></ruby>'
end

end
9 changes: 5 additions & 4 deletions lib/bopomofo.txt
Expand Up @@ -1653,7 +1653,7 @@
ㄉㄧㄥ
ㄈㄢ
ㄈㄢˊ
ㄆㄚˋ
ㄈㄤ
ㄒㄧ
ㄉㄢˋ
ㄊㄤˇ
Expand Down Expand Up @@ -5258,7 +5258,7 @@
ㄆㄨˇ
ㄌㄧㄣˊ
ㄨㄟˊ
ㄙㄜˋ
ㄏㄢ
ㄔㄥˊ
ㄉㄥˋ
ㄔㄥˊ
Expand Down Expand Up @@ -8027,7 +8027,7 @@
ㄅㄤ
ㄍㄨˇ
ㄆㄢˊ
ㄓㄡˋ
ㄒㄧㄤˋ
ㄐㄧㄢ
ㄘㄨㄛˋ
ㄘㄨㄛˇ
Expand Down Expand Up @@ -8627,7 +8627,7 @@
ㄒㄧㄢˋ
ㄊㄨㄟˇ
ㄅㄤˋ
ㄅㄤˇ
ㄓㄡˋ
ㄆㄤ
ㄆㄤˊ
ㄌㄩˇ
Expand Down Expand Up @@ -8679,6 +8679,7 @@
ㄕㄢ
ㄍㄨㄚ
ㄉㄢˇ
ㄓㄣ
ㄎㄨㄞˋ
ㄋㄨㄥˊ
ㄊㄨㄣˊ
Expand Down

0 comments on commit 064b862

Please sign in to comment.