diff --git a/polarityFuncs.R b/polarityFuncs.R new file mode 100644 index 0000000..54a419b --- /dev/null +++ b/polarityFuncs.R @@ -0,0 +1,113 @@ +# ======================================================================================# +# This Script contains functions designed to return a +# numerical value indicating the strength of polarity, or the polatiry value +# of a word or phrase. + +# The input taken by the functions are in the form of 2x2 Contingency matrix, eg. +# pos neg +# c | f(c,pos) f(c,neg) +# ~c | f(~c,pos) f(~c,neg) +# ======================================================================================# + + +# ================================================================================ +# Function: calcChiSqrPV +# Task: Calculate the Chi Square based Polarity Value from a contingency matrix +# Input: Numeric vector/Matrix representing the contingeny matrix +# Output: Polarity Value +# ================================================================================ + +calcChiSqrPV<-function(conMat){ + + # Chi Square based polarity value + mat<- matrix(as.numeric(conMat), ncol=2, byrow=TRUE) + expmat<- matrix(data=NA, nrow=2, ncol=2) + + # Sum of all elements of contingency matrix + sum<-0 + + #Loop to get the sum + for(i in 1:2){ # row + for(j in 1:2){ #coloumn + sum<-sum+mat[i,j] + } + } + + # For-loop to genetare matrix of expected values + for(i in 1:2){ # row + for(j in 1:2){ #coloumn + rowsum<-0 + colsum<-0 + + for(k in 1:2){ + rowsum<- rowsum+mat[i,k] + } + + for(k in 1:2){ + colsum<- colsum+mat[k,j] + } + + expmat[i,j]<- (rowsum*colsum)/sum + } + } + + chisqr<-0 + + # Loop to calculate the chi-square value + for(i in 1:2){ # row + for(j in 1:2){ #coloumn + + chisqr<- chisqr+ ((mat[i,j]-expmat[i,j])^2)/expmat[i,j] + + } + } + + Pc_pos<-mat[1,1]/(mat[1,1]+mat[2,1]) + Pc_neg<-mat[1,2]/(mat[1,2]+mat[2,2]) + + if(Pc_pos