pingilish 0.1.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 +15 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/db/char_maps.yml +24 -0
- data/db/pingilish_words.yml +9 -0
- data/lib/pingilish.rb +99 -0
- data/lib/pingilish/backend_manager.rb +19 -0
- data/lib/pingilish/backends/yaml.rb +45 -0
- data/lib/pingilish/stem_filter.rb +3 -0
- data/lib/pingilish/tokenizer.rb +11 -0
- data/lib/pingilish/version.rb +3 -0
- data/pingilish.gemspec +26 -0
- metadata +104 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
!binary "U0hBMQ==":
|
|
3
|
+
metadata.gz: !binary |-
|
|
4
|
+
ZDIyMDgzNzQ2OTI3YTcwM2Q1ZmVlNzhmNzlkZGQ2MGFhMDgwZWZkZQ==
|
|
5
|
+
data.tar.gz: !binary |-
|
|
6
|
+
OWQwMzE0YmVkNzU1Y2M4NWQ5NDg0NTU3YzdmODVkMDFjMmM4MjNiYw==
|
|
7
|
+
SHA512:
|
|
8
|
+
metadata.gz: !binary |-
|
|
9
|
+
ZjgzN2I3NDg5OGI1NDA0NTUwMDlmM2RkMTc5ZGViOWM0Y2I2Y2RkZmNkNDdl
|
|
10
|
+
MDMzOWQ2OTE5NzgwZTAwYTlmMjhlYThmM2VmNmI0ZTdkNTFjOTVjYmRkMmQ3
|
|
11
|
+
N2FmMjkxMTc2ZTc4NTk2MzU0YmExNTZhZWE2YzdhNTMxYWM1NjI=
|
|
12
|
+
data.tar.gz: !binary |-
|
|
13
|
+
Mzk0MGNmNzg3YmM1ZTA4ZTFiYTJhZDNmNGE0OGJjNzUwZjgwNmQ0ZWM2ZTc3
|
|
14
|
+
MWY2YjY0ZDhjNzk4NDI3MTE4ZjI0ODhmYTg4OTQ5N2UwZTJmZmI1NDM1MGZi
|
|
15
|
+
MjBlZWM5NjQxYjJiYWY0NjcwMzMyNjQ4MjlhOGZmNjQyNWM2ZmQ=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 tokhi
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Pingilish
|
|
2
|
+
|
|
3
|
+
pinglish gem converts Persian/farsi text to english and vice versa.
|
|
4
|
+
|
|
5
|
+
This gem is an improvements of the efforts made by [Aziz](https://github.com/aziz/pingilish.git).
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Add this line to your application's Gemfile:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
gem 'pingilish'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
And then execute:
|
|
17
|
+
|
|
18
|
+
$ bundle
|
|
19
|
+
|
|
20
|
+
Or install it yourself as:
|
|
21
|
+
|
|
22
|
+
$ gem install pingilish
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
# farsi to english
|
|
28
|
+
Pingilish.to_default("توانا بود هرکه دانا بود")
|
|
29
|
+
=> "toana bod hrkh dana bod"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# english to farsi
|
|
33
|
+
Pingilish.to_farsi("nabordeh ranj ganj moiasar namishawad")
|
|
34
|
+
=> "نبرده رنج گنج میسر نمیشود "
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Development
|
|
38
|
+
|
|
39
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
40
|
+
|
|
41
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
42
|
+
|
|
43
|
+
## Contributing
|
|
44
|
+
|
|
45
|
+
This gem requires a lot of improvments so bug reports and pull requests are welcome.
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## License
|
|
49
|
+
|
|
50
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
51
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "pingilish"
|
|
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
|
data/bin/setup
ADDED
data/db/char_maps.yml
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
آ: 'aa'
|
|
3
|
+
چ: 'ch'
|
|
4
|
+
خ: 'kh'
|
|
5
|
+
ژ: 'zh'
|
|
6
|
+
ش: 'sh'
|
|
7
|
+
ع: "a'"
|
|
8
|
+
غ: 'gh'
|
|
9
|
+
ق: 'gh'
|
|
10
|
+
o: ' va '
|
|
11
|
+
o: ' v'
|
|
12
|
+
هٔ: 'eye'
|
|
13
|
+
ه: '_H_'
|
|
14
|
+
و: 'o'
|
|
15
|
+
ی: 'i'
|
|
16
|
+
_H_: 'e '
|
|
17
|
+
_H_: 'h'
|
|
18
|
+
i: ' y'
|
|
19
|
+
i: 'y '
|
|
20
|
+
َ: 'a'
|
|
21
|
+
ِ: 'e'
|
|
22
|
+
ُ: 'o'
|
|
23
|
+
ً: 'n'
|
|
24
|
+
ء: "'"
|
data/lib/pingilish.rb
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require "pingilish/version"
|
|
3
|
+
require 'yaml'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
module Pingilish
|
|
7
|
+
def self.to_farsi(str)
|
|
8
|
+
pin = Farsi.new(str)
|
|
9
|
+
pin.to_farsi.force_encoding('UTF-8')
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.to_default(text)
|
|
13
|
+
db_filter = DbFilter.new(text)
|
|
14
|
+
text = db_filter.process
|
|
15
|
+
|
|
16
|
+
# stem_filter = StemFilter.new(text)
|
|
17
|
+
# text = stem_filter.process
|
|
18
|
+
|
|
19
|
+
mapping_filter = MappingFilter.new(text)
|
|
20
|
+
text = mapping_filter.process
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class MappingFilter
|
|
24
|
+
PERSIAN_CHARS = "۱۲۳۴۵۶۷۸۹۰،×؛ابپتثجحدذرزسصضطظفکگلمنك؟"
|
|
25
|
+
ENGLISH_CHARS = "1234567890,*;abptsjhdzrzssztzfkglmnk?"
|
|
26
|
+
|
|
27
|
+
CHAR_MAP_FILE_PATH = File.join(File.dirname(__FILE__), '../db/char_maps.yml')
|
|
28
|
+
CHAR_MAP = YAML.load_file(CHAR_MAP_FILE_PATH)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def initialize(text)
|
|
32
|
+
@text = text
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def process
|
|
36
|
+
# general character replacement
|
|
37
|
+
text = @text.to_s.tr(PERSIAN_CHARS,ENGLISH_CHARS)
|
|
38
|
+
|
|
39
|
+
CHAR_MAP.each { |k, v| text.gsub!("#{k}","#{v}") }
|
|
40
|
+
return text
|
|
41
|
+
# TODO: make a mapping hash instead of this dirty gsub ing
|
|
42
|
+
# TODO: replace non latin alphabet at the end
|
|
43
|
+
# TODO: return an object with alternatives (like chert/chort)
|
|
44
|
+
# TODO: seperate the login into classes: Tokenizer, StemmingFilter, DbFilter, GeneralMappingFilter
|
|
45
|
+
# TODO: seperate the logic of dealing with a backend in a class, subclass it and write something for YML
|
|
46
|
+
# BUG: major bug, do not replace latin chars that came from DB by latin chars in mapping array!
|
|
47
|
+
# TODO: write the mapping filter interations
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class DbFilter
|
|
54
|
+
require 'pingilish/backends/yaml'
|
|
55
|
+
require 'pingilish/tokenizer'
|
|
56
|
+
|
|
57
|
+
def initialize(text)
|
|
58
|
+
@text = text.gsub(/ي/,"ی").gsub(/ك/,"ک") # dealing with non-standard characters
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def process
|
|
62
|
+
text = @text
|
|
63
|
+
y_db = YamlBackend.new
|
|
64
|
+
words = Tokenizer.new(@text)
|
|
65
|
+
|
|
66
|
+
db = y_db.load
|
|
67
|
+
words.tokens.each do |w|
|
|
68
|
+
text = text.gsub(/#{w}/, db[w].to_s) if db[w]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
return text
|
|
72
|
+
# BUG: do not gsub just replace the text
|
|
73
|
+
# BUG: case for hash and string
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
require 'net/http'
|
|
79
|
+
require 'uri'
|
|
80
|
+
|
|
81
|
+
class Farsi < String
|
|
82
|
+
|
|
83
|
+
TO_FARSI_SERVICE_URL = "http://www.behnevis.com/php/convert.php"
|
|
84
|
+
TO_FARSI_QUERY_STRING = 'farsi'
|
|
85
|
+
|
|
86
|
+
def initialize(text)
|
|
87
|
+
@text = text
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def to_farsi
|
|
91
|
+
Net::HTTP.post_form(URI.parse(TO_FARSI_SERVICE_URL), {TO_FARSI_QUERY_STRING => @text.downcase }).body
|
|
92
|
+
# TODO: save the files in yaml
|
|
93
|
+
# TODO: verify that it's persian
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'yaml'
|
|
2
|
+
require 'rubygems'
|
|
3
|
+
|
|
4
|
+
class YamlBackend
|
|
5
|
+
|
|
6
|
+
DB_FILE_PATH = File.join(File.dirname(__FILE__), '../../../db/pingilish_words.yml')
|
|
7
|
+
|
|
8
|
+
def initialize
|
|
9
|
+
@db = YAML.load_file(DB_FILE_PATH)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def load
|
|
13
|
+
@db
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def update(word_list)
|
|
17
|
+
# opening yaml file. reading all items and appending new items
|
|
18
|
+
filename = DB_FILE_PATH
|
|
19
|
+
db = @db
|
|
20
|
+
# TODO: check is it exist and the value is diffrenent then convert it to a hash
|
|
21
|
+
db.merge!(word_list) if word_list.is_a?(Hash)
|
|
22
|
+
|
|
23
|
+
begin
|
|
24
|
+
# lock file and write to it
|
|
25
|
+
if( File.exists? filename )
|
|
26
|
+
file = File.new( filename, "r+")
|
|
27
|
+
else
|
|
28
|
+
file = File.new( filename, "w+" )
|
|
29
|
+
end
|
|
30
|
+
file.flock( File::LOCK_EX )
|
|
31
|
+
file.truncate( 0 )
|
|
32
|
+
file.rewind
|
|
33
|
+
file.write( db.ya2yaml )
|
|
34
|
+
ensure
|
|
35
|
+
# unlock and close file
|
|
36
|
+
file.flock( File::LOCK_UN )
|
|
37
|
+
file.close
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def stats
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
end
|
data/pingilish.gemspec
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'pingilish/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "pingilish"
|
|
8
|
+
spec.version = Pingilish::VERSION
|
|
9
|
+
spec.authors = ["tokhi"]
|
|
10
|
+
spec.email = ["shafi.tokhi@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = "converts persian text to latin and latin to persian"
|
|
13
|
+
spec.description = ""
|
|
14
|
+
spec.homepage = "https://github.com/tokhi/pingilish"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
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_development_dependency "bundler", "~> 1.10"
|
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
25
|
+
spec.add_development_dependency "rspec"
|
|
26
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pingilish
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- tokhi
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-04-01 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.10'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.10'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ~>
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ~>
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ! '>='
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ! '>='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
description: ''
|
|
56
|
+
email:
|
|
57
|
+
- shafi.tokhi@gmail.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- .gitignore
|
|
63
|
+
- .rspec
|
|
64
|
+
- .travis.yml
|
|
65
|
+
- Gemfile
|
|
66
|
+
- LICENSE.txt
|
|
67
|
+
- README.md
|
|
68
|
+
- Rakefile
|
|
69
|
+
- bin/console
|
|
70
|
+
- bin/setup
|
|
71
|
+
- db/char_maps.yml
|
|
72
|
+
- db/pingilish_words.yml
|
|
73
|
+
- lib/pingilish.rb
|
|
74
|
+
- lib/pingilish/backend_manager.rb
|
|
75
|
+
- lib/pingilish/backends/yaml.rb
|
|
76
|
+
- lib/pingilish/stem_filter.rb
|
|
77
|
+
- lib/pingilish/tokenizer.rb
|
|
78
|
+
- lib/pingilish/version.rb
|
|
79
|
+
- pingilish.gemspec
|
|
80
|
+
homepage: https://github.com/tokhi/pingilish
|
|
81
|
+
licenses:
|
|
82
|
+
- MIT
|
|
83
|
+
metadata: {}
|
|
84
|
+
post_install_message:
|
|
85
|
+
rdoc_options: []
|
|
86
|
+
require_paths:
|
|
87
|
+
- lib
|
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
89
|
+
requirements:
|
|
90
|
+
- - ! '>='
|
|
91
|
+
- !ruby/object:Gem::Version
|
|
92
|
+
version: '0'
|
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
|
+
requirements:
|
|
95
|
+
- - ! '>='
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: '0'
|
|
98
|
+
requirements: []
|
|
99
|
+
rubyforge_project:
|
|
100
|
+
rubygems_version: 2.4.8
|
|
101
|
+
signing_key:
|
|
102
|
+
specification_version: 4
|
|
103
|
+
summary: converts persian text to latin and latin to persian
|
|
104
|
+
test_files: []
|