keyword_finder 0.2.3 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eab7bc8c2075c2d8089e5bfe3b566d3788edb24a
4
- data.tar.gz: 5b85855956903931d51340dc2e86fd9464db6972
3
+ metadata.gz: a6eb718b2a534b20dca7e09ee120890ec77bfa1b
4
+ data.tar.gz: cb754ff99ce1064afeeac4d26276b3962cbd849d
5
5
  SHA512:
6
- metadata.gz: 484751134fd4a1aee65f8d3f1cb2cf1b9715fa811fb59b1ac6ac1bd7642a1f99e097754f2237d20a7611bc0b3049e075b4d204380a4ec938d596457106486552
7
- data.tar.gz: 0d829de6a51ea7f815329e2801d0451fd81cf9862dd19d913f489c5aacb3b91181a02b9f229fc36264a0f203741557d59d1103cfc5909806660629994140b6b5
6
+ metadata.gz: ecadae7d7b8976ffc1546b34781218d3f573d30491f6501ae5b140931cf4a31516964a4eff2bb63af1bf31b45cc2b98000608dc1f9689e72132a573eb3838657
7
+ data.tar.gz: a6de7e1c5efdc3f3dc960420ba66ab31d7940cdbd616f82c006241e65382f1320098aa149840adb7a76ca44ef862a9a803a6b153a7587aabb710f7d5687940c7
@@ -6,14 +6,18 @@ module KeywordFinder
6
6
  def escape_regex_chars string
7
7
  Regexp.escape(string).downcase
8
8
  end
9
- def to_regex
10
- @to_regex ||= Regexp.new("(#{
11
- self.ordered_by_length.collect{|a| "\\s#{self.escape_regex_chars(a.gsub(' ', ' '))}\\s"}.join("|")
9
+
10
+ def to_regex(options={})
11
+ options = {entire_words_only: true}.merge(options)
12
+ spacer = options[:entire_words_only] ? "\\s" : ""
13
+ @to_regex = {} unless defined?(@to_regex)
14
+ @to_regex[options[:entire_words_only]] ||= Regexp.new("(#{
15
+ self.ordered_by_length.collect{|a| "#{spacer}#{self.escape_regex_chars(a.gsub(' ', ' '))}#{spacer}"}.join("|")
12
16
  })")
13
17
  end
14
18
 
15
- def scan_in sentence
16
- " #{sentence} ".scan(self.to_regex)
19
+ def scan_in sentence, options={}
20
+ " #{sentence} ".scan(self.to_regex(options))
17
21
  end
18
22
 
19
23
  def clean_sentence sentence
@@ -26,8 +30,8 @@ module KeywordFinder
26
30
  gsub(/([A-Za-z]+\([A-Za-z]*\)[A-Za-z]*)/) { |s| s.gsub(/(\(|\))/,'') }
27
31
  end
28
32
 
29
- def scan_part sentence
30
- scan_results = self.scan_in(self.clean_sentence(sentence))
33
+ def scan_part sentence, options={}
34
+ scan_results = self.scan_in(self.clean_sentence(sentence), options)
31
35
  scan_results.flatten!
32
36
  scan_results.uniq!
33
37
  scan_results.compact!
@@ -42,27 +46,28 @@ module KeywordFinder
42
46
  #
43
47
  #
44
48
  # @param [String] sentence that might contain the keywords this instance was initalized with
45
- # @param [Hash] options; notably the +:subsentences_strategy+, which can be one of +:none+, +:ignore_if_found_in_main+, +:always_ignore+
49
+ # @param [Hash] options; notably the +:subsentences_strategy+, which can be one of +:none+, +:ignore_if_found_in_main+, +:always_ignore+ and the +:entire_words_only+ boolean, which can be either +true+ or +false+
46
50
 
47
51
  def find_in sentence, options={}
48
52
  options = {
49
- subsentences_strategy: :none # :none, :ignore_if_found_in_main, :always_ignore
53
+ subsentences_strategy: :none, # :none, :ignore_if_found_in_main, :always_ignore
54
+ entire_words_only: true
50
55
  }.merge(options)
51
56
 
52
57
  sentence = sentence.downcase.gsub(/\n/," ")
53
58
 
54
- full_sentence_results = self.scan_part(sentence)
59
+ full_sentence_results = self.scan_part(sentence, options)
55
60
 
56
61
  sentence = self.combine_more_specifics(sentence)
57
62
  main_and_subs = self.separate_main_and_sub_sentences(sentence)
58
- main_results = self.scan_part(main_and_subs[:main])
63
+ main_results = self.scan_part(main_and_subs[:main], options)
59
64
 
60
65
  sub_results = []
61
66
  unless (
62
67
  options[:subsentences_strategy] == :always_ignore or
63
68
  (main_results.count > 0 and options[:subsentences_strategy] == :ignore_if_found_in_main)
64
69
  )
65
- sub_results = main_and_subs[:subs].collect{|subsentence| self.scan_part(subsentence)}.flatten
70
+ sub_results = main_and_subs[:subs].collect{|subsentence| self.scan_part(subsentence, options)}.flatten
66
71
  end
67
72
 
68
73
  clean_sentence_results = main_results + sub_results
@@ -1,3 +1,3 @@
1
1
  module KeywordFinder
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keyword_finder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - murb
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-25 00:00:00.000000000 Z
11
+ date: 2017-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,8 +94,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  version: '0'
95
95
  requirements: []
96
96
  rubyforge_project:
97
- rubygems_version: 2.5.1
97
+ rubygems_version: 2.4.5
98
98
  signing_key:
99
99
  specification_version: 4
100
100
  summary: Find given set of keywords in a sentence.
101
101
  test_files: []
102
+ has_rdoc: