farsi_processor 0.2.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 14caf0cd116b2df5f3c3dec3266733e2356d8a3b
4
+ data.tar.gz: 231eb73eda6fd8d035ab607f81128a1179eaf756
5
+ SHA512:
6
+ metadata.gz: 5d50d1e956b6a582c37de9a62fa122f652a0ae2b036ef7303df142d4ae4317feb8961986b7f62c0e2603e682c487b3cfe9cd5fe6a5b936d6fe92bc108198a582
7
+ data.tar.gz: a7dd822e3a43d8ea0c3855fed403aa7b3972185e0bfe4cd0b8e9ed7ba4c6d9b3a9849ce4cf2d3457da3a97275d9cfd02d2c6c6049b783ab6081b6f942f09c14a
@@ -0,0 +1,39 @@
1
+ # Ruby CircleCI 2.0 configuration file
2
+ #
3
+ # Check https://circleci.com/docs/2.0/language-ruby/ for more details
4
+ #
5
+ version: 2
6
+ jobs:
7
+ build:
8
+ docker:
9
+ # specify the version you desire here
10
+ - image: circleci/ruby:2.4.1-node-browsers
11
+
12
+ # Specify service dependencies here if necessary
13
+ # CircleCI maintains a library of pre-built images
14
+ # documented at https://circleci.com/docs/2.0/circleci-images/
15
+ # - image: circleci/postgres:9.4
16
+
17
+ working_directory: ~/repo
18
+
19
+ steps:
20
+ - checkout
21
+
22
+ - run:
23
+ name: install dependencies
24
+ command: |
25
+ bundle install --jobs=4 --retry=3 --path vendor/bundle
26
+
27
+ # run tests!
28
+ - run:
29
+ name: run tests
30
+ command: |
31
+ mkdir /tmp/test-results
32
+ bundle exec rspec --out /tmp/test-results/rspec.xml --format progress
33
+
34
+ # collect reports
35
+ - store_test_results:
36
+ path: /tmp/test-results
37
+ - store_artifacts:
38
+ path: /tmp/test-results
39
+ destination: test-results
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.5
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at mark.jad@live.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in farsi_processor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 mark jad
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,68 @@
1
+ # Farsi Normalizer [![CircleCI](https://circleci.com/gh/mshka/farsi_processor.svg?style=svg)](https://circleci.com/gh/mshka/farsi_processor)
2
+
3
+ FarsiProcessor is a ruby gem to normalize and stem Persian/Farsi text
4
+
5
+ Normalization is defined as:
6
+ - Normalization of [arabic kaf](https://unicode-table.com/en/0643/) to [farsi keheh](https://unicode-table.com/en/06A9/)
7
+ - Normalization of [arabic yeh](https://unicode-table.com/en/064A/) and [arabic alef maksoura](https://unicode-table.com/en/0649/) to [farsi yeh](https://unicode-table.com/en/06CC/)
8
+ - Normalization of [alef mada](https://unicode-table.com/en/0622/), [alef with hamza below](https://unicode-table.com/en/0625/) and [alef with hamza above](https://unicode-table.com/en/0623/) to [alef](https://unicode-table.com/en/0627/)
9
+ - Removing [TATWIL](https://unicode-table.com/en/0640/)
10
+ - Removing [DIACRITICS](https://github.com/mshka/farsi_processor/blob/master/lib/farsi_normalizer.rb#L18-L25)
11
+
12
+ Stemming is defined as removing these [suffixes (+ suffixes of plural form)](https://github.com/mshka/farsi_processor/blob/master/lib/farsi_stemmer.rb#L19-L28)
13
+
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem "farsi_processor"
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install farsi_processor
30
+
31
+ ## Usage
32
+
33
+ ```ruby
34
+ require 'farsi_processor'
35
+
36
+ [1] pry(main)> FarsiProcessor.process("ك")
37
+ => "ک"
38
+
39
+ [2] pry(main)> FarsiProcessor.process("کتاب‌ ها")
40
+ => "کتاب"
41
+
42
+ # it supports only and except options
43
+ [3] pry(main)> FarsiProcessor.process("ك ي", only: ["ك"])
44
+ => "ک ي"
45
+
46
+ [4] pry(main)> FarsiProcessor.process("ك ي", except: ["ك"])
47
+ => "ك ی"
48
+
49
+ [5] pry(main)> FarsiProcessor.process('دخترهای', except: ['های'])
50
+ => "دختره"
51
+
52
+ # you can choose to just normalize or stem a word,
53
+ # they also support an only and except option
54
+ [6] pry(main)> FarsiProcessor.normalize("ك")
55
+ => "ک"
56
+
57
+ [7] pry(main)> FarsiProcessor.stem("کتاب‌ ها")
58
+ => "کتاب"
59
+
60
+ ```
61
+
62
+ ### Questions or Problems?
63
+
64
+ If you have any issues with farsi_processor which you cannot find the solution, please add an [issue on GitHub](https://github.com/mshka/farsi_processor/issues) or fork the project and send a pull request.
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "farsi_processor"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'farsi_processor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "farsi_processor"
8
+ spec.version = FarsiProcessor::VERSION
9
+ spec.authors = ["mark jad"]
10
+ spec.email = ["mark.jad@live.com"]
11
+
12
+ spec.summary = %q{farsi_processor is a Ruby gem to process (stem and normalize) persian/farsi text}
13
+ spec.description = %q{farsi_processor is a Ruby gem to process (stem and normalize) persian/farsi text}
14
+ spec.homepage = "https://github.com/mshka/farsi_processor"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.14"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,97 @@
1
+ require 'farsi_processor/version'
2
+
3
+ class FarsiNormalizer
4
+ ARABIC_KAF = "\u0643".freeze # ك
5
+ FARSI_KEHEH = "\u06a9".freeze # ک
6
+
7
+ ARABIC_YEH = "\u064a".freeze # ي
8
+ ARABIC_ALEF_MAKSOURA = "\u0649".freeze # ى
9
+ FARSI_YEH = "\u06cc".freeze # ی
10
+
11
+ ALEF_MADDA = "\u0622".freeze # آ
12
+ ALEF_WITH_HAMZA_BELOW = "\u0625".freeze # إ
13
+ ALEF_WITH_HAMZA_ABOVE = "\u0623".freeze # أ
14
+ ALEF = "\u0627".freeze # ا
15
+
16
+ TATWIL = "\u0640".freeze # ـ
17
+
18
+ FATHATAN = "\u064b".freeze
19
+ DAMMATAN = "\u064c".freeze
20
+ KASRATAN = "\u064d".freeze
21
+ FATHA = "\u064e".freeze
22
+ DAMMA = "\u064f".freeze
23
+ KASRA = "\u0650".freeze
24
+ SHADDA = "\u0651".freeze
25
+ SUKUN = "\u0652".freeze
26
+
27
+ CHARACTERS_MAPPINGS = {
28
+ ARABIC_KAF => FARSI_KEHEH,
29
+ ARABIC_YEH => FARSI_YEH,
30
+ ARABIC_ALEF_MAKSOURA => FARSI_YEH,
31
+ ALEF_MADDA => ALEF,
32
+ ALEF_WITH_HAMZA_BELOW => ALEF,
33
+ ALEF_WITH_HAMZA_ABOVE => ALEF,
34
+ TATWIL => ''
35
+ }.freeze
36
+
37
+ DIACRITICS = [
38
+ FATHATAN,
39
+ DAMMATAN,
40
+ KASRATAN,
41
+ FATHA,
42
+ DAMMA,
43
+ KASRA,
44
+ SHADDA,
45
+ SUKUN
46
+ ].freeze
47
+
48
+ def self.process(word, options = {})
49
+ new(word, options).process
50
+ end
51
+
52
+ attr_reader :word, :excepts, :onlys
53
+
54
+ def initialize(word, options = {})
55
+ @word = word.dup
56
+
57
+ @onlys = []
58
+ @excepts = []
59
+ if options[:only]
60
+ @onlys = options[:only]
61
+ elsif options[:except]
62
+ @excepts = options[:except]
63
+ end
64
+ end
65
+
66
+ def process
67
+ map_charachters
68
+ remove_diacritics
69
+ word
70
+ end
71
+
72
+ private
73
+
74
+ def filter_rules(group)
75
+ if excepts.any?
76
+ group.reject { |k, _v| excepts.include?(k) }
77
+ elsif onlys.any?
78
+ group.select { |k, _v| onlys.include?(k) }
79
+ else
80
+ group
81
+ end
82
+ end
83
+
84
+ def map_charachters
85
+ rules = filter_rules(CHARACTERS_MAPPINGS)
86
+ return if rules.empty?
87
+
88
+ word.gsub!(/[#{rules.keys.join}]/, rules)
89
+ end
90
+
91
+ def remove_diacritics
92
+ rules = filter_rules(DIACRITICS)
93
+ return if rules.empty?
94
+
95
+ word.gsub!(/[#{rules.join}]/, '')
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ class FarsiProcessor
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'farsi_processor/version'
2
+ require_relative 'farsi_normalizer'
3
+ require_relative 'farsi_stemmer'
4
+
5
+ class FarsiProcessor
6
+ def self.process(word, options = {})
7
+ new(word, options).process
8
+ end
9
+
10
+ def self.normalize(word, options = {})
11
+ new(word, options).normalize
12
+ end
13
+
14
+ def self.stem(word, options = {})
15
+ new(word, options).stem
16
+ end
17
+
18
+ attr_reader :word, :options
19
+
20
+ def initialize(word, options = {})
21
+ @word = word
22
+ @options = options
23
+ end
24
+
25
+ def process
26
+ normalize
27
+ stem
28
+ end
29
+
30
+ def normalize
31
+ @word = FarsiNormalizer.process(word, options)
32
+ end
33
+
34
+ def stem
35
+ @word = FarsiStemmer.process(word, options)
36
+ end
37
+ end
@@ -0,0 +1,73 @@
1
+ require 'farsi_processor/version'
2
+
3
+ class FarsiStemmer
4
+ ALEF = "\u0627".freeze # ا
5
+ YEH = "\u06cc".freeze # ی
6
+ HEH = "\u0647".freeze # ه
7
+ TET = "\u062a".freeze # ت
8
+ REH = "\u0631".freeze # ر
9
+ NOON = "\u0646".freeze # ن
10
+ GAF = "\u06af".freeze # گ
11
+ MEEM = "\u0645".freeze # م
12
+
13
+ PLURAL_FORMS = [
14
+ ALEF + NOON,
15
+ ALEF + TET,
16
+ HEH + ALEF
17
+ ].freeze
18
+
19
+ SUFFIXES = [
20
+ TET + REH + YEH + NOON,
21
+ TET + REH + YEH,
22
+ GAF + REH + YEH,
23
+ HEH + ALEF + YEH,
24
+ ALEF + MEEM,
25
+ GAF + REH,
26
+ TET + REH,
27
+ ALEF + YEH
28
+ ] + PLURAL_FORMS
29
+
30
+ def self.process(word, options = {})
31
+ new(word, options).process
32
+ end
33
+
34
+ attr_reader :word, :excepts, :onlys
35
+
36
+ def initialize(word, options = {})
37
+ @word = word.dup
38
+
39
+ @onlys = []
40
+ @excepts = []
41
+ if options[:only]
42
+ @onlys = options[:only]
43
+ elsif options[:except]
44
+ @excepts = options[:except]
45
+ end
46
+ end
47
+
48
+ def process
49
+ stem_suffix
50
+ word.strip
51
+ end
52
+
53
+ private
54
+
55
+ def filter_rules(group)
56
+ if excepts.any?
57
+ group.reject { |k, _v| excepts.include?(k) }
58
+ elsif onlys.any?
59
+ group.select { |k, _v| onlys.include?(k) }
60
+ else
61
+ group
62
+ end
63
+ end
64
+
65
+ def stem_suffix
66
+ filter_rules(SUFFIXES).each do |suffix|
67
+ if word.end_with?(suffix)
68
+ @word = word[0..-(suffix.length + 1)]
69
+ break
70
+ end
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: farsi_processor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - mark jad
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-27 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: farsi_processor is a Ruby gem to process (stem and normalize) persian/farsi
56
+ text
57
+ email:
58
+ - mark.jad@live.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".circleci/config.yml"
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - farsi_processor.gemspec
75
+ - lib/farsi_normalizer.rb
76
+ - lib/farsi_processor.rb
77
+ - lib/farsi_processor/version.rb
78
+ - lib/farsi_stemmer.rb
79
+ homepage: https://github.com/mshka/farsi_processor
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.5.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: farsi_processor is a Ruby gem to process (stem and normalize) persian/farsi
103
+ text
104
+ test_files: []