-
Notifications
You must be signed in to change notification settings - Fork 0
/
happyorsad.pl
executable file
·161 lines (159 loc) · 3.99 KB
/
happyorsad.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/perl
###############################################################################
# Author: Puneet Singh
# Script to know if your text's mood is Happy or Sad
###############################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
# prints out a list of frequencies and words of an arbitrary text file
#
# I know the code sucks! as there are so many levels of indentations
# I promise to update this in future, clean it and make it faster
###############################################################################
open(MYINPUTFILE, "<@ARGV[0]");
open(MYDB, "<./database.db");
open(EMODB, "<./emotions.db");
@dbcontent = <MYDB>;
@emodbcontent=<EMODB>;
$/ = ""; # paragraph mode on
$i=0;
do "./bigram.pl";
doBigram();
foreach $enter(@bigramArr){
$enter=~/([a-zA-Z0-9]*) ([a-zA-Z0-9]*)/;
$first=$1;
$second=$2;
foreach $line(@dbcontent) {
if($line=~/^[a-zA-Z]/)
{
if (($line=~/^$first\*\s/)||($line=~/^$first\s/))
{
$numbers=$line;
$foundWords=$line;
$numbers=~s/[a-zA-Z\*]*//;#at the end do not have to print word
$foundWords=~s/[0-9].*//;
$foundWords=~s/[\s]*//;
$foundWords=~s/[\s\*(]//;
@number1=split(/\s/,$numbers);
}
}
}
foreach $line(@dbcontent) {
if($line=~/^[a-zA-Z]/)
{
if (($line=~/^$second\*\s/)||($line=~/^$second\s/))
{
$numbers=$line;
$foundWords=$line;
$numbers=~s/[a-zA-Z\*]*//;#at the end do not have to print word
$foundWords=~s/[0-9].*//;
$foundWords=~s/[\s]*//;
$foundWords=~s/[\s\*(]//;
@number2=split(/\s/,$numbers);
}
}
}
foreach $nu1(@number1)
{
foreach $nu2(@number2)
{
foreach $line(@emodbcontent)
{
if($line=~/([0-9+\-]*) ([0-9+\-]*) ([0-9+\-]*) ([0-9+\-]*)/)
{
if(($1==$nu1 and $2==$nu2)or($1==$nu2 and $2==$nu1))
{
if($3>=0 and $4>=0)
{
#I know, I know, "6 level of indentation. WTH!" :P
#Soon would port it to Java and with better code,Relax!
@q1[0]=@q1[0]+$3;
@q1[1]=@q1[1]+$4;
$qw1++;
}
if($3<0 and $4<0)
{
@q3[0]=@q3[0]+$3;
@q3[1]=@q3[1]+$4;
$qw3++;
}
if($3>=0 and $4<0)
{
@q4[0]=@q4[0]+$3;
@q4[1]=@q4[1]+$4;
$qw4++;
}
if($3<0 and $4>=0)
{
@q2[0]=@q2[0]+$3;
@q2[1]=@q2[1]+$4;
$qw2++;
}
}
elsif($1==0 and ($2==$nu1 or $2==$nu2))
{
if($3>=0 and $4>=0)
{
@q1[0]=@q1[0]+$3;
@q1[1]=@q1[1]+$4;
$qw1++;
}
if($3<0 and $4<0)
{
@q3[0]=@q3[0]+$3;
@q3[1]=@q3[1]+$4;
$qw3++;
}
if($3>=0 and $4<0)
{
@q4[0]=@q4[0]+$3;
@q4[1]=@q4[1]+$4;
$qw4++;
}
if($3<0 and $4>=0)
{
@q2[0]=@q2[0]+$3;
@q2[1]=@q2[1]+$4;
$qw2++;
}
}
}
}
}
}
$i=$i+1;
}
$all=(@q1[0]+@q4[0])-(@q2[0]+@q3[0]);
if($all != 0)
{
$happy = (@q1[0]+@q4[0])*100/$all;
$sad =((@q2[0]+@q3[0])*-1)*100/$all;
if($happy>$sad)
{
print "This text is so Happy!\n"
}
elsif($happy<$sad)
{
print "This text is so Sad!\n"
}
else
{
"Not sure if this is Happy or Sad :-( :-)\n";
}
printf "Happy:\t%.2f% :-)\nSad:\t%.2f% :-(\n",(@q1[0]+@q4[0])*100/$all,((@q2[0]+@q3[0])*-1)*100/$all;
}
else
{
printf "Not sure if this is Happy or Sad :-( :-)";
}