Skip to content

Commit

Permalink
Changed console logs to the slf4j api framework
Browse files Browse the repository at this point in the history
Added an optional logging implementation of logback

This closes #2
  • Loading branch information
Chaiavi committed Apr 21, 2017
1 parent 95903bd commit ab14d67
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
42 changes: 38 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,66 @@

<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>

<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<optional>true</optional>
</dependency>


</dependencies>

<build>
<plugins>

<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<version>3.6.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>

<!-- Make this jar executable -->
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<!-- Jar file entry point -->
<mainClass>org.chaiware.app.TextToEmotion</mainClass>
</manifest>
</archive>

</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/chaiware/app/TextToEmotion.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@

import org.chaiware.emotion.EmotionalState;
import org.chaiware.emotion.Empathyscope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** This class is the class you should use in order to parse your text for emotions, it contains static methods */
public class TextToEmotion {

private static Logger logger = LoggerFactory.getLogger(TextToEmotion.class);

/** For internal use, in order to try the app and see that it is working while developing it */
public static void main(String[] args ) throws Exception {
if (args.length < 1) {
System.out.println("Please send an argument with the string you want to sense for emotion");
logger.info("Please send an argument with the string you want to sense for emotion");
System.exit(0);
}
String text = args[0];

System.out.println(textToEmotion(text));
logger.info(textToEmotion(text).toString());
}

/** Use this method in order to analyze any text for emotions */
Expand All @@ -26,7 +30,7 @@ public static EmotionalState textToEmotion(String text) throws Exception {
try {
sentenceState = Empathyscope.getInstance().feel(text);
} catch (IOException e) {
e.printStackTrace();
logger.debug("TextToEmotion failure", e);
throw new Exception("TextToEmotion failed to start (probably due to failure of loading its internal files)");
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/chaiware/emotion/Empathyscope.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.chaiware.emotion.util.HeuristicsUtility;
import org.chaiware.emotion.util.LexicalUtility;
import org.chaiware.emotion.util.ParsingUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Defines logic for transferring textual affect information, emotional
Expand All @@ -16,11 +18,13 @@
*/
public class Empathyscope {

private static Logger logger = LoggerFactory.getLogger(Empathyscope.class);
private static Empathyscope instance;
private LexicalUtility lexUtil;

private Empathyscope() throws IOException {
lexUtil = LexicalUtility.getInstance();
logger.info("Empathy Scope Instantiated");
}

/**
Expand Down Expand Up @@ -53,7 +57,7 @@ public EmotionalState feel(String text) throws IOException {

for (String sentence : sentences) {

System.out.println("- " + sentence);
logger.debug("- " + sentence);

// we imply 6 heuristic rules to adjust emotive weights of the words:
// (1) more exclamation signs in a sentence => more intensive emotive weights
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/chaiware/emotion/util/LexicalUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

import org.chaiware.emotion.AffectWord;
import org.chaiware.util.PropertiesManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Utility class for some text processing algorithms (Singleton)
*/
public class LexicalUtility {

private static Logger logger = LoggerFactory.getLogger(LexicalUtility.class);
private static LexicalUtility instance;

private String FILENAME_LEXICON = "/data/lex/lexicon.txt";
Expand All @@ -36,6 +39,8 @@ private LexicalUtility() throws IOException {
intensityModifiers = ParsingUtility.splitWords(pm.getProperty("intensity.modifiers"), ", ");
parseLexiconFile(affectWords, FILENAME_LEXICON);
parseLexiconFile(emoticons, FILENAME_EMOTICONS);

logger.debug("Lexical Utility Instantiated");
}

/**
Expand All @@ -54,15 +59,18 @@ public static LexicalUtility getInstance() throws IOException {

private void parseLexiconFile(List<AffectWord> wordList, String fileName) throws IOException {

try (BufferedReader in = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(fileName), "UTF8"))){
try (BufferedReader in = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(fileName), "UTF8"))) {

String line = in.readLine();
while (line != null) {
AffectWord record = parseLine(line);
wordList.add(record);
line = in.readLine();
}

logger.debug("Parsed lexicon file: {}", fileName);
}
}
}

/**
* Parses one line of the Lexicon and returns the {@link AffectWord}
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/chaiware/util/PropertiesManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.chaiware.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
Expand All @@ -10,7 +12,8 @@
* A class for handling XML property files.
*/
public class PropertiesManager {


Logger logger = LoggerFactory.getLogger(PropertiesManager.class);
private Properties props;
private File file;

Expand All @@ -21,13 +24,13 @@ public class PropertiesManager {
*/

public PropertiesManager(String fileName) {
props = new Properties();
props = new Properties();

try {
props.loadFromXML(this.getClass().getResourceAsStream(fileName));
} catch (IOException e) {
props = new Properties();
e.printStackTrace();
logger.error("Failed loading properties", e);
}
}

Expand Down Expand Up @@ -105,7 +108,7 @@ public void save(){
try {
props.storeToXML(new FileOutputStream(file), null);
} catch (Exception e) {
e.printStackTrace();
logger.error("Failed saving properties", e);
}
}
}

0 comments on commit ab14d67

Please sign in to comment.