yazawa 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0a2c1bc3059af8969bbcfa15ad566c07440ea69d
4
+ data.tar.gz: c4da92543c6bc7fc7284567e88be2d2625bc3eb4
5
+ SHA512:
6
+ metadata.gz: 4839e50863793f583910e52ba3daa92cdff52b482bceadea60186ed97c83391b047dddde097bd5576dca6d9f03756a113500319b88c2aeb709be0823db0978f4
7
+ data.tar.gz: e0e86e5205516aef27986b61c0e97b33aa00803624350c0a1cde2620d424927e94e22d2a141705cca6965caa28eb9d82113b2b7498f0593d7663aa2cd5f08f77
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yazawa.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 toooooooby
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,71 @@
1
+ # YAZAWA
2
+ [![Gem Version](https://badge.fury.io/rb/yazawa.png)](http://badge.fury.io/rb/yazawa) [![Build Status](https://travis-ci.org/toooooooby/yazawa.png?branch=master)](https://travis-ci.org/toooooooby/yazawa) [![Dependency Status](https://gemnasium.com/toooooooby/yazawa.png)](https://gemnasium.com/toooooooby/yazawa) [![Code Climate](https://codeclimate.com/github/toooooooby/yazawa.png)](https://codeclimate.com/github/toooooooby/yazawa) [![Coverage Status](https://coveralls.io/repos/toooooooby/yazawa/badge.png?branch=master)](https://coveralls.io/r/toooooooby/yazawa)
3
+
4
+ **『YAZAWA』** is one of `text-converter`, like [Yazawa](http://en.wikipedia.org/wiki/Eikichi_Yazawa).
5
+
6
+ ```bash
7
+ $ yazawa '俺達の熱意で世界が変わる'
8
+ 俺達の『NETSUI』で世界が変わる
9
+ ```
10
+
11
+ ## Installation
12
+
13
+ Requirements:
14
+
15
+ * MeCab (`sudo apt-get install mecab libmecab-dev mecab-ipadic-utf8` on Debian or Ubuntu linux)
16
+ * Ruby 1.9.x or lator
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'yazawa'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install yazawa
29
+
30
+ ## Usage as a command
31
+
32
+ Usage: `yazawa TEXT [-r|--random]`
33
+
34
+ ## Examples as a command
35
+
36
+ ```bash
37
+ $ yazawa '俺達の熱意で世界が変わる'
38
+ 俺達の『NETSUI』で世界が変わる
39
+
40
+ $ yazawa -r '俺達の熱意で世界が変わる'
41
+ 俺達の熱意で『SEKAI』が変わる
42
+
43
+ $ yazawa -r '俺達の熱意で世界が変わる'
44
+ 俺達の熱意で世界が『KAWARU』
45
+
46
+ $ echo '唸る回転寿司' | yazawa
47
+ 唸る『KAITEN』寿司
48
+
49
+ $ yazawa '便利を勘違いしていないか?'
50
+ 便利を『KANCHIGAI』していないか?
51
+
52
+ $ yazawa '意識の高いインターンが社内チャットで「世界を変えたいんです!!!」とか主張してたら社員が「ドラム缶で核融合するもの作ってどっかの国で爆発させましょうよ」とか言いだして意識格差すごい'
53
+ 意識の高いインターンが社内チャットで「世界を変えたいんです!!!」とか主張してたら社員が「『DORAMUKAN』で核融合するもの作ってどっかの国で爆発させましょうよ」とか言いだして意識格差すごい
54
+ ```
55
+
56
+ ## Examples as a library for ruby
57
+
58
+ ```ruby
59
+ require 'yazawa'
60
+
61
+ p Yazawa.convert('俺達の熱意で世界が変わる') # => '俺達の『NETSUI』で世界が変わる'
62
+ p Yazawa.convert('俺達の熱意で世界が変わる', at_random: true) # => '俺達の熱意で『SEKAI』が変わる'
63
+ ```
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it ( http://github.com/toooooooby/yazawa/fork )
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ task default: :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.test_files = FileList['test/test_*.rb']
8
+ t.verbose = true
9
+ end
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require_relative '../lib/yazawa'
5
+
6
+ config = {}
7
+ opt = OptionParser.new do |opts|
8
+ program_name = File.basename($PROGRAM_NAME)
9
+
10
+ opts.banner = 'YAZAWA: One of text-converter like YAZAWA'
11
+ opts.define_head "Usage: #{program_name} TEXT"
12
+ opts.separator <<-EOD
13
+ Examples:
14
+ $ #{program_name} '俺達の熱意で世界が変わる'
15
+ 俺達の『NETSUI』で世界が変わる
16
+
17
+ $ #{program_name} -r '俺達の熱意で世界が変わる'
18
+ 俺達の熱意で『SEKAI』が変わる'
19
+
20
+ $ #{program_name} -r '俺達の熱意で世界が変わる'
21
+ 俺達の熱意で世界が『KAWARU』
22
+
23
+ $ echo '唸る回転寿司' | #{program_name}
24
+ 唸る『KAITEN』寿司
25
+
26
+ $ #{program_name} '便利を勘違いしていないか?'
27
+ 便利を『KANCHIGAI』していないか?
28
+
29
+ $ #{program_name} '意識の高いインターンが社内チャットで「世界を変えたいんです!!!」とか主張してたら社員が「ドラム缶で核融合するもの作ってどっかの国で爆発させましょうよ」とか言いだして意識格差すごい'
30
+ 意識の高いインターンが社内チャットで「世界を変えたいんです!!!」とか主張してたら社員が「『DORAMUKAN』で核融合するもの作ってどっかの国で爆発させましょうよ」とか言いだして意識格差すごい
31
+
32
+ Options:
33
+ EOD
34
+
35
+ opts.on('-r', '--random', 'Random mode. Default: false') do
36
+ config[:at_random] = true
37
+ end
38
+ opts.on_tail('-h', '--help', 'Show this message') do
39
+ puts opts
40
+ exit
41
+ end
42
+ opts.parse!
43
+ end
44
+
45
+ # When no args on interactive shell
46
+ if $stdin.tty? && ARGV.empty?
47
+ puts opt
48
+ exit 0
49
+ end
50
+
51
+ # Fetch source text
52
+ source = ARGV.shift || ARGF.read
53
+
54
+ puts YAZAWA.convert(source, config)
@@ -0,0 +1,91 @@
1
+ #require "yazawa/version"
2
+
3
+ require 'mecab/light'
4
+ require 'mojinizer'
5
+
6
+ module MeCab
7
+ module Light
8
+ class CustomTagger < Tagger
9
+ def initialize(options)
10
+ @mecab = Binding.new(options)
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ module YAZAWA
17
+ class << self
18
+ # e.g. "空飛ぶ 寿司" => "空飛ぶ 『SUSHI』"
19
+ def convert(text, options = {})
20
+ # e.g. "空飛ぶ 寿司" => "空飛ぶ 『SUSHI』"
21
+ # Find a word index which is 'noun'(名詞) and longest length
22
+ index_for_replace = find_suitable_index_for_replace(tagger.parse(text), options)
23
+
24
+ # Convert specific word only
25
+ words = separate_words(text)
26
+ words[index_for_replace] = convert_word(words[index_for_replace])
27
+ words.join
28
+ end
29
+
30
+ def tagger
31
+ # Specify mecab options for keeping white spaces in parsed text
32
+ @tagger ||= MeCab::Light::CustomTagger.new('--node-format=%M\t%H\n --unk-format=%M\t%H\n')
33
+ end
34
+
35
+ # e.g. "空飛ぶ寿司" => ["空", "飛ぶ", "寿司"]
36
+ def separate_words(text)
37
+ tagger.parse(text).map(&:surface)
38
+ end
39
+
40
+ # e.g. "ジャバ" => "『JABA』"
41
+ # e.g. " JAVA" => " 『JAVA』"
42
+ def convert_word(word)
43
+ # e.g. " JAVA" => " "
44
+ left_space = word.match(/^\s+/).to_s
45
+ striped_word = word.lstrip
46
+
47
+ # e.g. "ジャバ" => "JABA"
48
+ katakana = tagger.parse(striped_word).map{|x| x.feature.split(',')[7] }.join
49
+ katakana = striped_word if katakana == ""
50
+
51
+ # Generate a result
52
+ left_space + "『" + katakana.romaji.upcase + "』"
53
+ end
54
+
55
+ def find_suitable_index_for_replace(parsed_words, options = {})
56
+ index_for_replace = 0
57
+ max_score = 0
58
+
59
+ parsed_words.each_with_index do |result, index|
60
+ # Calculate priority for determining a suitable word
61
+ score =
62
+ # Japanese++
63
+ (result.surface.contains_japanese? ? 100 : 0) +
64
+ # Katakana++
65
+ (result.surface.contains_katakana? ? 10 : 0) +
66
+ # Kanji++
67
+ (result.surface.contains_kanji? ? 10 : 0) +
68
+ # adjective++
69
+ (result.feature.split(',')[0] == "形容詞" ? 20 : 0) +
70
+ # noun++
71
+ (result.feature.split(',')[0] == "名詞" ? 10 : 0) +
72
+ # verb++
73
+ (result.feature.split(',')[0] == "動詞" ? 8 : 0)
74
+
75
+ score += if options[:at_random]
76
+ rand(20)
77
+ else
78
+ result.surface.length
79
+ end
80
+
81
+ if max_score < score
82
+ max_score = score
83
+ index_for_replace = index
84
+ end
85
+ end
86
+
87
+ index_for_replace
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module YAZAWA
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ $VERBOSE = true
4
+ require 'minitest/autorun'
5
+ begin require 'minitest/pride' rescue LoadError end # Ignore error for old ruby
6
+
7
+ require_relative '../lib/yazawa'
8
+
9
+ describe 'YAZAWA' do
10
+ TEST_FIXTURES = [
11
+ # One Katakana word
12
+ {
13
+ original: '仕事、課金しなくてもどんどんクエストくるしすごい',
14
+ converted: '仕事、課金しなくてもどんどん『KUESUTO』くるしすごい',
15
+ separated: ['仕事', '、', '課金', 'し', 'なく', 'て', 'も', 'どんどん', 'クエスト', 'くるし', 'すごい'],
16
+ },
17
+
18
+ # Some Katakana words
19
+ {
20
+ original: '意識の高いインターンが社内チャットで「世界を変えたいんです!!!」とか主張してたら社員が「ドラム缶で核融合するもの作ってどっかの国で爆発させましょうよ」とか言いだして意識格差すごい',
21
+ converted: '意識の高いインターンが社内チャットで「世界を変えたいんです!!!」とか主張してたら社員が「『DORAMUKAN』で核融合するもの作ってどっかの国で爆発させましょうよ」とか言いだして意識格差すごい',
22
+ separated: ["意識", "の", "高い", "インターン", "が", "社内", "チャット", "で", "「", "世界", "を", "変え", "たい", "ん", "です", "!", "!", "!", "」", "とか", "主張", "し", "て", "たら", "社員", "が", "「", "ドラム缶", "で", "核", "融合", "する", "もの", "作っ", "て", "どっか", "の", "国", "で", "爆発", "さ", "せ", "ましょ", "う", "よ", "」", "とか", "言い", "だし", "て", "意識", "格差", "すごい"],
23
+ },
24
+
25
+ # One word only
26
+ {
27
+ original: 'ジャバ',
28
+ converted: '『JABA』',
29
+ separated: ['ジャバ'],
30
+ },
31
+
32
+ # No Katakana characters
33
+ {
34
+ original: '職質の本、すごいこと書いてあるな http://instagram.com/p/bwDEFvRrz8/',
35
+ converted: '職質の本、『SUGOI』こと書いてあるな http://instagram.com/p/bwDEFvRrz8/',
36
+ separated: ["職", "質", "の", "本", "、", "すごい", "こと", "書い", "て", "ある", "な", " http", "://", "instagram", ".", "com", "/", "p", "/", "bwDEFvRrz", "8", "/"],
37
+ },
38
+ {
39
+ original: '空飛ぶ 寿司',
40
+ converted: '空飛ぶ 『SUSHI』',
41
+ separated: ["空", "飛ぶ", " 寿司"],
42
+ },
43
+ ]
44
+
45
+ TEST_FIXTURES.each do |text|
46
+ it "can converts text to like Japanese yankee''s one(#{text[:original].slice(0..9)}...)" do
47
+ YAZAWA.convert(text[:original]).must_equal text[:converted]
48
+ end
49
+
50
+ it "can separate words in Japanese with spaces(#{text[:original].slice(0..9)}...)" do
51
+ YAZAWA.separate_words(text[:original]).must_equal text[:separated]
52
+
53
+ end
54
+ end
55
+
56
+ it "can convert one word" do
57
+ YAZAWA.convert_word('ドラム缶').must_equal '『DORAMUKAN』'
58
+ YAZAWA.convert_word('ジャバ').must_equal '『JABA』'
59
+ YAZAWA.convert_word(' JAVA').must_equal ' 『JAVA』'
60
+ YAZAWA.convert_word(' JAVA').must_equal ' 『JAVA』'
61
+ end
62
+
63
+ it "can find suitable index for replace" do
64
+ YAZAWA.find_suitable_index_for_replace(YAZAWA.tagger.parse "空飛ぶ寿司").must_equal 2
65
+ YAZAWA.find_suitable_index_for_replace(YAZAWA.tagger.parse "職質の本、すごいこと書いてある").must_equal 5
66
+
67
+ end
68
+
69
+ end
70
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yazawa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yazawa"
8
+ spec.version = YAZAWA::VERSION
9
+ spec.authors = ["toooooooby"]
10
+ spec.email = ["toby.net.info.mail+git@gmail.com"]
11
+ spec.summary = %q{『YAZAWA』 is one of text-converter, like Yazawa}
12
+ spec.description = %q{$ yazawa '俺達の熱意で世界が変わる'\n俺達の『NETSUI』で世界が変わる}
13
+ spec.homepage = "https://github.com/toooooooby/yazawa"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'mecab-light', '~> 0.2'
22
+ spec.add_dependency 'mojinizer', '~> 0.2'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yazawa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - toooooooby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mecab-light
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mojinizer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: "$ yazawa '俺達の熱意で世界が変わる'\\n俺達の『NETSUI』で世界が変わる"
70
+ email:
71
+ - toby.net.info.mail+git@gmail.com
72
+ executables:
73
+ - yazawa
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/yazawa
83
+ - lib/yazawa.rb
84
+ - lib/yazawa/version.rb
85
+ - test/test_yazawa.rb
86
+ - yazawa.gemspec
87
+ homepage: https://github.com/toooooooby/yazawa
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.0
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: "『YAZAWA』 is one of text-converter, like Yazawa"
111
+ test_files:
112
+ - test/test_yazawa.rb