Skip to content

Commit

Permalink
Refactoring of log, using tinylog, fixing Gradle build, log4j2 remove…
Browse files Browse the repository at this point in the history
…d. Some fixes. Tests 100% OK!
  • Loading branch information
nunoachenriques committed Aug 8, 2017
1 parent 24993ec commit a453763
Show file tree
Hide file tree
Showing 10 changed files with 8 additions and 113 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.gradle
.idea
build
local.properties
/.gradle
/.idea
/build
/local.properties
4 changes: 0 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ dependencies {
testImplementation 'junit:junit:4.12'
}

test {
systemProperty('log4j.configurationFile', project.getProperty('log4j.configurationFile'))
}

// EXTRA PACKAGING FOR RELEASE DISTRIBUTION

task release(dependsOn: ['test', 'distZip', 'sourcesZip', 'javadocZip'])
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
org.gradle.java.home = /usr/lib/jvm/java-7-openjdk-amd64/
log4j.configurationFile = net/nunoachenriques/vader/log4j2.properties
Binary file removed src/main/dist/log4j-api-2.8.2.jar
Binary file not shown.
Binary file removed src/main/dist/log4j-core-2.8.2.jar
Binary file not shown.
Binary file added src/main/dist/tinylog-1.2.jar
Binary file not shown.
80 changes: 2 additions & 78 deletions src/main/java/net/nunoachenriques/vader/SentimentAnalysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import net.nunoachenriques.vader.text.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -67,7 +65,6 @@
*/
public class SentimentAnalysis {

private static final Logger LOGGER = LogManager.getLogger(SentimentAnalysis.class);
private static final float NORMALIZE_SCORE_ALPHA_DEFAULT = 15.0f;

private String text;
Expand Down Expand Up @@ -208,10 +205,6 @@ private float checkForIdioms(float currentValence, int i) {
}
};

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Grams: " + leftGramSequences);
}

for (String leftGramSequence : leftGramSequences) {
if (English.SENTIMENT_LADEN_IDIOMS.containsKey(leftGramSequence)) {
currentValence = English.SENTIMENT_LADEN_IDIOMS.get(leftGramSequence);
Expand Down Expand Up @@ -246,69 +239,42 @@ private HashMap<String, Float> getSentiment() {
for (String item : wordsAndEmoticons) {
float currentValence = 0.0f;
int i = wordsAndEmoticons.indexOf(item);

LOGGER.debug("Current token, \"" + item + "\" with index, i = " + i);
LOGGER.debug("Sentiment State before \"kind of\" processing: " + sentiments);

if (i < wordsAndEmoticons.size() - 1
&& item.toLowerCase().equals("kind")
&& wordsAndEmoticons.get(i + 1).toLowerCase().equals("of")
|| English.BOOSTER_DICTIONARY.containsKey(item.toLowerCase())) {
sentiments.add(currentValence);
continue;
}

LOGGER.debug("Sentiment State after \"kind of\" processing: " + sentiments);
LOGGER.debug(String.format("Current Valence is %f for \"%s\"", currentValence, item));

String currentItemLower = item.toLowerCase();
if (English.WORD_VALENCE_DICTIONARY.containsKey(currentItemLower)) {
currentValence = English.WORD_VALENCE_DICTIONARY.get(currentItemLower);

LOGGER.debug(English.isUpper(item));
LOGGER.debug(textProperties.isCapDIff());
LOGGER.debug((English.isUpper(item) && textProperties.isCapDIff()) + "\t" + item + "\t" + textProperties.isCapDIff());

if (English.isUpper(item) && textProperties.isCapDIff()) {
currentValence = (currentValence > 0.0) ? currentValence + English.ALL_CAPS_BOOSTER_SCORE : currentValence - English.ALL_CAPS_BOOSTER_SCORE;
}

LOGGER.debug(String.format("Current Valence post all CAPS checks: %f", currentValence));

int startI = 0;
float gramBasedValence;
while (startI < 3) {
int closeTokenIndex = i - (startI + 1);
if (closeTokenIndex < 0) {
closeTokenIndex = pythonIndexToJavaIndex(closeTokenIndex);
}

if ((i > startI) && !English.WORD_VALENCE_DICTIONARY.containsKey(wordsAndEmoticons.get(closeTokenIndex).toLowerCase())) {

LOGGER.debug(String.format("Current Valence pre gramBasedValence: %f", currentValence));
gramBasedValence = valenceModifier(wordsAndEmoticons.get(closeTokenIndex), currentValence);
LOGGER.debug(String.format("Current Valence post gramBasedValence: %f", currentValence));

if (startI == 1 && gramBasedValence != 0.0f) {
gramBasedValence *= 0.95f;
}
if (startI == 2 && gramBasedValence != 0.0f) {
gramBasedValence *= 0.9f;
}
currentValence += gramBasedValence;
LOGGER.debug(String.format("Current Valence post gramBasedValence and distance boosting: %f", currentValence));

currentValence = checkForNever(currentValence, startI, i, closeTokenIndex);
LOGGER.debug(String.format("Current Valence post \"never\" check: %f", currentValence));

if (startI == 2) {
currentValence = checkForIdioms(currentValence, i);
LOGGER.debug(String.format("Current Valence post Idiom check: %f", currentValence));
}
}
startI++;
}

if (i > 1 && !English.WORD_VALENCE_DICTIONARY.containsKey(wordsAndEmoticons.get(i - 1).toLowerCase()) && wordsAndEmoticons.get(i - 1).toLowerCase().equals("least")) {
if (!(wordsAndEmoticons.get(i - 2).toLowerCase().equals("at") || wordsAndEmoticons.get(i - 2).toLowerCase().equals("very"))) {
currentValence *= English.N_SCALAR;
Expand All @@ -317,20 +283,9 @@ private HashMap<String, Float> getSentiment() {
currentValence *= English.N_SCALAR;
}
}

sentiments.add(currentValence);
}

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Sentiment state after first pass through tokens: " + sentiments);
}

sentiments = checkConjunctionBut(wordsAndEmoticons, sentiments);

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Sentiment state after checking conjunctions: " + sentiments);
}

return polarityScores(sentiments);
}

Expand Down Expand Up @@ -360,59 +315,29 @@ private HashMap<String, Float> polarityScores(ArrayList<Float> currentSentimentS
for (Float valence : currentSentimentState) {
totalValence += valence;
}

LOGGER.debug("Total valence: " + totalValence);

float punctuationAmplifier = boostByPunctuation();
if (totalValence > 0.0f) {
totalValence += boostByPunctuation();
} else if (totalValence < 0.0f) {
totalValence -= boostByPunctuation();
}

LOGGER.debug("Total valence after boost/damp by punctuation: " + totalValence);

float compoundPolarity = normalizeScore(totalValence);

LOGGER.debug("Final token-wise sentiment state: " + currentSentimentState);

ArrayList<Float> siftedScores = siftSentimentScores(currentSentimentState);
float positiveSentimentScore = siftedScores.get(0);
float negativeSentimentScore = siftedScores.get(1);
int neutralSentimentCount = Math.round(siftedScores.get(2));

LOGGER.debug(String.format("Post Sift Sentiment Scores: %s %s %s", positiveSentimentScore, negativeSentimentScore, neutralSentimentCount));

if (positiveSentimentScore > Math.abs(negativeSentimentScore)) {
positiveSentimentScore += punctuationAmplifier;
} else if (positiveSentimentScore < Math.abs(negativeSentimentScore)) {
negativeSentimentScore -= punctuationAmplifier;
}

float normalizationFactor = positiveSentimentScore + Math.abs(negativeSentimentScore)
float normalizationFactor = positiveSentimentScore
+ Math.abs(negativeSentimentScore)
+ neutralSentimentCount;

LOGGER.debug("Normalization Factor: " + normalizationFactor);

LOGGER.debug(String.format("Pre-Normalized Scores: %s %s %s %s",
Math.abs(positiveSentimentScore),
Math.abs(negativeSentimentScore),
Math.abs(neutralSentimentCount),
compoundPolarity
));

LOGGER.debug(String.format("Pre-Round Scores: %s %s %s %s",
Math.abs(positiveSentimentScore / normalizationFactor),
Math.abs(negativeSentimentScore / normalizationFactor),
Math.abs(neutralSentimentCount / normalizationFactor),
compoundPolarity
));

final float normalizedPositivePolarity = roundDecimal(Math.abs(positiveSentimentScore / normalizationFactor), 3);
final float normalizedNegativePolarity = roundDecimal(Math.abs(negativeSentimentScore / normalizationFactor), 3);
final float normalizedNeutralPolarity = roundDecimal(Math.abs(neutralSentimentCount / normalizationFactor), 3);
final float normalizedCompoundPolarity = roundDecimal(compoundPolarity, 4);

return new HashMap<String, Float>() {
{
put("compound", normalizedCompoundPolarity);
Expand All @@ -421,7 +346,6 @@ private HashMap<String, Float> polarityScores(ArrayList<Float> currentSentimentS
put("neutral", normalizedNeutralPolarity);
}
};

} else {
return new HashMap<String, Float>() {
{
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/nunoachenriques/vader/lexicon/English.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.nunoachenriques.vader.lexicon;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.pmw.tinylog.Logger;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -27,7 +26,6 @@
*/
public class English {

private static final Logger LOGGER = LogManager.getLogger(English.class);
private static final ClassLoader LOADER = English.class.getClassLoader();
private static final String LEXICON_ENGLISH_FILE = "net/nunoachenriques/vader/lexicon_english.txt";

Expand Down Expand Up @@ -172,7 +170,7 @@ private static HashMap<String, Float> getWordValenceDictionary(String filename)
lexDictionary.put(currentText, currentTextValence);
}
} catch (IOException ioe) {
LOGGER.error(ioe.getLocalizedMessage());
Logger.error(ioe.getLocalizedMessage());
}
}
return lexDictionary;
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/net/nunoachenriques/vader/text/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import net.nunoachenriques.vader.lexicon.English;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -19,8 +16,6 @@
*/
public class Properties {

private static final Logger LOGGER = LogManager.getLogger(Properties.class);

private final String inputText;
private ArrayList<String> wordsAndEmoticons;
private ArrayList<String> wordsOnly;
Expand Down Expand Up @@ -78,24 +73,18 @@ private void setCapDIff(boolean capDIff) {
private boolean isAllCapDifferential() {
int countAllCaps = 0;
for (String s : wordsAndEmoticons) {
LOGGER.debug(s + "\t" + English.isUpper(s));
if (English.isUpper(s)) {
countAllCaps++;
}
}
int capDifferential = wordsAndEmoticons.size() - countAllCaps;
LOGGER.debug(wordsAndEmoticons.size() + "\t" + capDifferential + "\t" + countAllCaps);
return (0 < capDifferential) && (capDifferential < wordsAndEmoticons.size());
}

public ArrayList<String> getWordsAndEmoticons() {
return wordsAndEmoticons;
}

public ArrayList<String> getWordsOnly() {
return wordsOnly;
}

public boolean isCapDIff() {
return isCapDIff;
}
Expand Down
11 changes: 0 additions & 11 deletions src/main/resources/net/nunoachenriques/vader/log4j2.properties

This file was deleted.

0 comments on commit a453763

Please sign in to comment.