karamzin 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.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 pavel
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.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Karamzin
2
+
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'karamzin'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install karamzin
17
+
18
+ ## Usage
19
+
20
+ ```ruby
21
+ #=> Karamzin.initialize_dictionaries
22
+ #=> Karamzin.insert 'В лесу родилась елочка!'
23
+ ```
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/karamzin/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/lib/*_test.rb', "test/lib/**/*_test.rb"]
8
+ t.verbose = true
9
+ end
10
+
11
+ task default: :test
data/karamzin.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'karamzin/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "karamzin"
8
+ spec.version = Karamzin::VERSION
9
+ spec.authors = ["Pavel Kalashnikov"]
10
+ spec.email = ["kalashnikovisme@gmail.com"]
11
+ spec.summary = %q{Вставляет букву Ё там, где она нужна.}
12
+ spec.description = %q{Принимает на вход строку и возвращает строку с проставленной буквой Ё именно в тех словах, где она нужна.}
13
+ spec.homepage = "https://github.com/kalashnikovisme/karamzin"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = '>= 2.0.0'
16
+ spec.metadata = { "issue_tracker" => "https://github.com/kalashnikovisme/karamzin/issues" }
17
+ spec.post_install_message = "Карамзин доволен тобой!:)"
18
+
19
+ spec.files = `git ls-files`.split("\n")
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,15 @@
1
+ require 'active_support/core_ext/hash'
2
+
3
+ module Karamzin
4
+ module Config
5
+ class YamlLoader
6
+ def self.gem_root
7
+ Gem::Specification.find_by_name('karamzin').gem_dir
8
+ end
9
+
10
+ def self.yaml_object(filename)
11
+ YAML.load_file(gem_root + '/yaml/' + filename + '.yml').with_indifferent_access
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Karamzin::Config
2
+ autoload :YamlLoader, 'karamzin/config/yaml_loader.rb'
3
+ end
@@ -0,0 +1,36 @@
1
+ module Karamzin
2
+ class Dictionary
3
+ def initialize(name)
4
+ @dictionary = make_dictionary name
5
+ end
6
+
7
+ def [](letter)
8
+ @dictionary[letter]
9
+ end
10
+
11
+ def indexes
12
+ @indexes
13
+ end
14
+
15
+ def make_dictionary(name)
16
+ words = YamlLoader.yaml_object(name)[:words]
17
+ variable = {}
18
+ @indexes = {}
19
+ words.map do |word|
20
+ variable[word[0]] ||= []
21
+ variable[word[0]] << word.split(' ')[0]
22
+ @indexes[word[0]] ||= []
23
+ @indexes[word[0]] << word.split(' ')[1]
24
+ end
25
+ variable
26
+ end
27
+
28
+ def is_in_dictionary(word)
29
+ if @dictionary[word[0].mb_chars.downcase.to_s]
30
+ unless word[0] == YO_LETTER
31
+ @dictionary[word[0].mb_chars.downcase.to_s].index word.mb_chars.downcase.to_s
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,14 @@
1
+ module Karamzin
2
+ class String
3
+ def uppercase(str)
4
+ if str.match /[а-яА-яёЁ].*/
5
+ self.mb_chars.uppercase.to_s
6
+ end
7
+ end
8
+ def downcase(str)
9
+ if str.match /[а-яА-яёЁ].*/
10
+ self.mb_chars.downcase.to_s
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Karamzin
2
+ VERSION = "1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ module Karamzin
2
+ module WordsHelper
3
+ def filter_words(words)
4
+ words.map { |w| w[/[а-яА-ЯЁё\-]*[еЕ][а-яА-ЯЁё\-]*/] }.compact
5
+ end
6
+
7
+ def equate_words_register(wordE, word)
8
+ unless wordE == word
9
+ wordE.split('').each_with_index do |c, i|
10
+ unless wordE[i] == word[i]
11
+ if word[i] == YO_LETTER
12
+ if wordE[i] == BIG_E_LETTER
13
+ word[i] = BIG_YO_LETTER
14
+ else
15
+ next
16
+ end
17
+ else
18
+ word[i] = UnicodeUtils.upcase word[i]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ word
24
+ end
25
+
26
+ def first_letter_in(word)
27
+ letter = word[0].mb_chars.downcase.to_s
28
+ letter == YO_LETTER ? E_LETTER : letter
29
+ end
30
+ end
31
+ end
data/lib/karamzin.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'active_support'
2
+ require 'karamzin/version'
3
+ require 'karamzin/config'
4
+ require 'karamzin/dictionary'
5
+ require 'karamzin/words_helper'
6
+ require 'yaml'
7
+
8
+ module Karamzin
9
+ include WordsHelper
10
+ include Config
11
+
12
+ E_LETTER = 'е'
13
+ YO_LETTER = 'ё'
14
+ BIG_E_LETTER = 'Е'
15
+ BIG_YO_LETTER = 'Ё'
16
+
17
+ def insert(str)
18
+ @dictionary = Dictionary.new 'dictionary'
19
+ words = filter_words str.split
20
+ words.each do |word|
21
+ index_in_dictionary = @dictionary.is_in_dictionary word
22
+ if index_in_dictionary
23
+ word_with_yo = make_word_with_yo(word, index_in_dictionary)
24
+ index = str.index word
25
+ next if index.nil?
26
+ str.gsub! str[index..(index + word.length - 1)], equate_words_register(word, word_with_yo)
27
+ end
28
+ end
29
+ str
30
+ end
31
+
32
+ def make_word_with_yo(word, index_in_dictionary)
33
+ word_with_yo = @dictionary[first_letter_in(word)][index_in_dictionary]
34
+ index = @dictionary.indexes[first_letter_in(word)][index_in_dictionary].to_i
35
+ letter = word[index]
36
+ if letter == E_LETTER
37
+ word_with_yo[index] = YO_LETTER
38
+ elsif letter == BIG_E_LETTER
39
+ word_with_yo[index] = BIG_YO_LETTER
40
+ end
41
+ word_with_yo
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ include Karamzin
4
+
5
+ class KaramzinTest < TestCase
6
+ def test_insert
7
+ assert_equal 'Ёлки, палки', Karamzin.insert('Елки, палки')
8
+ assert_equal 'В лесу родилась ёлочка!', Karamzin.insert('В лесу родилась елочка!')
9
+ assert_equal 'В лесу родилась ёлочка ёлочка!', Karamzin.insert('В лесу родилась елочка елочка!')
10
+ file = File.open("Idiot", "rb")
11
+ contents = file.read
12
+ contents.force_encoding 'utf-8'
13
+ assert Karamzin.insert(contents)
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'bundler/setup'
2
+ if ENV["TRAVIS"]
3
+ require 'coveralls'
4
+ Coveralls.wear!
5
+ end
6
+ require 'simplecov'
7
+ ENV["RAILS_ENV"] = "test"
8
+ SimpleCov.start('rails') if ENV["COVERAGE"]
9
+
10
+ Bundler.require
11
+
12
+ MiniTest.autorun
13
+
14
+ class TestCase < MiniTest::Test
15
+ end