som2 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- data/lib/som2/version.rb +1 -1
- data/lib/som2.rb +56 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c7b3da5d3e397adf304f81d2a67617a6af0412e
|
4
|
+
data.tar.gz: 8ff9050d0c29ef90f170d64315cda74e4fac9d6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2deb619ad43940ed251ed5ec88a35a2e94bde7a2ae5eb6783cdadc2639ee64351e1c1ae685a838e156627b0c8d2205aed3b95946c8380b9f93dc02346974302e
|
7
|
+
data.tar.gz: 5a21def79fc0ea81d8466193050756cbbe11d278180519e38f26b23610b9bae2fd7819cc506bfd76ac35dde24bf79a9ee55443da22f3d2c5c7e8a90539019f57
|
data/lib/som2/version.rb
CHANGED
data/lib/som2.rb
CHANGED
@@ -1,9 +1,60 @@
|
|
1
1
|
require "som2/version"
|
2
|
-
|
2
|
+
require 'engtagger'
|
3
3
|
module Som2
|
4
4
|
# Your code goes here...
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
ADJECTIVES = %w[so such very much many how].freeze
|
6
|
+
EMOTIONS = %w[wow amaze excite].freeze
|
7
|
+
def initialize
|
8
|
+
@tagger = EngTagger.new
|
9
|
+
end
|
10
|
+
def process(str, options = {})
|
11
|
+
# Parse sentences.
|
12
|
+
sentences = str.downcase.split(/[\.!?]+/).map(&:strip)
|
13
|
+
sentences = sentences.map do |sentence|
|
14
|
+
# Ignore any provided patterns.
|
15
|
+
sentence = ignore_patterns(sentence, options[:ignore]) if options[:ignore]
|
16
|
+
# Select just the nouns.
|
17
|
+
tagged_sentence = tagger.add_tags(sentence)
|
18
|
+
phrases = tagger.get_nouns(tagged_sentence).keys rescue []
|
19
|
+
# Prefix nouns with adjectives, and convert to sentences.
|
20
|
+
phrases.map! { |phrase| correct_spelling(phrase) }
|
21
|
+
phrases.map! { |phrase| "#{adjective} #{phrase}." }
|
22
|
+
# Append a word or phrase describing emotion.
|
23
|
+
phrases << "#{emotional_summary}."
|
24
|
+
phrases.join(' ')
|
25
|
+
end
|
26
|
+
sentences.join(' ')
|
27
|
+
end
|
28
|
+
private
|
29
|
+
attr_reader :tagger
|
30
|
+
def correct_spelling(word)
|
31
|
+
word.dup.tap do |word|
|
32
|
+
word.gsub!(/er$/, 'ar') # super => supar
|
33
|
+
word.gsub!(/ph/, 'f') # phone => fone
|
34
|
+
word.gsub!(/cious/, 'shus') # delicious => delishus, deliciousness => delishusness
|
35
|
+
word.gsub!(/([^s])tion(s$)?/, '\1shun\2') # emotion => emoshun, emotions => emoshuns, emotionless => emoshunless, question (unchanged)
|
36
|
+
word.gsub!(/stion$/, 'schun') # question => queschun, potion (unchanged)
|
37
|
+
word.gsub!(/dog([^e]|\b)/, 'doge\1') # dog => doge, dogs => doges, underdog => underdoge, doge (unchanged)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
def ignore_patterns(sentence, patterns)
|
41
|
+
sentence.dup.tap do |sentence|
|
42
|
+
Array(patterns).map { |pattern| sentence.gsub!(pattern, '') }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
def adjective
|
46
|
+
ADJECTIVES[adjective_offset]
|
47
|
+
end
|
48
|
+
def adjective_offset
|
49
|
+
@adjective_offset ||= -1
|
50
|
+
@adjective_offset = (@adjective_offset + 1) % ADJECTIVES.size
|
51
|
+
end
|
52
|
+
def emotional_summary
|
53
|
+
EMOTIONS[emotional_summary_offset]
|
54
|
+
end
|
55
|
+
def emotional_summary_offset
|
56
|
+
@emotional_summary_offset ||= -1
|
57
|
+
@emotional_summary_offset = (@emotional_summary_offset + 1) % EMOTIONS.size
|
58
|
+
end
|
59
|
+
|
9
60
|
end
|