linguify 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.5.1
@@ -51,7 +51,7 @@ module Linguify
51
51
  :regexp => rule[:match].inspect,
52
52
  :args => rule[:match].match(str).to_a[1..-1]
53
53
 
54
- str = reduce_string(str,rule[:match],reduction.to_rexp)
54
+ str = Linguified.reduce_string(str,rule[:match],reduction.to_rexp)
55
55
  break if /^{.*}$/ =~ str
56
56
  end
57
57
 
@@ -92,10 +92,28 @@ module Linguify
92
92
  # @param [ String ] the replacement
93
93
  # @returns [ String ] the reduced string
94
94
  #
95
- def reduce_string str,match_expression,reduction
95
+ def self.reduce_string str,match_expression,reduction
96
96
  match = match_expression.match(str).to_a
97
97
  if match.size == 1
98
- str.gsub(match_expression,reduction)
98
+ needle = match[0]
99
+ splitted = Linguified.informative_split(str,needle)
100
+ prev = ' '
101
+ i = -1
102
+ splitted.map! do |split|
103
+ prev = splitted[i] if i>=0
104
+ nxt = splitted[i+2] || ' '
105
+ i += 1
106
+ if split.kind_of?(Symbol)
107
+ if prev[-1] == ' ' and nxt[0] == ' '
108
+ reduction
109
+ else
110
+ needle
111
+ end
112
+ else
113
+ split
114
+ end
115
+ end
116
+ splitted.join
99
117
  else
100
118
  needle = match[0]
101
119
  splitted = Linguified.informative_split(str,needle)
@@ -113,11 +131,7 @@ module Linguify
113
131
  def self.informative_split str,needle
114
132
  splitted = str.split(needle)
115
133
  if str.index(needle) > 0
116
- if splitted.size & 1 == 0
117
- splitted.map{ |m| [m,:needle] }.flatten[0..-2]
118
- else
119
- splitted.map{ |m| [m,:needle] }.flatten
120
- end
134
+ splitted.map{ |m| [m,:needle] }.flatten[0..splitted.size+str.scan(needle).size-1]
121
135
  else
122
136
  if splitted.size > 0
123
137
  splitted.map{ |m| [m,:needle] }.flatten[1..-2]
data/linguify.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "linguify"
8
- s.version = "0.5.0"
8
+ s.version = "0.5.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Patrick Hanevold"]
@@ -26,17 +26,25 @@ require 'linguify'
26
26
  describe Linguify::Linguified, "#linguify" do
27
27
 
28
28
  it "finds words in sentences" do
29
- Linguify::Linguified.informative_split("I fight for the users","users").should == ["I fight for the ", :needle]
30
- Linguify::Linguified.informative_split("I fight for the users","for the").should == ["I fight ", :needle, " users"]
31
- Linguify::Linguified.informative_split("I fight for the users","t for the u").should == ["I figh", :needle, "sers"]
32
- Linguify::Linguified.informative_split("I fight for the users","I").should == [:needle, " fight for the users"]
29
+ l = Linguify::Linguified
30
+ l.informative_split("I fight for the users","users").should == ["I fight for the ", :needle]
31
+ l.informative_split("I fight for the users","for the").should == ["I fight ", :needle, " users"]
32
+ l.informative_split("I fight for the users","t for the u").should == ["I figh", :needle, "sers"]
33
+ l.informative_split("I fight for the users","I").should == [:needle, " fight for the users"]
34
+ l.informative_split('I fight for the users with email "user@domain.com"',"user").should == ["I fight for the ", :needle, "s with email \"", :needle, "@domain.com\""]
33
35
  end
34
36
 
35
37
  it "respects word boundaries" do
36
- Linguify::Linguified.has_needle_on_word_boundary?(Linguify::Linguified.informative_split("I fight for the users","users")).should == true
37
- Linguify::Linguified.has_needle_on_word_boundary?(Linguify::Linguified.informative_split("I fight for the users","for the")).should == true
38
- Linguify::Linguified.has_needle_on_word_boundary?(Linguify::Linguified.informative_split("I fight for the users","t for the u")).should == false
39
- Linguify::Linguified.has_needle_on_word_boundary?(Linguify::Linguified.informative_split("I fight for the users","I")).should == true
38
+ l = Linguify::Linguified
39
+ l.has_needle_on_word_boundary?(Linguify::Linguified.informative_split("I fight for the users","users")).should == true
40
+ l.has_needle_on_word_boundary?(Linguify::Linguified.informative_split("I fight for the users","for the")).should == true
41
+ l.has_needle_on_word_boundary?(Linguify::Linguified.informative_split("I fight for the users","t for the u")).should == false
42
+ l.has_needle_on_word_boundary?(Linguify::Linguified.informative_split("I fight for the users","I")).should == true
43
+ end
44
+
45
+ it "respects word boundaries on reductions" do
46
+ l = Linguify::Linguified
47
+ l.reduce_string('I fight for a user with email "user@domain.com"',/user/,"{needle}").should == 'I fight for a {needle} with email "user@domain.com"'
40
48
  end
41
49
 
42
50
  it "should reduce multiple rules into ruby code" do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: linguify
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.0
5
+ version: 0.5.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Patrick Hanevold
@@ -115,7 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
115
  requirements:
116
116
  - - ">="
117
117
  - !ruby/object:Gem::Version
118
- hash: 2420952439541187421
118
+ hash: -1602177770022402179
119
119
  segments:
120
120
  - 0
121
121
  version: "0"