Class: SentimentAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/sentimentanalyzer.rb

Class Method Summary collapse

Class Method Details

.analyzer(comment) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sentimentanalyzer.rb', line 33

def self.analyzer(comment)
    @totalScore = 0
    @result = []
    @finalSentimentOutput = []
    @tokenizer = Tokenizer::WhitespaceTokenizer.new
    @tokensizedComment = @tokenizer.tokenize(comment)
    @tokensizedComment.each do |word|
        @score = @sentimentWords[word].to_f
        @totalScore += @score
        @result.push({"key": word, "score": @score})
    end

    @finalSentimentOutput.push({"result": @result, "totalScore": @totalScore})
    
    return @finalSentimentOutput
end

.commentSentimentAnalyzer(comment) ⇒ Object



75
76
77
78
79
# File 'lib/sentimentanalyzer.rb', line 75

def self.commentSentimentAnalyzer(comment)
    @finalScore = analyzer(comment)[0][:totalScore]
    @sentimentResult = sentimentClassifier(@finalScore)
    return @sentimentResult
end

.initializeObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/sentimentanalyzer.rb', line 6

def self.initialize()
    @file = nil,
    @sentimentWords = '',
    @score = 0,
    @totalScore = 0,
    @sentimentScore = '',
    @finalScore = 0,
    @sentimentResult = ''
    @profaneWords = ''
    @cleanContent = true
end

.loadFile(fileName) ⇒ Object



18
19
20
21
22
# File 'lib/sentimentanalyzer.rb', line 18

def self.loadFile(fileName)
    @file = File.read(fileName)
    @sentimentWords = JSON.parse(@file)
    return @sentimentWords
end

.loadProfaneWords(filePath) ⇒ Object



24
25
26
27
# File 'lib/sentimentanalyzer.rb', line 24

def self.loadProfaneWords(filePath)
    @profaneWords = CSV.read(filePath)
    return @profaneWords
end

.profaneWordsFilter(content) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sentimentanalyzer.rb', line 50

def self.profaneWordsFilter(content)
    @cleanContent = true
    @tokenizer = Tokenizer::WhitespaceTokenizer.new
    @content = content.to_s
    @tokenizedContent = @tokenizer.tokenize(@content)
    @tokenizedContent.each do |word|
       if @profaneWords.include?([word])
            @cleanContent = false
       end
    end
    return @cleanContent
end

.sentimentClassifier(totalScore) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sentimentanalyzer.rb', line 63

def self.sentimentClassifier(totalScore)
    @sentimentScore = ""
    if @totalScore > 0
        @sentimentScore = "Positive"
    elsif @totalScore < 0
        @sentimentScore = "Negative"
    elsif @totalScore == 0
        @sentimentScore = "Neutral"
    end
    return @sentimentScore
end

.setFilePath(filePath) ⇒ Object



29
30
31
# File 'lib/sentimentanalyzer.rb', line 29

def self.setFilePath(filePath)
    @sentimentWords = loadFile(filePath)
end