psych_analyzer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in psych_analyzer.gemspec
4
+ gemspec
5
+
6
+ # memcache
7
+ gem "memcache", "~> 1.2.13"
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ramy Khater
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # PsychAnalyzer
2
+
3
+ PsychAnalyzer creates a psychological profile of any content given based on provided dictionary contains the categories and its keywords that fits with your needs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'psych_analyzer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install psych_analyzer
18
+
19
+ ## Usage
20
+
21
+ 1. require 'psych_analyzer'
22
+ 2. paths = {"dictionary" => "psych_dictionary.csv", "ignored_keywords" => "ignored_keywords.csv"}
23
+ 3. input = ["I love this car.","This view is amazing.","I feel great this morning.","I am so excited about the concert.","He is my best friend.","I do not like this car.","This view is horrible.","I feel tired this morning.","I am not looking forward to the concert.","He is my enemy.", "that is so good"]
24
+ 4. PsychAnalyzer.train paths
25
+ 5. psychological_profile = PsychAnalyzer.analyze input
26
+ 6. puts psychological_profile
27
+
28
+ => {"positive"=>63.64, "negative"=>36.36}
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ "i", "am", "you", "he", "she", "they", "them", "the", "this", "these", "those", "at", "a", "an", "on", "in", "is", "to", "for",
2
+ "my", "your", "so", "do", "did", "dose"
@@ -0,0 +1,107 @@
1
+ require "psych_analyzer/version"
2
+ require "memcache"
3
+ module PsychAnalyzer
4
+
5
+ @cache = Memcache.new(:server => 'localhost:11211', :native => true, :segment_large_values => true)
6
+ @ignored_words = Array.new()
7
+ @dictionary = Hash.new
8
+
9
+ def self.train(trainingFiles)
10
+ trainingData = {"dictionary"=>Array.new(), "ignored_words"=>Array.new()}
11
+ unless !@cache.get('trainingData').nil?
12
+ # initialize/read dictionary file
13
+ dictionary_file = File.new(trainingFiles["dictionary"], 'r')
14
+ dictionary_class = String.new("")
15
+ dictionary_class_words = Array.new()
16
+ # build dictionary array
17
+ dictionary_file.each_line("\n") do |row|
18
+ # set dictionary classes key
19
+ unless !row.include?('#')
20
+ dictionary_class = row.sub!('#', '').sub!("\n", '')
21
+ @dictionary[dictionary_class] = Array.new
22
+ end
23
+ # assign class keywords of each dictionary class
24
+ unless row=="\n"
25
+ dictionary_class_words = Array.new()
26
+ row.split(",").each do |word|
27
+ dictionary_class_words.push(word.gsub("\"", '').strip) unless word == "\n"
28
+ end
29
+ @dictionary[dictionary_class].concat(dictionary_class_words)
30
+ end
31
+ end
32
+ trainingData["dictionary"] = @dictionary
33
+ # initialize/read ingored keywords file
34
+ ignored_keywords_file = File.new(trainingFiles["ignored_keywords"], 'r')
35
+ ignored_keywords_file.each_line("\n") do |row|
36
+ row.split(",").each do |word|
37
+ @ignored_words.push(word.gsub("\"", '').strip) unless word == "\n"
38
+ end
39
+ end
40
+ trainingData["ignored_words"] = @ignored_words
41
+
42
+ #cache the trained data
43
+ @cache.set('trainingData', trainingData, :expiry => (15*24*3600))
44
+ else
45
+ trainingData = @cache.get('trainingData')
46
+ @dictionary = trainingData["dictionary"]
47
+ @ignored_words = trainingData["ignored_words"]
48
+ end
49
+ #
50
+ return true
51
+
52
+ rescue => err
53
+ puts "Exception: #{err}"
54
+ err
55
+ end
56
+
57
+ # Extract feature words from given string
58
+ #
59
+ # Example:
60
+ # >> input = ["sample statement", "sample statement2"]
61
+ # >> extract_feature_words(input)
62
+ #
63
+ # Arguments:
64
+ # contents: (Array of Strings)
65
+ #
66
+ # Returns:
67
+ # Hash of feature words in "word, repeat" pairs ex.
68
+ # { "sample" => 2, "statement" => 1, "statement2" => 1 }
69
+ def self.extract_feature_words(contents)
70
+ words = Hash.new(0)
71
+ contents.each do |content|
72
+ tokens = content.split(/[^a-zA-z]/)
73
+ tokens.each do |token|
74
+ token.downcase! unless token.upcase == token # make token all lowercase unless it is all caps
75
+ words[token] += 1 unless token.empty? || @ignored_words.include?(token.downcase)
76
+ end
77
+ end
78
+
79
+ words.keys.sort
80
+
81
+ return words
82
+
83
+ rescue => err
84
+ puts "Exception: #{err}"
85
+ err
86
+ end
87
+
88
+ def self.analyze(contents)
89
+ psychological_profile = Hash.new(0)
90
+ _classes_total = 0
91
+
92
+ (self.extract_feature_words(contents)).each do |word, repeated|
93
+ @dictionary.each do |_class, _class_words|
94
+ psychological_profile[_class] += 1 unless !_class_words.include?(word)
95
+ end
96
+ end
97
+
98
+ psychological_profile.each { |profile_class, total| _classes_total += total }
99
+ psychological_profile.each { |profile_class, total| psychological_profile[profile_class] = (total.to_f/_classes_total.to_f*100.0).round(2) }
100
+
101
+ return psychological_profile
102
+
103
+ rescue => err
104
+ puts "Exception: #{err}"
105
+ err
106
+ end
107
+ end
@@ -0,0 +1,3 @@
1
+ module PsychAnalyzer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'psych_analyzer/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "psych_analyzer"
8
+ gem.version = PsychAnalyzer::VERSION
9
+ gem.authors = ["Ramy Khater"]
10
+ gem.email = ["eng.ramymohie@gmail.com"]
11
+ gem.description = %q{creates a psychological profile of given content based-on provided dictionary.}
12
+ gem.summary = %q{creates a psychological profile of given content based-on provided dictionary.}
13
+ gem.homepage = "https://github.com/rayasocialmedia/psych_analyzer"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,6 @@
1
+ #positive
2
+ "love", "like", "amazing", "great", "excited", "best"
3
+ "good"
4
+
5
+ #negative
6
+ "not", "bad", "horrible", "tired", "enemy"
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: psych_analyzer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ramy Khater
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: creates a psychological profile of given content based-on provided dictionary.
15
+ email:
16
+ - eng.ramymohie@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - ignored_keywords.csv
27
+ - lib/psych_analyzer.rb
28
+ - lib/psych_analyzer/version.rb
29
+ - psych_analyzer.gemspec
30
+ - psych_dictionary.csv
31
+ homepage: https://github.com/rayasocialmedia/psych_analyzer
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.11
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: creates a psychological profile of given content based-on provided dictionary.
55
+ test_files: []