├── .gitignore ├── .rspec ├── .travis.yml ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── exe └── rhymer ├── lib ├── rhymer.rb └── rhymer │ ├── cli.rb │ ├── lyric.rb │ ├── parser.rb │ └── version.rb ├── rhymer.gemspec └── spec ├── rhymer ├── cli_spec.rb ├── lyric_spec.rb └── parser_spec.rb ├── rhymer_spec.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --format documentation 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.2.2 4 | before_install: 5 | - base_dir=`pwd` 6 | - wget https://mecab.googlecode.com/files/mecab-0.996.tar.gz 7 | - tar zxfv mecab-0.996.tar.gz 8 | - cd mecab-0.996 9 | - ./configure --enable-utf8-only 10 | - make 11 | - make check 12 | - sudo make install 13 | - sudo ldconfig 14 | - cd $base_dir 15 | - wget https://mecab.googlecode.com/files/mecab-ipadic-2.7.0-20070801.tar.gz 16 | - tar zxfv mecab-ipadic-2.7.0-20070801.tar.gz 17 | - cd mecab-ipadic-2.7.0-20070801 18 | - ./configure --with-charset=utf8 19 | - make 20 | - sudo make install 21 | - sudo ldconfig 22 | - cd $base_dir 23 | - gem install bundler 24 | before_script: 25 | - bundle install 26 | script: 27 | - rspec 28 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in rhymer.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 suzuki86 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rhymer 2 | 3 | 与えられた文章の中から韻を踏んでいるフレーズの組み合わせを見つけ出すライブラリです。 4 | 5 | [![Build Status](https://travis-ci.org/suzuki86/rhymer.svg?branch=master)](https://travis-ci.org/suzuki86/rhymer) 6 | 7 | ## インストール方法 8 | 9 | リポジトリをクローンします。 10 | 11 | ``` 12 | git clone https://github.com/suzuki86/rhymer.git 13 | ``` 14 | 15 | クローンしたディレクトリに移動します。 16 | 17 | ``` 18 | cd rhymer 19 | ``` 20 | 21 | 必要なgemをインストールします。 22 | 23 | ``` 24 | bundle install 25 | ``` 26 | 27 | Gemをビルドします。 28 | 29 | ``` 30 | gem build rhymer.gemspec 31 | ``` 32 | 33 | インストールします。 34 | 35 | ``` 36 | gem install rhymer-x.x.x.gem 37 | ``` 38 | 39 | ## 依存関係 40 | 41 | `natto`、`thor`が利用できる必要があります。 42 | 43 | 44 | ## 使用方法 45 | 46 | `Rhymer::Parser.new`の引数に文章を渡すと、検査結果が含まれたインスタンスが生成されます。インスタンスの`rhymes`メソッドを実行すると、韻を踏んでいるフレーズの組み合わせの配列が返されます。 47 | 48 | ```ruby 49 | require "rhymer" 50 | 51 | rhymer = Rhymer::Parser.new("今日はとても良い天気ですね。こんな日は自然に元気になります。") 52 | rhymer.rhymes.each do |rhyme| 53 | puts [rhyme[0], rhyme[1]].join(" ") 54 | end 55 | ``` 56 | 57 | 上記のコードを実行すると下記の結果が出力されます。 58 | 59 | ``` 60 | 今日は良い天気 こんな日は自然に元気 61 | ``` 62 | 63 | ### CLI 64 | 65 | コマンドラインからも実行できます。 66 | 67 | ``` 68 | rhymer spit 今日はとても良い天気ですね。こんな日は自然に元気になります。 69 | ``` 70 | 71 | ## 開発への参加 72 | 73 | バグの報告やプルリクエストはお気軽にどうぞ。 74 | 75 | ## ライセンス 76 | 77 | [MIT License](http://opensource.org/licenses/MIT) 78 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rspec/core/rake_task" 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "rhymer" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start 15 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | 5 | bundle install 6 | 7 | # Do any other automated setup that you need to do here 8 | -------------------------------------------------------------------------------- /exe/rhymer: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "rhymer/cli" 4 | 5 | Rhymer::CLI.start 6 | -------------------------------------------------------------------------------- /lib/rhymer.rb: -------------------------------------------------------------------------------- 1 | require "natto" 2 | require "rhymer/version" 3 | require "rhymer/lyric" 4 | require "rhymer/parser" 5 | 6 | module Rhymer 7 | end 8 | -------------------------------------------------------------------------------- /lib/rhymer/cli.rb: -------------------------------------------------------------------------------- 1 | require "thor" 2 | require "natto" 3 | require "rhymer/lyric" 4 | require "rhymer/parser" 5 | require "rhymer/version" 6 | 7 | module Rhymer 8 | class CLI < Thor 9 | desc 'spit TEXT', 'Detect and print rhymes in the TEXT' 10 | def spit(text) 11 | parser = Parser.new(text) 12 | parser.rhymes.each do |rhyme| 13 | puts "#{rhyme[0]} #{rhyme[1]}" 14 | end 15 | end 16 | 17 | desc '-v, --version', 'Print the version' 18 | map %w(-v --version) => :version 19 | def version 20 | puts "rhymer #{Rhymer::VERSION}" 21 | end 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /lib/rhymer/lyric.rb: -------------------------------------------------------------------------------- 1 | module Rhymer 2 | class Lyric 3 | attr_reader :lyric 4 | 5 | def initialize(text) 6 | @lyric = [] 7 | mecab.parse(text) do |m| 8 | @lyric << m 9 | end 10 | end 11 | 12 | def mecab 13 | @mecab ||= Natto::MeCab.new 14 | end 15 | 16 | def nouns 17 | nouns = {} 18 | lyric.each_with_index do |term, index| 19 | if term.feature.split(",")[0] == "名詞" 20 | nouns.store(index, term) 21 | end 22 | end 23 | nouns 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/rhymer/parser.rb: -------------------------------------------------------------------------------- 1 | module Rhymer 2 | class Parser 3 | VIBES_THRESHOLD_DEFAULT = 4 4 | PREFIX_LENGTH_DEFAULT = 4 5 | 6 | attr_reader :lyric, :rhymes 7 | 8 | def initialize(text, config = { :vibes_threshold => VIBES_THRESHOLD_DEFAULT, :prefix_length => PREFIX_LENGTH_DEFAULT }) 9 | text = remove_symbols(text) 10 | @lyric = Lyric.new(text) 11 | @rhymes = [] 12 | consonants = [] 13 | 14 | @lyric.nouns.each do |noun| 15 | consonants << { 16 | :position => noun[0], 17 | :noun => romanize(noun[1].feature.split(",")[7]) 18 | } 19 | end 20 | 21 | rhymes = [] 22 | consonants.combination(2) do |arr| 23 | score = vibes(arr.first[:noun], arr.last[:noun]) 24 | if 25 | arr.first[:noun] != arr.last[:noun] && 26 | score > config[:vibes_threshold] 27 | then 28 | rhymes << { 29 | :first => arr.first[:position], 30 | :second => arr.last[:position], 31 | :vibes => score, 32 | } 33 | end 34 | end 35 | 36 | rhymes.each do |rhyme| 37 | prefixes = [[], []] 38 | 39 | %w(first second).each_with_index do |label, n| 40 | counter = 0 41 | (rhyme[label.to_sym] - 1).step(0, -1) do |i| 42 | 43 | parts_of_speech = @lyric.lyric[i].feature.split(",")[0] 44 | kind_of_parts_of_speech = @lyric.lyric[i].feature.split(",")[1] 45 | previous_parts_of_speech = @lyric.lyric[i - 1].feature.split(",")[0] 46 | further_previous_parts_of_speech = @lyric.lyric[i - 2].feature.split(",")[0] 47 | 48 | if prefixes[n].length > 0 49 | latest_stored_parts_of_speech = prefixes[n][0][:term].feature.split(",")[0] 50 | end 51 | 52 | if n > 1 && i <= rhyme[:first] 53 | break 54 | end 55 | 56 | if 57 | counter > config[:prefix_length] && 58 | latest_stored_parts_of_speech != "助動詞" && 59 | latest_stored_parts_of_speech != "助詞" && 60 | latest_stored_parts_of_speech != "動詞" && 61 | previous_parts_of_speech != "名詞" && 62 | previous_parts_of_speech != "連体詞" && 63 | parts_of_speech != "名詞" && 64 | parts_of_speech != "連体詞" 65 | then 66 | break 67 | end 68 | 69 | if 70 | kind_of_parts_of_speech == "句点" || 71 | kind_of_parts_of_speech == "読点" 72 | then 73 | break 74 | end 75 | 76 | if 77 | parts_of_speech == "名詞" || 78 | parts_of_speech == "接頭詞" || 79 | parts_of_speech == "動詞" || 80 | parts_of_speech == "接続詞" || 81 | parts_of_speech == "形容詞" || 82 | parts_of_speech == "連体詞" 83 | then 84 | prefixes[n].unshift({ 85 | :position => i, 86 | :term => @lyric.lyric[i] 87 | }) 88 | end 89 | 90 | if 91 | ( 92 | parts_of_speech == "助詞" && 93 | previous_parts_of_speech != "記号" 94 | ) || 95 | ( 96 | parts_of_speech == "助動詞" && 97 | previous_parts_of_speech != "記号" 98 | ) 99 | then 100 | prefixes[n].unshift({ 101 | :position => i, 102 | :term => @lyric.lyric[i] 103 | }) 104 | end 105 | 106 | if 107 | previous_parts_of_speech != "名詞" && 108 | previous_parts_of_speech != "動詞" && 109 | previous_parts_of_speech != "連体詞" && 110 | further_previous_parts_of_speech == "記号" 111 | then 112 | break 113 | end 114 | 115 | counter = counter + 1 116 | end 117 | 118 | end 119 | 120 | prefix_surfaces = ["", ""] 121 | prefixes.each_with_index do |prefix, i| 122 | prefix.each do |p| 123 | prefix_surfaces[i] = prefix_surfaces[i] + p[:term].surface 124 | end 125 | end 126 | 127 | if prefix_surfaces[0].empty? || prefix_surfaces[1].empty? 128 | next 129 | end 130 | 131 | @rhymes << [ 132 | prefix_surfaces[0] + @lyric.lyric[rhyme[:first]].surface, 133 | prefix_surfaces[1] + @lyric.lyric[rhyme[:second]].surface, 134 | rhyme[:vibes] 135 | ] 136 | end 137 | 138 | @rhymes = @rhymes.sort_by {|a, b, c| c }.reverse 139 | end 140 | 141 | def remove_html(text) 142 | text.gsub(/<\/?[^>]*>/, "") 143 | end 144 | 145 | def remove_symbols(text) 146 | text.gsub(/\[.+?\]/, "") 147 | end 148 | 149 | def romanize(term) 150 | { 151 | "キャ" => "kya", 152 | "キュ" => "kyu", 153 | "キョ" => "kyo", 154 | "シャ" => "sya", 155 | "シュ" => "syu", 156 | "ショ" => "syo", 157 | "チャ" => "tya", 158 | "チュ" => "tyu", 159 | "チョ" => "tyo", 160 | "ニャ" => "nya", 161 | "ニュ" => "nyu", 162 | "ニョ" => "nyo", 163 | "ヒャ" => "hya", 164 | "ヒュ" => "hyu", 165 | "ヒョ" => "hyo", 166 | "ミャ" => "mya", 167 | "ミュ" => "myu", 168 | "ミョ" => "myo", 169 | "リャ" => "rya", 170 | "リュ" => "ryu", 171 | "リョ" => "ryo", 172 | "ギャ" => "gya", 173 | "ギュ" => "gyu", 174 | "ギョ" => "gyo", 175 | "ジャ" => "jya", 176 | "ジュ" => "jyu", 177 | "ジョ" => "jyo", 178 | "ビャ" => "bya", 179 | "ビュ" => "byu", 180 | "ビョ" => "byo", 181 | "ピャ" => "pya", 182 | "ピュ" => "pyu", 183 | "ピョ" => "pyo", 184 | "カ" => "ka", 185 | "キ" => "ki", 186 | "ク" => "ku", 187 | "ケ" => "ke", 188 | "コ" => "ko", 189 | "サ" => "sa", 190 | "シ" => "si", 191 | "ス" => "su", 192 | "セ" => "se", 193 | "ソ" => "so", 194 | "タ" => "ta", 195 | "チ" => "ti", 196 | "ツ" => "tu", 197 | "テ" => "te", 198 | "ト" => "to", 199 | "ナ" => "na", 200 | "ニ" => "ni", 201 | "ヌ" => "nu", 202 | "ネ" => "ne", 203 | "ノ" => "no", 204 | "ハ" => "ha", 205 | "ヒ" => "hi", 206 | "フ" => "hu", 207 | "ヘ" => "he", 208 | "ホ" => "ho", 209 | "マ" => "ma", 210 | "ミ" => "mi", 211 | "ム" => "mu", 212 | "メ" => "me", 213 | "モ" => "mo", 214 | "ヤ" => "ya", 215 | "ユ" => "yu", 216 | "ヨ" => "yo", 217 | "ラ" => "ra", 218 | "リ" => "ri", 219 | "ル" => "ru", 220 | "レ" => "re", 221 | "ロ" => "ro", 222 | "ワ" => "wa", 223 | "ヲ" => "wo", 224 | "ガ" => "ga", 225 | "ギ" => "gi", 226 | "グ" => "gu", 227 | "ゲ" => "ge", 228 | "ゴ" => "go", 229 | "ザ" => "za", 230 | "ジ" => "zi", 231 | "ズ" => "zu", 232 | "ゼ" => "ze", 233 | "ゾ" => "zo", 234 | "ダ" => "da", 235 | "ヂ" => "di", 236 | "ヅ" => "du", 237 | "デ" => "de", 238 | "ド" => "do", 239 | "バ" => "ba", 240 | "ビ" => "bi", 241 | "ブ" => "bu", 242 | "ベ" => "be", 243 | "ボ" => "bo", 244 | "パ" => "pa", 245 | "ピ" => "pi", 246 | "プ" => "pu", 247 | "ペ" => "pe", 248 | "ポ" => "po", 249 | "ア" => "a", 250 | "イ" => "i", 251 | "ウ" => "u", 252 | "エ" => "e", 253 | "オ" => "o", 254 | "ン" => "#", 255 | "ッ" => "*", 256 | }.each do |key, value| 257 | term = term.to_s.gsub(Regexp.new(key), value) 258 | end 259 | term 260 | end 261 | 262 | def replace_consonant(romanized_term) 263 | romanized_term.gsub(/[bcdfghjklmnpqrstvwxyz]/, "x") 264 | end 265 | 266 | def extract_vowel(romanized_term) 267 | romanized_term.gsub(/[bcdfghjklmnpqrstvwxyz]/, "") 268 | end 269 | 270 | def vibes(a, b) 271 | score = 0 272 | 273 | a = replace_consonant(a) 274 | b = replace_consonant(b) 275 | 276 | if extract_vowel(a).length < 3 || extract_vowel(b).length < 3 277 | return 0 278 | end 279 | 280 | if a[-2..-1] != b[-2..-1] 281 | return 0 282 | end 283 | 284 | if a[-4..-1] == b[-4..-1] 285 | score = score + 2 286 | end 287 | 288 | if a[-5..-1] == b[-5..-1] 289 | score = score + 4 290 | end 291 | 292 | if a[-6..-1] == b[-6..-1] 293 | score = score + 6 294 | end 295 | 296 | if extract_vowel(a) == extract_vowel(b) 297 | score = score + 8 298 | end 299 | 300 | score 301 | end 302 | end 303 | end 304 | -------------------------------------------------------------------------------- /lib/rhymer/version.rb: -------------------------------------------------------------------------------- 1 | module Rhymer 2 | VERSION = "0.0.4" 3 | end 4 | -------------------------------------------------------------------------------- /rhymer.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'rhymer/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "rhymer" 8 | spec.version = Rhymer::VERSION 9 | spec.authors = ["suzuki86"] 10 | spec.email = ["tsnr0001@gmail.com"] 11 | 12 | spec.summary = %q{Find rhyme from text.} 13 | spec.description = %q{Find rhyme from text.} 14 | spec.homepage = "https://github.com/suzuki86/rhymer" 15 | spec.license = "MIT" 16 | 17 | # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or 18 | # delete this section to allow pushing this gem to any host. 19 | if spec.respond_to?(:metadata) 20 | spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'" 21 | else 22 | raise "RubyGems 2.0 or newer is required to protect against public gem pushes." 23 | end 24 | 25 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 26 | spec.bindir = "exe" 27 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 28 | spec.require_paths = ["lib"] 29 | 30 | spec.add_runtime_dependency "natto", "~> 1.1.0" 31 | spec.add_runtime_dependency "thor", "~> 0.19.1" 32 | spec.add_development_dependency "rspec", "~> 3.4.0" 33 | spec.add_development_dependency "simplecov", "~> 0.12.0" 34 | end 35 | -------------------------------------------------------------------------------- /spec/rhymer/cli_spec.rb: -------------------------------------------------------------------------------- 1 | describe Rhymer::CLI do 2 | let(:cli) { described_class.new } 3 | 4 | describe '#spit' do 5 | context 'given text' do 6 | let(:text) { '今日はとても良い天気ですね。こんな日は自然に元気になります。' } 7 | 8 | subject { -> { cli.spit(text) } } 9 | it { is_expected.to output("今日は良い天気 こんな日は自然に元気\n").to_stdout } 10 | end 11 | end 12 | 13 | describe '#version' do 14 | subject { -> { cli.version } } 15 | it { is_expected.to output("rhymer #{Rhymer::VERSION}\n").to_stdout } 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /spec/rhymer/lyric_spec.rb: -------------------------------------------------------------------------------- 1 | describe Rhymer::Lyric do 2 | let(:text) { '今日はとても良い天気ですね。' } 3 | let(:lyric) { described_class.new(text) } 4 | 5 | describe '.new' do 6 | context 'given text' do 7 | subject { lyric } 8 | it { is_expected.to be_instance_of described_class } 9 | it { is_expected.to respond_to(:lyric) } 10 | it { is_expected.not_to respond_to(:lyric=) } 11 | end 12 | 13 | context 'given nil text' do 14 | let(:text) { nil } 15 | 16 | subject { -> { lyric } } 17 | it { is_expected.to raise_error(ArgumentError, 'Text to parse cannot be nil') } 18 | end 19 | end 20 | 21 | describe '#lyric' do 22 | subject { lyric.lyric } 23 | it { is_expected.to include an_instance_of(Natto::MeCabNode) } 24 | end 25 | 26 | describe '#mecab' do 27 | subject { lyric.mecab } 28 | it { is_expected.to be_instance_of Natto::MeCab } 29 | end 30 | 31 | describe '#nouns' do 32 | let(:nouns) do 33 | { 34 | 0 => an_instance_of(Natto::MeCabNode), 35 | 4 => an_instance_of(Natto::MeCabNode) 36 | } 37 | end 38 | 39 | subject { lyric.nouns } 40 | it { is_expected.to include nouns } 41 | 42 | describe '#[0]' do 43 | describe '#feature' do 44 | subject { lyric.nouns[0].feature } 45 | it { is_expected.to match /\A名詞/ } 46 | end 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /spec/rhymer/parser_spec.rb: -------------------------------------------------------------------------------- 1 | describe Rhymer::Parser do 2 | let(:text) { '今日はとても良い天気ですね。こんな日は自然に元気になります。' } 3 | let(:parser) { described_class.new(text) } 4 | 5 | describe '.new' do 6 | context 'given text' do 7 | subject { parser } 8 | it { is_expected.to be_instance_of described_class } 9 | it { is_expected.to respond_to(:lyric) } 10 | it { is_expected.not_to respond_to(:lyric=) } 11 | it { is_expected.to respond_to(:rhymes) } 12 | it { is_expected.not_to respond_to(:rhymes=) } 13 | end 14 | 15 | context 'given nil text' do 16 | let(:text) { nil } 17 | 18 | subject { -> { parser } } 19 | it { is_expected.to raise_error(NoMethodError, "undefined method `gsub' for nil:NilClass") } 20 | end 21 | end 22 | 23 | describe '#lyric' do 24 | subject { parser.lyric } 25 | it { is_expected.to be_instance_of Rhymer::Lyric } 26 | end 27 | 28 | describe '#rhymes' do 29 | subject { parser.rhymes } 30 | 31 | context 'with default config' do 32 | context 'given rhymed text' do 33 | it { is_expected.to eq [['今日は良い天気', 'こんな日は自然に元気', 20]] } 34 | end 35 | 36 | context 'given no rhymed text' do 37 | let(:text) { '今日はとても良い天気ですね。' } 38 | it { is_expected.to eq [] } 39 | end 40 | end 41 | 42 | context 'with custom config' do 43 | let(:parser) { described_class.new(text, config) } 44 | 45 | context 'given low config[:vibes_threshold]' do 46 | let(:config) { { vibes_threshold: 19, prefix_length: 4 } } 47 | it { is_expected.to eq [['今日は良い天気', 'こんな日は自然に元気', 20]] } 48 | end 49 | 50 | context 'given high config[:vibes_threshold]' do 51 | let(:config) { { vibes_threshold: 20, prefix_length: 4 } } 52 | it { is_expected.to eq [] } 53 | end 54 | 55 | context 'given config[:prefix_length]' do 56 | let(:config) { { vibes_threshold: 4, prefix_length: 0 } } 57 | it { is_expected.to eq [['良い天気', 'こんな日は自然に元気', 20]] } 58 | end 59 | end 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /spec/rhymer_spec.rb: -------------------------------------------------------------------------------- 1 | describe Rhymer do 2 | describe 'VERSION' do 3 | subject { described_class::VERSION } 4 | it { is_expected.not_to be_nil } 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'simplecov' 2 | 3 | SimpleCov.start do 4 | add_filter '/spec/' 5 | end 6 | 7 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) 8 | require 'rhymer' 9 | require 'rhymer/cli' 10 | --------------------------------------------------------------------------------