emoji_translate 1.0.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.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +6 -0
- data/assets/emojis.json +16007 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/emoji_translate.gemspec +28 -0
- data/lib/emoji_translate/version.rb +3 -0
- data/lib/emoji_translate.rb +132 -0
- metadata +114 -0
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "emoji_translate"
|
|
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(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "emoji_translate/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "emoji_translate"
|
|
8
|
+
spec.version = EmojiTranslate::VERSION
|
|
9
|
+
spec.authors = ["Vyacheslav Pukhanov"]
|
|
10
|
+
spec.email = ["vyacheslav.pukhanov@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Allows you to 📚 translate text to ✨ emoji ✨}
|
|
13
|
+
spec.homepage = "https://github.com/vpukhanov/emoji_translate"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
|
18
|
+
end
|
|
19
|
+
spec.bindir = "exe"
|
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
21
|
+
spec.require_paths = ["lib"]
|
|
22
|
+
|
|
23
|
+
spec.add_runtime_dependency "json", "~> 2.1"
|
|
24
|
+
|
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
28
|
+
end
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
require 'emoji_translate/version'
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module EmojiTranslate
|
|
6
|
+
@emojis = nil
|
|
7
|
+
|
|
8
|
+
def self.transform_to_array(text)
|
|
9
|
+
text.split ' ' unless text.to_s.empty?
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.clean_up_word(word)
|
|
13
|
+
# Save punctuation from the start and the end of the word
|
|
14
|
+
begin_punctuation = ''
|
|
15
|
+
end_punctuation = ''
|
|
16
|
+
|
|
17
|
+
while !word.empty? && !(word[0] =~ /\W/).nil?
|
|
18
|
+
begin_punctuation += word[0]
|
|
19
|
+
word = word[1..-1]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
while !word.empty? && !(word[word.length - 1] =~ /\W/).nil?
|
|
23
|
+
end_punctuation = word[word.length - 1] + end_punctuation
|
|
24
|
+
word = word[0..-2]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
[begin_punctuation, word, end_punctuation]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.load_emojis
|
|
31
|
+
if @emojis.nil?
|
|
32
|
+
file_path = File.join(File.dirname(__FILE__), '../assets/emojis.json')
|
|
33
|
+
file = File.read file_path
|
|
34
|
+
@emojis = JSON.parse file
|
|
35
|
+
end
|
|
36
|
+
@emojis
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.get_all_possibilities(word)
|
|
40
|
+
# It could be singular word in plural form
|
|
41
|
+
possible_singular = nil
|
|
42
|
+
possible_singular = word[0..-2] if word.length > 2 && word.end_with?('s')
|
|
43
|
+
|
|
44
|
+
# It could be a plural word in singular form
|
|
45
|
+
possible_plural = word.length > 1 ? word + 's' : nil
|
|
46
|
+
|
|
47
|
+
# It could be different verbed form
|
|
48
|
+
possible_verbed_simple = nil
|
|
49
|
+
possible_verbed_vowel = nil
|
|
50
|
+
possible_verbed_doubled = nil
|
|
51
|
+
if word.end_with?('ing')
|
|
52
|
+
verb = word.chomp 'ing'
|
|
53
|
+
|
|
54
|
+
possible_verbed_simple = verb # starting -> start
|
|
55
|
+
possible_verbed_vowel = verb + 'e' # dancing -> dance
|
|
56
|
+
possible_verbed_doubled = verb[0..-2] # beginning -> begin
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
[
|
|
60
|
+
word,
|
|
61
|
+
possible_singular, possible_plural,
|
|
62
|
+
possible_verbed_simple, possible_verbed_vowel, possible_verbed_doubled
|
|
63
|
+
].compact
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.find_by_keyword(keyword)
|
|
67
|
+
self.load_emojis
|
|
68
|
+
|
|
69
|
+
# First, check if it's a common keyword
|
|
70
|
+
if %w{i you}.include? keyword
|
|
71
|
+
return '😊'
|
|
72
|
+
elsif keyword == 'she'
|
|
73
|
+
return '💁'
|
|
74
|
+
elsif keyword == 'he'
|
|
75
|
+
return '💁♂️'
|
|
76
|
+
elsif %w{we they}.include? keyword
|
|
77
|
+
return '👩👩👦👦'
|
|
78
|
+
elsif %w{am is are}.include? keyword
|
|
79
|
+
return '👉'
|
|
80
|
+
elsif keyword == 'and'
|
|
81
|
+
return '➕'
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
possibilities = self.get_all_possibilities(keyword)
|
|
85
|
+
|
|
86
|
+
# Try to find the matching emoji name
|
|
87
|
+
emoji = @emojis.find do |emoji|
|
|
88
|
+
emoji_name, _ = emoji
|
|
89
|
+
possibilities.include? emoji_name
|
|
90
|
+
end
|
|
91
|
+
return emoji[1]['char'] unless emoji.nil?
|
|
92
|
+
|
|
93
|
+
# If no matching name was found, search by keywords
|
|
94
|
+
emoji = @emojis.find do |emoji|
|
|
95
|
+
_, emoji_data = emoji
|
|
96
|
+
!(emoji_data['keywords'] & possibilities).empty?
|
|
97
|
+
end
|
|
98
|
+
return emoji[1]['char'] unless emoji.nil?
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def self.translate_word(word)
|
|
102
|
+
self.load_emojis
|
|
103
|
+
|
|
104
|
+
begin_punctuation, word, end_punctuation = self.clean_up_word(word)
|
|
105
|
+
downcased = word.downcase
|
|
106
|
+
|
|
107
|
+
return begin_punctuation + end_punctuation if word.empty?
|
|
108
|
+
|
|
109
|
+
emoji = self.find_by_keyword downcased
|
|
110
|
+
result = emoji.nil? ? word : emoji
|
|
111
|
+
|
|
112
|
+
begin_punctuation + result + end_punctuation
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def self.translate_line(line)
|
|
116
|
+
self.load_emojis
|
|
117
|
+
|
|
118
|
+
processed_text = self.transform_to_array line
|
|
119
|
+
emojified = processed_text.map { |word| self.translate_word word }
|
|
120
|
+
|
|
121
|
+
emojified.join(' ')
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def self.translate(text)
|
|
125
|
+
self.load_emojis
|
|
126
|
+
|
|
127
|
+
lines = text.lines
|
|
128
|
+
emojified = lines.map { |line| self.translate_line line }
|
|
129
|
+
|
|
130
|
+
emojified.join("\n")
|
|
131
|
+
end
|
|
132
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: emoji_translate
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Vyacheslav Pukhanov
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-08-26 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: json
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.1'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.1'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.15'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.15'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rake
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '10.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '10.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.0'
|
|
69
|
+
description:
|
|
70
|
+
email:
|
|
71
|
+
- vyacheslav.pukhanov@gmail.com
|
|
72
|
+
executables: []
|
|
73
|
+
extensions: []
|
|
74
|
+
extra_rdoc_files: []
|
|
75
|
+
files:
|
|
76
|
+
- ".gitignore"
|
|
77
|
+
- ".rspec"
|
|
78
|
+
- ".rubocop.yml"
|
|
79
|
+
- ".travis.yml"
|
|
80
|
+
- Gemfile
|
|
81
|
+
- LICENSE.txt
|
|
82
|
+
- README.md
|
|
83
|
+
- Rakefile
|
|
84
|
+
- assets/emojis.json
|
|
85
|
+
- bin/console
|
|
86
|
+
- bin/setup
|
|
87
|
+
- emoji_translate.gemspec
|
|
88
|
+
- lib/emoji_translate.rb
|
|
89
|
+
- lib/emoji_translate/version.rb
|
|
90
|
+
homepage: https://github.com/vpukhanov/emoji_translate
|
|
91
|
+
licenses:
|
|
92
|
+
- MIT
|
|
93
|
+
metadata: {}
|
|
94
|
+
post_install_message:
|
|
95
|
+
rdoc_options: []
|
|
96
|
+
require_paths:
|
|
97
|
+
- lib
|
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: '0'
|
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
|
+
requirements:
|
|
105
|
+
- - ">="
|
|
106
|
+
- !ruby/object:Gem::Version
|
|
107
|
+
version: '0'
|
|
108
|
+
requirements: []
|
|
109
|
+
rubyforge_project:
|
|
110
|
+
rubygems_version: 2.6.11
|
|
111
|
+
signing_key:
|
|
112
|
+
specification_version: 4
|
|
113
|
+
summary: "Allows you to \U0001F4DA translate text to ✨ emoji ✨"
|
|
114
|
+
test_files: []
|