langue 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 langue.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Takahiro Kondo
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,57 @@
1
+ What is langue
2
+ ==============
3
+
4
+ It is the foundation to provide the operations to the natural languages.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'langue'
12
+
13
+ # When using Japanese
14
+ gem 'langue-japanese'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install langue
23
+ $ gem install langue-japanese
24
+
25
+ Usage
26
+ -----
27
+
28
+ # coding: utf-8
29
+ require 'langue-japanese'
30
+
31
+ # Get a language class
32
+ language = Langue['japanese'].new
33
+
34
+ # Split to morphemes a text
35
+ morphemes = language.parse('今日は妹と一緒にお買い物してきたよ。楽しかった〜')
36
+
37
+ # Create a structured text from the morphemes
38
+ text = language.structure(morphemes)
39
+
40
+ # When using English
41
+ require 'langue-english'
42
+
43
+ other_language = Langue['english'].new
44
+
45
+ If tiresome to require gem for each language, you will write the code following
46
+ instead:
47
+
48
+ require 'langue/all'
49
+
50
+ Contributing
51
+ ------------
52
+
53
+ 1. Fork it
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/langue/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Takahiro Kondo"]
6
+ gem.email = ["kondo@atedesign.net"]
7
+ gem.description = %q{It is the foundation to provide the operations to the natural languages.}
8
+ gem.summary = %q{The foundation for the natural languages}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "langue"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Langue::VERSION
17
+
18
+ gem.add_runtime_dependency 'activesupport'
19
+
20
+ gem.add_development_dependency 'rspec'
21
+ end
@@ -0,0 +1,47 @@
1
+ require 'langue/version'
2
+ require 'langue/language'
3
+ require 'langue/exceptions'
4
+
5
+ module Langue
6
+ extend self
7
+
8
+ @languages = {}
9
+
10
+ def languages
11
+ @languages.values
12
+ end
13
+
14
+ def support(language_class)
15
+ id = to_language_id(language_class)
16
+ raise LanguageAlreadySupported, "'#{language_class.name}' language is supported already" if @languages.key?(id)
17
+ @languages[id] = language_class
18
+ end
19
+
20
+ def unsupport(language_spec)
21
+ id = to_language_id(language_spec)
22
+ raise unsupported!(language_spec) unless @languages.key?(id)
23
+ @languages.delete(id)
24
+ end
25
+
26
+ def unsupport_all
27
+ @languages.clear
28
+ end
29
+
30
+ def language(language_spec)
31
+ id = to_language_id(language_spec)
32
+ raise unsupported!(language_spec) unless @languages.key?(id)
33
+ @languages[id]
34
+ end
35
+ alias [] language
36
+
37
+ private
38
+
39
+ def to_language_id(language_spec)
40
+ Class === language_spec ? language_spec.id : language_spec.to_s.downcase
41
+ end
42
+
43
+ def unsupported!(language_spec)
44
+ name = Class === language_spec ? language_spec.name : language_spec
45
+ raise LanguageUnsupported, "'#{name}' language is unsupported"
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ Gem::Specification.each do |gem|
2
+ require gem.name if gem.name =~ /^langue-/
3
+ end
@@ -0,0 +1,5 @@
1
+ module Langue
2
+ class InvalidDefinition < StandardError; end
3
+ class LanguageUnsupported < StandardError; end
4
+ class LanguageAlreadySupported < StandardError; end
5
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+
3
+ require 'langue/exceptions'
4
+
5
+ module Langue
6
+ class Language
7
+ class << self
8
+ def id
9
+ parts = name.split('::').reverse.drop_while { |part| part == 'Language' }
10
+ raise InvalidDefinition, "'#{name}' is invalid definition" if parts.empty?
11
+ parts.first.underscore
12
+ end
13
+
14
+ def depend_to(orig_method_name, *gems)
15
+ method_name = "_#{orig_method_name}"
16
+ alias_method(method_name, orig_method_name) unless respond_to?(method_name)
17
+
18
+ define_method(orig_method_name) do |*args|
19
+ gems.each { |gem| require gem }
20
+ value = __send__(method_name, *args)
21
+ self.class.class_eval { alias_method(orig_method_name, method_name) }
22
+ value
23
+ end
24
+ end
25
+ end
26
+
27
+ def initialize(options = {})
28
+ @options = options
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,30 @@
1
+ module Langue
2
+ class Morpheme
3
+ KEYS = %w(
4
+ text
5
+ part_of_speech
6
+ categories
7
+ inflection
8
+ inflection_type
9
+ root_form
10
+ yomi
11
+ pronunciation
12
+ ).map(&:to_sym)
13
+
14
+ def initialize(attrs)
15
+ KEYS.each { |key| instance_variable_set("@#{key}", attrs[key]) }
16
+ end
17
+
18
+ attr_reader(*KEYS)
19
+
20
+ def classified?(part_of_speech, *categories)
21
+ got = [@part_of_speech] + @categories
22
+ expected = [part_of_speech] + categories
23
+ expected.zip(got).all? { |pair| pair[0] == pair[1] }
24
+ end
25
+
26
+ def inflected?(inflection)
27
+ @inflection == inflection
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ require 'langue/morpheme'
2
+
3
+ module Langue
4
+ class Morphemes < Array
5
+ def valid?
6
+ all? { |morpheme| Morpheme === morpheme }
7
+ end
8
+
9
+ def at(index)
10
+ morpheme = self[index]
11
+ morpheme.nil? || !block_given? ? morpheme : yield(morpheme)
12
+ end
13
+
14
+ # def match?(index, text)
15
+ # at(index) {|morpheme| morpheme.text == text}
16
+ # end
17
+
18
+ # def after(index)
19
+ # self[index..-1]
20
+ # end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ require 'langue/morphemes'
2
+ require 'langue/word'
3
+
4
+ module Langue
5
+ class Sentence < Array
6
+ def valid?
7
+ all? { |word| Word === word && word.valid? }
8
+ end
9
+
10
+ def words
11
+ self
12
+ end
13
+
14
+ def morphemes
15
+ @morphemes ||= Morphemes.new(flatten)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ require 'langue/morphemes'
2
+ require 'langue/sentence'
3
+
4
+ module Langue
5
+ class Text < Array
6
+ def valid?
7
+ all? { |sentence| Sentence === sentence && sentence.valid? }
8
+ end
9
+
10
+ def sentences
11
+ self
12
+ end
13
+
14
+ def words
15
+ @words ||= inject(&:+)
16
+ end
17
+
18
+ def morphemes
19
+ @morphemes ||= Morphemes.new(flatten)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Langue
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,64 @@
1
+ require 'langue/morphemes'
2
+ require 'langue/morpheme'
3
+
4
+ module Langue
5
+ class Word < Array
6
+ def valid?
7
+ all? { |morpheme| Morpheme === morpheme }
8
+ end
9
+
10
+ def morphemes
11
+ @morphemes ||= Morphemes.new(self)
12
+ end
13
+
14
+ alias key_morpheme last
15
+
16
+ def text
17
+ @text = not_empty? { map(&:text) } unless instance_variable_defined?(:@text)
18
+ @text
19
+ end
20
+
21
+ def inflection
22
+ @inflection = value_in_key_morpheme(:inflection) unless instance_variable_defined?(:@inflection)
23
+ @inflection
24
+ end
25
+
26
+ def inflection_type
27
+ @inflection_type = value_in_key_morpheme(:inflection_type) unless instance_variable_defined?(:@inflection_type)
28
+ @inflection_type
29
+ end
30
+
31
+ def root_form
32
+ @root_form = not_empty? { self[0..-2].map(&:text) + self[-1, 1].map(&:root_form) } unless instance_variable_defined?(:@root_form)
33
+ @root_form
34
+ end
35
+
36
+ def yomi
37
+ @yomi = join_values(:yomi) unless instance_variable_defined?(:@yomi)
38
+ @yomi
39
+ end
40
+
41
+ def pronunciation
42
+ @pronunciation = join_values(:pronunciation) unless instance_variable_defined?(:@pronunciation)
43
+ @pronunciation
44
+ end
45
+
46
+ private
47
+
48
+ def not_empty?
49
+ empty? ? nil : yield.join
50
+ end
51
+
52
+ def value_in_key_morpheme(key)
53
+ morpheme = key_morpheme
54
+ morpheme.nil? ? nil : morpheme.__send__(key)
55
+ end
56
+
57
+ def join_values(key)
58
+ empty? ? nil : begin
59
+ values = map(&key)
60
+ values.any? { |value| value.nil? } ? nil : values.join
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,46 @@
1
+ require 'langue/language'
2
+
3
+ module Example
4
+ class LanguageExample < Langue::Language
5
+ def parse(text)
6
+ text
7
+ end
8
+ depend_to :parse, 'example/language_example/parser'
9
+ end
10
+
11
+ class Language < Langue::Language
12
+ end
13
+ end
14
+
15
+ class Language < Langue::Language
16
+ end
17
+
18
+ describe Langue::Language, '.id' do
19
+ it 'returns the class name based underscore' do
20
+ Example::LanguageExample.id.should == 'language_example'
21
+ end
22
+
23
+ it 'returns the class name of the parent that are separated by the namespace if the name of the language class is "Language"' do
24
+ Example::Language.id.should == 'example'
25
+ end
26
+
27
+ it 'raises Langue::InvalidDefinition if the name of the language class is "Language" and it is the top level object' do
28
+ lambda { Language.id }.should raise_error(Langue::InvalidDefinition, /'Language'/)
29
+ end
30
+ end
31
+
32
+ describe Langue::Language, '.depend_to' do
33
+ before do
34
+ @language = Example::LanguageExample.new
35
+ end
36
+
37
+ it 'calls require method' do
38
+ @language.should_receive(:require).with('example/language_example/parser')
39
+ @language.parse('text')
40
+ end
41
+
42
+ it 'does not call require method more than once' do
43
+ @language.should_not_receive(:require)
44
+ @language.parse('text')
45
+ end
46
+ end
@@ -0,0 +1,105 @@
1
+ require 'langue/morpheme'
2
+
3
+ describe Langue::Morpheme, ' accessors' do
4
+ before do
5
+ @morpheme = described_class.new(
6
+ :text => 'text',
7
+ :part_of_speech => 'part_of_speech',
8
+ :categories => %w(category1 category2),
9
+ :inflection => 'inflection',
10
+ :inflection_type => 'inflection_type',
11
+ :root_form => 'root_form',
12
+ :yomi => 'yomi',
13
+ :pronunciation => 'pronunciation'
14
+ )
15
+ end
16
+
17
+ it 'returns the text by text method' do
18
+ @morpheme.text.should == 'text'
19
+ end
20
+
21
+ it 'returns the part of speech by part_of_speech method' do
22
+ @morpheme.part_of_speech.should == 'part_of_speech'
23
+ end
24
+
25
+ it 'returns the categories by categories method' do
26
+ @morpheme.categories.should == %w(category1 category2)
27
+ end
28
+
29
+ it 'returns the inflection by inflection method' do
30
+ @morpheme.inflection.should == 'inflection'
31
+ end
32
+
33
+ it 'returns the type of the inflection by inflection_type method' do
34
+ @morpheme.inflection_type.should == 'inflection_type'
35
+ end
36
+
37
+ it 'returns the root form by root_form method' do
38
+ @morpheme.root_form.should == 'root_form'
39
+ end
40
+
41
+ it 'returns the yomi by yomi method' do
42
+ @morpheme.yomi.should == 'yomi'
43
+ end
44
+
45
+ it 'returns the pronunciation by pronunciation method' do
46
+ @morpheme.pronunciation.should == 'pronunciation'
47
+ end
48
+ end
49
+
50
+ describe Langue::Morpheme, '#classified?' do
51
+ before do
52
+ @morpheme = described_class.new(
53
+ :part_of_speech => 'part_of_speech',
54
+ :categories => %w(category1 category2)
55
+ )
56
+ end
57
+
58
+ it 'returns true if part of speech matched' do
59
+ @morpheme.should be_classified('part_of_speech')
60
+ end
61
+
62
+ it 'returns false if part of speech did not match' do
63
+ @morpheme.should_not be_classified('part_0f_speech')
64
+ end
65
+
66
+ context 'with fewer than the categories' do
67
+ it 'returns true if matched' do
68
+ @morpheme.should be_classified('part_of_speech', 'category1')
69
+ end
70
+
71
+ it 'returns false if not matched' do
72
+ @morpheme.should_not be_classified('part_of_speech', 'category10')
73
+ end
74
+ end
75
+
76
+ context 'with equal to the categories' do
77
+ it 'returns true if matched' do
78
+ @morpheme.should be_classified('part_of_speech', 'category1', 'category2')
79
+ end
80
+
81
+ it 'returns false if not matched' do
82
+ @morpheme.should_not be_classified('part_of_speech', 'category1', 'category20')
83
+ end
84
+ end
85
+
86
+ context 'with many than the categories' do
87
+ it 'returns false' do
88
+ @morpheme.should_not be_classified('part_of_speech', 'category1', 'category2', 'category3')
89
+ end
90
+ end
91
+ end
92
+
93
+ describe Langue::Morpheme, '#inflected?' do
94
+ before do
95
+ @morpheme = described_class.new(:inflection => 'inflection')
96
+ end
97
+
98
+ it 'returns true if inflection matched' do
99
+ @morpheme.should be_inflected('inflection')
100
+ end
101
+
102
+ it 'returns false if inflection did not match' do
103
+ @morpheme.should_not be_inflected('inflecti0n')
104
+ end
105
+ end
@@ -0,0 +1,73 @@
1
+ require 'langue/morphemes'
2
+
3
+ describe Langue::Morphemes, '#valid?' do
4
+ before do
5
+ @morphemes = described_class.new([
6
+ Langue::Morpheme.new({}),
7
+ Langue::Morpheme.new({}),
8
+ Langue::Morpheme.new({})
9
+ ])
10
+ end
11
+
12
+ context 'with all morphemes' do
13
+ it 'returns true' do
14
+ @morphemes.should be_valid
15
+ end
16
+ end
17
+
18
+ context 'with including non-morpheme' do
19
+ before do
20
+ @morphemes[1] = 'invalid_as_morpheme'
21
+ end
22
+
23
+ it do
24
+ @morphemes.should_not be_valid
25
+ end
26
+ end
27
+ end
28
+
29
+ describe Langue::Morphemes, '#at' do
30
+ before do
31
+ @morphemes = described_class.new([1, 2, 3])
32
+ end
33
+
34
+ it 'returns a morpheme at the position of the index' do
35
+ morpheme = @morphemes.at(0)
36
+ morpheme.should == 1
37
+ end
38
+
39
+ context 'with do not have a morpheme at the position of the index' do
40
+ it 'returns nil' do
41
+ morpheme = @morphemes.at(3)
42
+ morpheme.should be_nil
43
+ end
44
+ end
45
+
46
+ context 'with a block' do
47
+ it 'passes to the block a morpheme at the position of the index' do
48
+ @morphemes.at(0) { |morpheme| morpheme.should == 1 }
49
+ end
50
+
51
+ it 'returns the value returned from the block' do
52
+ value = @morphemes.at(0) { |morpheme| 'value' }
53
+ value.should == 'value'
54
+ end
55
+
56
+ context 'with do not have a morpheme at the position of the index' do
57
+ it 'does not call the block' do
58
+ lambda { @morphemes.at(3) { |morpheme| fail } }.should_not raise_error
59
+ end
60
+
61
+ it 'returns nil' do
62
+ value = @morphemes.at(3) { |morpheme| fail }
63
+ value.should be_nil
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ # describe Langue::Morphemes, '#match?' do
70
+ # end
71
+
72
+ # describe Langue::Morphemes, '#after' do
73
+ # end
@@ -0,0 +1,57 @@
1
+ require 'langue/sentence'
2
+
3
+ describe Langue::Sentence, '#valid?' do
4
+ before do
5
+ @sentence = described_class.new([
6
+ Langue::Word.new,
7
+ Langue::Word.new,
8
+ Langue::Word.new
9
+ ])
10
+ end
11
+
12
+ context 'with all words' do
13
+ it 'returns true' do
14
+ @sentence.should be_valid
15
+ end
16
+ end
17
+
18
+ context 'with including non-word' do
19
+ before do
20
+ @sentence[1] = 'invalid_as_word'
21
+ end
22
+
23
+ it 'returns false' do
24
+ @sentence.should_not be_valid
25
+ end
26
+ end
27
+ end
28
+
29
+ describe Langue::Sentence, '#words' do
30
+ before do
31
+ sentence = described_class.new([[1, 2], [3, 4]])
32
+ @words = sentence.words
33
+ end
34
+
35
+ it 'returns an instance of Array' do
36
+ @words.should be_an Array
37
+ end
38
+
39
+ it 'returns the words' do
40
+ @words.should == [[1, 2], [3, 4]]
41
+ end
42
+ end
43
+
44
+ describe Langue::Sentence, '#morphemes' do
45
+ before do
46
+ sentence = described_class.new([[1, 2], [3, 4]])
47
+ @morphemes = sentence.morphemes
48
+ end
49
+
50
+ it 'returns an instance of Langue::Morphemes' do
51
+ @morphemes.should be_a Langue::Morphemes
52
+ end
53
+
54
+ it 'returns the morphemes in the words' do
55
+ @morphemes.should == [1, 2, 3, 4]
56
+ end
57
+ end
@@ -0,0 +1,72 @@
1
+ require 'langue/text'
2
+
3
+ describe Langue::Text, '#valid?' do
4
+ before do
5
+ @text = described_class.new([
6
+ Langue::Sentence.new,
7
+ Langue::Sentence.new,
8
+ Langue::Sentence.new
9
+ ])
10
+ end
11
+
12
+ context 'with all sentences' do
13
+ it 'returns true' do
14
+ @text.should be_valid
15
+ end
16
+ end
17
+
18
+ context 'with including non-sentence' do
19
+ before do
20
+ @text[1] = 'invalid_as_sentence'
21
+ end
22
+
23
+ it 'returns false' do
24
+ @text.should_not be_valid
25
+ end
26
+ end
27
+ end
28
+
29
+ describe Langue::Text, '#sentences' do
30
+ before do
31
+ text = described_class.new([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
32
+ @sentences = text.sentences
33
+ end
34
+
35
+ it 'returns an instance of Array' do
36
+ @sentences.should be_an Array
37
+ end
38
+
39
+ it 'returns the sentences' do
40
+ @sentences.should == [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
41
+ end
42
+ end
43
+
44
+ describe Langue::Text, '#words' do
45
+ before do
46
+ text = described_class.new([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
47
+ @words = text.words
48
+ end
49
+
50
+ it 'returns an instance of Array' do
51
+ @words.should be_an Array
52
+ end
53
+
54
+ it 'returns the words in the sentences' do
55
+ @words.should == [[1, 2], [3, 4], [5, 6], [7, 8]]
56
+ end
57
+ end
58
+
59
+ describe Langue::Text, '#morphemes' do
60
+ before do
61
+ text = described_class.new([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
62
+ @morphemes = text.morphemes
63
+ end
64
+
65
+ it 'returns an instance of Langue::Morphemes' do
66
+ @morphemes.should be_a Langue::Morphemes
67
+ end
68
+
69
+ it 'returns the morphemes in the words in the sentences' do
70
+ @morphemes.should == [1, 2, 3, 4, 5, 6, 7, 8]
71
+ end
72
+ end
@@ -0,0 +1,261 @@
1
+ require 'langue/word'
2
+
3
+ describe Langue::Word, '#valid?' do
4
+ before do
5
+ @word = described_class.new([
6
+ Langue::Morpheme.new({}),
7
+ Langue::Morpheme.new({}),
8
+ Langue::Morpheme.new({})
9
+ ])
10
+ end
11
+
12
+ context 'with all morphemes' do
13
+ it 'returns true' do
14
+ @word.should be_valid
15
+ end
16
+ end
17
+
18
+ context 'with including non-morpheme' do
19
+ before do
20
+ @word[1] = 'invalid_as_morpheme'
21
+ end
22
+
23
+ it 'returns false' do
24
+ @word.should_not be_valid
25
+ end
26
+ end
27
+ end
28
+
29
+ describe Langue::Word, '#morphemes' do
30
+ before do
31
+ word = described_class.new([1, 2, 3])
32
+ @morphemes = word.morphemes
33
+ end
34
+
35
+ it 'returns an instance of Langue::Morphemes' do
36
+ @morphemes.should be_a Langue::Morphemes
37
+ end
38
+
39
+ it 'returns ' do
40
+ @morphemes.should == [1, 2, 3]
41
+ end
42
+ end
43
+
44
+ describe Langue::Word, '#key_morpheme' do
45
+ before do
46
+ word = described_class.new([1, 2, 3])
47
+ @morpheme = word.key_morpheme
48
+ end
49
+
50
+ it 'returns the last morpheme' do
51
+ @morpheme.should == 3
52
+ end
53
+
54
+ context 'with do not have morphemes' do
55
+ before do
56
+ word = described_class.new
57
+ @morpheme = word.key_morpheme
58
+ end
59
+
60
+ it 'returns nil' do
61
+ @morpheme.should be_nil
62
+ end
63
+ end
64
+ end
65
+
66
+ describe Langue::Word, '#text' do
67
+ before do
68
+ word = described_class.new([
69
+ stub.tap { |s| s.stub!(:text).and_return('text1') },
70
+ stub.tap { |s| s.stub!(:text).and_return('text2') },
71
+ stub.tap { |s| s.stub!(:text).and_return('text3') }
72
+ ])
73
+
74
+ @text = word.text
75
+ end
76
+
77
+ it 'returns a concatenated string of the text of the morphemes' do
78
+ @text.should == 'text1text2text3'
79
+ end
80
+ end
81
+
82
+ describe Langue::Word, '#inflection' do
83
+ before do
84
+ word = described_class.new([
85
+ stub.tap { |s| s.stub!(:inflection).and_return('inflection1') },
86
+ stub.tap { |s| s.stub!(:inflection).and_return('inflection2') },
87
+ stub.tap { |s| s.stub!(:inflection).and_return('inflection3') }
88
+ ])
89
+
90
+ @inflection = word.inflection
91
+ end
92
+
93
+ it 'returns the inflection of the last morpheme' do
94
+ @inflection.should == 'inflection3'
95
+ end
96
+
97
+ context 'with do not have morphemes' do
98
+ before do
99
+ word = described_class.new
100
+ @inflection = word.inflection
101
+ end
102
+
103
+ it 'returns nil' do
104
+ @inflection.should be_nil
105
+ end
106
+ end
107
+ end
108
+
109
+ describe Langue::Word, '#inflection_type' do
110
+ before do
111
+ word = described_class.new([
112
+ stub.tap { |s| s.stub!(:inflection_type).and_return('inflection_type1') },
113
+ stub.tap { |s| s.stub!(:inflection_type).and_return('inflection_type2') },
114
+ stub.tap { |s| s.stub!(:inflection_type).and_return('inflection_type3') }
115
+ ])
116
+
117
+ @inflection_type = word.inflection_type
118
+ end
119
+
120
+ it 'returns the type of the inflection of the last morpheme' do
121
+ @inflection_type.should == 'inflection_type3'
122
+ end
123
+
124
+ context 'with do not have morphemes' do
125
+ before do
126
+ word = described_class.new
127
+ @inflection_type = word.inflection_type
128
+ end
129
+
130
+ it 'returns nil' do
131
+ @inflection_type.should be_nil
132
+ end
133
+ end
134
+ end
135
+
136
+ describe Langue::Word, '#root_form' do
137
+ before do
138
+ word = described_class.new([
139
+ stub.tap { |s| s.stub!(:text).and_return('text1') },
140
+ stub.tap { |s| s.stub!(:text).and_return('text2') },
141
+ stub.tap { |s| s.stub!(:root_form).and_return('root_form3') }
142
+ ])
143
+
144
+ @root_form = word.root_form
145
+ end
146
+
147
+ it 'makes only the last of the text of the morphemes the root form, and returns a concatenated string' do
148
+ @root_form.should == 'text1text2root_form3'
149
+ end
150
+
151
+ context 'with have a morpheme' do
152
+ before do
153
+ word = described_class.new([
154
+ stub.tap { |s| s.stub!(:root_form).and_return('root_form1') }
155
+ ])
156
+
157
+ @root_form = word.root_form
158
+ end
159
+
160
+ it 'returns the root form text of the morpheme' do
161
+ @root_form.should == 'root_form1'
162
+ end
163
+ end
164
+
165
+ context 'with do not have morphemes' do
166
+ before do
167
+ word = described_class.new
168
+ @root_form = word.root_form
169
+ end
170
+
171
+ it 'returns nil' do
172
+ @root_form.should be_nil
173
+ end
174
+ end
175
+ end
176
+
177
+ describe Langue::Word, '#yomi' do
178
+ before do
179
+ word = described_class.new([
180
+ stub.tap { |s| s.stub!(:yomi).and_return('yomi1') },
181
+ stub.tap { |s| s.stub!(:yomi).and_return('yomi2') },
182
+ stub.tap { |s| s.stub!(:yomi).and_return('yomi3') }
183
+ ])
184
+
185
+ @yomi = word.yomi
186
+ end
187
+
188
+ it 'returns a concatenated string of the yomi of the morphemes' do
189
+ @yomi.should == 'yomi1yomi2yomi3'
190
+ end
191
+
192
+ context 'with have morpheme without a yomi' do
193
+ before do
194
+ word = described_class.new([
195
+ stub.tap { |s| s.stub!(:yomi).and_return('yomi1') },
196
+ stub.tap { |s| s.stub!(:yomi).and_return(nil) },
197
+ stub.tap { |s| s.stub!(:yomi).and_return('yomi3') }
198
+ ])
199
+
200
+ @yomi = word.yomi
201
+ end
202
+
203
+ it 'returns nil' do
204
+ @yomi.should be_nil
205
+ end
206
+ end
207
+
208
+ context 'with do not have morphemes' do
209
+ before do
210
+ word = described_class.new
211
+ @yomi = word.yomi
212
+ end
213
+
214
+ it 'returns nil' do
215
+ @yomi.should be_nil
216
+ end
217
+ end
218
+ end
219
+
220
+ describe Langue::Word, '#pronunciation' do
221
+ before do
222
+ word = described_class.new([
223
+ stub.tap { |s| s.stub!(:pronunciation).and_return('pronunciation1') },
224
+ stub.tap { |s| s.stub!(:pronunciation).and_return('pronunciation2') },
225
+ stub.tap { |s| s.stub!(:pronunciation).and_return('pronunciation3') }
226
+ ])
227
+
228
+ @pronunciation = word.pronunciation
229
+ end
230
+
231
+ it 'returns a concatenated string of the pronunciation of the morphemes' do
232
+ @pronunciation.should == 'pronunciation1pronunciation2pronunciation3'
233
+ end
234
+
235
+ context 'with have morpheme without a pronunciation' do
236
+ before do
237
+ word = described_class.new([
238
+ stub.tap { |s| s.stub!(:pronunciation).and_return('pronunciation1') },
239
+ stub.tap { |s| s.stub!(:pronunciation).and_return(nil) },
240
+ stub.tap { |s| s.stub!(:pronunciation).and_return('pronunciation3') }
241
+ ])
242
+
243
+ @pronunciation = word.pronunciation
244
+ end
245
+
246
+ it 'returns nil' do
247
+ @pronunciation.should be_nil
248
+ end
249
+ end
250
+
251
+ context 'with do not have morphemes' do
252
+ before do
253
+ word = described_class.new
254
+ @pronunciation = word.pronunciation
255
+ end
256
+
257
+ it 'returns nil' do
258
+ @pronunciation.should be_nil
259
+ end
260
+ end
261
+ end
@@ -0,0 +1,134 @@
1
+ require 'langue'
2
+
3
+ class LanguageExample < Langue::Language; end
4
+ class OtherLanguageExample < Langue::Language; end
5
+
6
+ describe Langue do
7
+ it 'is an instance of Module' do
8
+ described_class.should be_an_instance_of Module
9
+ end
10
+
11
+ it 'has VERSION constant' do
12
+ described_class.should be_const_defined :VERSION
13
+ end
14
+ end
15
+
16
+ describe Langue, '.languages' do
17
+ before do
18
+ @languages = described_class.languages
19
+ end
20
+
21
+ it 'returns an instance of Array' do
22
+ @languages.should be_an Array
23
+ end
24
+
25
+ it 'returns an empty array in default' do
26
+ @languages.should be_empty
27
+ end
28
+ end
29
+
30
+ describe Langue, '.support' do
31
+ before do
32
+ described_class.unsupport_all
33
+ described_class.support(LanguageExample)
34
+ end
35
+
36
+ it 'adds to the supported languages' do
37
+ language = described_class.languages.first
38
+ language.should == LanguageExample
39
+ end
40
+
41
+ context 'with already a supported language' do
42
+ it "raises #{described_class}::LanguageAlreadySupported" do
43
+ lambda { described_class.support(LanguageExample) }.should raise_error(Langue::LanguageAlreadySupported, /LanguageExample/)
44
+ end
45
+ end
46
+ end
47
+
48
+ describe Langue, '.unsupport' do
49
+ before do
50
+ described_class.unsupport_all
51
+ described_class.support(LanguageExample)
52
+ described_class.unsupport(LanguageExample)
53
+ end
54
+
55
+ it 'removes from the supported languages' do
56
+ languages = described_class.languages
57
+ languages.should be_empty
58
+ end
59
+
60
+ context 'with not a class' do
61
+ before do
62
+ described_class.unsupport_all
63
+ described_class.support(LanguageExample)
64
+ described_class.unsupport('language_example')
65
+ end
66
+
67
+ it 'removes from the supported languages' do
68
+ languages = described_class.languages
69
+ languages.should be_empty
70
+ end
71
+ end
72
+
73
+ context 'with an unsupported language' do
74
+ before do
75
+ described_class.unsupport_all
76
+ end
77
+
78
+ it "raises #{described_class}::LanguageUnsupported" do
79
+ lambda { described_class.unsupport(OtherLanguageExample) }.should raise_error(Langue::LanguageUnsupported, /OtherLanguageExample/)
80
+ end
81
+
82
+ context 'with not a language class' do
83
+ it "raises #{described_class}::LanguageUnsupported" do
84
+ lambda { described_class.unsupport('other_language_example') }.should raise_error(Langue::LanguageUnsupported, /other_language_example/)
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ describe Langue, '.unsupport_all' do
91
+ it 'removes all languages from the supported languages' do
92
+ described_class.support(LanguageExample)
93
+ described_class.support(OtherLanguageExample)
94
+ described_class.languages.should_not be_empty
95
+ described_class.unsupport_all
96
+ described_class.languages.should be_empty
97
+ end
98
+ end
99
+
100
+ describe Langue, '.language' do
101
+ before do
102
+ described_class.unsupport_all
103
+ described_class.support(LanguageExample)
104
+ end
105
+
106
+ it 'returns the language class' do
107
+ language = described_class.language('language_example')
108
+ language.should == LanguageExample
109
+ end
110
+
111
+ context 'with an unsupported language' do
112
+ it "raises #{described_class}::LanguageUnsupported" do
113
+ lambda { described_class.language('other_language_example') }.should raise_error(Langue::LanguageUnsupported)
114
+ end
115
+ end
116
+ end
117
+
118
+ describe Langue, '.[]' do
119
+ before do
120
+ described_class.unsupport_all
121
+ described_class.support(LanguageExample)
122
+ end
123
+
124
+ it 'returns the language class' do
125
+ language = described_class['language_example']
126
+ language.should == LanguageExample
127
+ end
128
+
129
+ context 'with an unsupported language' do
130
+ it "raises #{described_class}::LanguageUnsupported" do
131
+ lambda { described_class['other_language_example'] }.should raise_error(Langue::LanguageUnsupported)
132
+ end
133
+ end
134
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: langue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Takahiro Kondo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: It is the foundation to provide the operations to the natural languages.
47
+ email:
48
+ - kondo@atedesign.net
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - langue.gemspec
59
+ - lib/langue.rb
60
+ - lib/langue/all.rb
61
+ - lib/langue/exceptions.rb
62
+ - lib/langue/language.rb
63
+ - lib/langue/morpheme.rb
64
+ - lib/langue/morphemes.rb
65
+ - lib/langue/sentence.rb
66
+ - lib/langue/text.rb
67
+ - lib/langue/version.rb
68
+ - lib/langue/word.rb
69
+ - spec/langue/language_spec.rb
70
+ - spec/langue/morpheme_spec.rb
71
+ - spec/langue/morphemes_spec.rb
72
+ - spec/langue/sentence_spec.rb
73
+ - spec/langue/text_spec.rb
74
+ - spec/langue/word_spec.rb
75
+ - spec/langue_spec.rb
76
+ homepage: ''
77
+ licenses: []
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 1.8.21
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: The foundation for the natural languages
100
+ test_files: []
101
+ has_rdoc: