ranjax 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bab74e88c5b5d674ddeee9da55b48dd87a5e4bdb
4
+ data.tar.gz: e5fa025ac236836ec6060ada7884102127eaed6b
5
+ SHA512:
6
+ metadata.gz: 93af80b570fb16cc4b6e631e2557d91115cc4d5437b2338c4969a1a7de5f05c6dfff0b2f6e008c48ef6773908a55edc7a079039609b3b2c70803ac8cb47e2a67
7
+ data.tar.gz: dd4cfb5df9c39e4e2d78348b1f1765ea13c39136bbde68f07a0ef6d119ac6f6dd9d1dbe8c8381be529d73682e6b56c012d8cf4741c6c493ee55a343c1d896497
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Gem dependencies declared in natto.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ Ranjax
2
+ ==============
3
+
4
+ Random japanese text generator
5
+
6
+ ## Usage
7
+
8
+ ```ruby
9
+ text = <<EOS
10
+ 日本語(にほんご、にっぽんご)は、主に日本国内や日本人同士の間で使われている言語である。日本は法令によって「公用語」を規定していないが、法令その他の公用文は全て日本語で記述され、各種法令(裁判所法第74条、会社計算規則第57条、特許法施行規則第2条など)において日本語を用いることが定められるなど事実上の公用語となっており、学校教育の「国語」でも教えられる。
11
+ 使用人口について正確な統計はないが、日本国内の人口、および日本国外に住む日本人や日系人、日本がかつて統治した地域の一部の住民など、約1億3千万人以上と考えられている。統計によって前後する可能性はあるが、この数は世界の母語話者数で上位10位以内に入る人数である。
12
+ 日本で生まれ育ったほとんどの人は、日本語を母語とする。日本語の文法体系や音韻体系を反映する手話として日本語対応手話がある。
13
+ 2013年1月現在、インターネット上の言語使用者数は、英語、中国語、スペイン語、アラビア語、ポルトガル語に次いで6番目に多い。
14
+ EOS
15
+
16
+ ranjax = Ranjax.new()
17
+ ranjax.import_text(text)
18
+ ranjax.save("ranjax.bin")
19
+
20
+ ranjax = Ranjax.new(path: "ranjax.bin")
21
+ text = ranjax.generate_text()
22
+ #"日本語(にほんご、にっぽんご)は、英語、中国語、スペイン語、スペイン語、ポルトガル語に次いで6番目に多い。"
23
+ ```
24
+
25
+ ## Requirements
26
+
27
+ * MeCab 0.996
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/ranjax.rb ADDED
@@ -0,0 +1,85 @@
1
+ require 'natto'
2
+
3
+ class Ranjax
4
+ END_OF_TEXT = '__E__'
5
+ @words = []
6
+ @head_idxs = []
7
+
8
+ def initialize(path: nil)
9
+ @words = []
10
+ @head_idxs = []
11
+
12
+ unless path.nil?
13
+ load(path)
14
+ end
15
+ end
16
+
17
+ def import_text(text)
18
+ nm = Natto::MeCab.new
19
+
20
+ words = []
21
+ nm.parse(text) do |n|
22
+ words << n.surface
23
+ end
24
+ words << END_OF_TEXT
25
+
26
+ @head_idxs << @words.size
27
+ @words += words
28
+ end
29
+
30
+ def generate_text(max: nil)
31
+ units = []
32
+ @words.each_cons(3) do |unit|
33
+ units << unit
34
+ end
35
+
36
+ head_idx= @head_idxs.sample
37
+ t1 = units[head_idx][0]
38
+ t2 = units[head_idx][1]
39
+
40
+ dst_text = t1 + t2
41
+ loop do
42
+ candidate_units = []
43
+ units.each do |unit|
44
+ candidate_units << unit if unit[0] == t1 && unit[1] == t2
45
+ end
46
+
47
+ break if candidate_units.size == 0
48
+
49
+ unit = candidate_units.sample
50
+
51
+ break if max != nil && dst_text.size + unit[2].size > max
52
+
53
+ break if unit[2] == END_OF_TEXT
54
+
55
+ dst_text += unit[2]
56
+
57
+ t1 = unit[1]
58
+ t2 = unit[2]
59
+ end
60
+
61
+ dst_text
62
+ end
63
+
64
+ def save(path)
65
+ if path.empty?
66
+ raise ArgumentError.new('Bad Path')
67
+ end
68
+
69
+ data = Marshal.dump({
70
+ :words=>@words,
71
+ :head_idxs=>@head_idxs
72
+ })
73
+ File.write(path, data)
74
+ end
75
+
76
+ private
77
+
78
+ def load(path)
79
+ r = File.read(path)
80
+ data = Marshal.load(r)
81
+ @words = data[:words]
82
+ @head_idxs = data[:head_idxs]
83
+ end
84
+
85
+ end
@@ -0,0 +1,3 @@
1
+ module Ranjax
2
+ VERSION = '0.1.0'
3
+ end
data/ranjax.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ $:.unshift('lib')
2
+ require 'ranjax/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'ranjax'
6
+ spec.version = Ranjax::VERSION
7
+ spec.authors = ['maruware']
8
+ spec.email = ['maruware@maruware.com']
9
+ spec.summary = %q{Random japanese text generator}
10
+ spec.description = %q{Random japanese text generator}
11
+ spec.homepage = 'https://github.com/maruware/ranjax'
12
+ spec.license = 'MIT'
13
+
14
+ spec.required_ruby_version = '>= 1.9'
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_dependency 'natto'
19
+ end
@@ -0,0 +1,84 @@
1
+ $: << 'lib'
2
+ require 'minitest'
3
+ require 'minitest/autorun'
4
+
5
+ require 'ranjax'
6
+
7
+ class RanjaxTest < MiniTest::Unit::TestCase
8
+ def setup
9
+ @texts = []
10
+ @texts << <<EOS
11
+ 日本語(にほんご、にっぽんご)は、主に日本国内や日本人同士の間で使われている言語である。日本は法令によって「公用語」を規定していないが、法令その他の公用文は全て日本語で記述され、各種法令(裁判所法第74条、会社計算規則第57条、特許法施行規則第2条など)において日本語を用いることが定められるなど事実上の公用語となっており、学校教育の「国語」でも教えられる。
12
+ 使用人口について正確な統計はないが、日本国内の人口、および日本国外に住む日本人や日系人、日本がかつて統治した地域の一部の住民など、約1億3千万人以上と考えられている。統計によって前後する可能性はあるが、この数は世界の母語話者数で上位10位以内に入る人数である。
13
+ 日本で生まれ育ったほとんどの人は、日本語を母語とする。日本語の文法体系や音韻体系を反映する手話として日本語対応手話がある。
14
+ 2013年1月現在、インターネット上の言語使用者数は、英語、中国語、スペイン語、アラビア語、ポルトガル語に次いで6番目に多い。
15
+ EOS
16
+ @texts << <<EOS
17
+ 日本語は、主に日本国内で使用される。話者人口についての調査は国内・国外を問わず未だないが、日本の人口に基づいて考えられることが一般的である。
18
+ 日本国内に、法令上、日本語を公用語ないし国語と定める直接の規定はない。しかし、そもそも法令は日本語で記されており、裁判所法においては「裁判所では、日本語を用いる」(同法74条)とされ、文字・活字文化振興法においては「国語」と「日本語」が同一視されており(同法3条、9条)、その他多くの法令において、日本語が唯一の公用語ないし国語であることが当然の前提とされている。また、法文だけでなく公用文はすべて日本語のみが用いられ、学校教育では日本語が「国語」として教えられている。
19
+ EOS
20
+ end
21
+
22
+ def test_simple_usage
23
+ text = @texts[0]
24
+
25
+ ranjax = Ranjax.new()
26
+ ranjax.import_text(text)
27
+
28
+ text = ranjax.generate_text()
29
+ assert_instance_of String, text
30
+ end
31
+
32
+ def test_generate_text_max_length()
33
+ text = @texts[0]
34
+
35
+ ranjax = Ranjax.new()
36
+ ranjax.import_text(text)
37
+ text = ranjax.generate_text(max: 40)
38
+ assert text.size <= 40
39
+ end
40
+
41
+ def test_multi_import_text
42
+ text = @texts[0]
43
+
44
+ ranjax = Ranjax.new()
45
+ ranjax.import_text(text)
46
+
47
+ text = @texts[1]
48
+
49
+ ranjax = Ranjax.new()
50
+ ranjax.import_text(text)
51
+
52
+ text = ranjax.generate_text()
53
+ assert_instance_of String, text
54
+ end
55
+
56
+ def test_save
57
+ text = @texts[0]
58
+
59
+ ranjax = Ranjax.new()
60
+ ranjax.import_text(text)
61
+
62
+ file = Tempfile.new('test')
63
+ ranjax.save(file.path)
64
+
65
+ content = File.read(file.path)
66
+ assert content.size > 0
67
+ end
68
+
69
+ def test_load
70
+ text = @texts[0]
71
+
72
+ ranjax = Ranjax.new()
73
+ ranjax.import_text(text)
74
+
75
+ file = Tempfile.new('test')
76
+ ranjax.save(file.path)
77
+
78
+ ranjax = Ranjax.new(path: file.path)
79
+ text = ranjax.generate_text()
80
+ assert_instance_of String, text
81
+
82
+ end
83
+
84
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ranjax
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - maruware
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: natto
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Random japanese text generator
28
+ email:
29
+ - maruware@maruware.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - README.md
37
+ - Rakefile
38
+ - lib/ranjax.rb
39
+ - lib/ranjax/version.rb
40
+ - ranjax.gemspec
41
+ - test/test_ranjax.rb
42
+ homepage: https://github.com/maruware/ranjax
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.4.5
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Random japanese text generator
66
+ test_files: []