Skip to content

Commit 95903bd

Browse files
committed
java8 compatibility
and several minor things This closes issue #1
1 parent f29dab5 commit 95903bd

File tree

10 files changed

+111
-107
lines changed

10 files changed

+111
-107
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.chaiware</groupId>
88
<artifactId>TextToEmotion</artifactId>
9-
<version>0.7</version>
9+
<version>0.8</version>
1010
<name>TextToEmotion</name>
1111

1212
<properties>

src/main/java/org/chaiware/app/TextToEmotion.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import org.chaiware.emotion.EmotionalState;
66
import org.chaiware.emotion.Empathyscope;
77

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

1011
/** For internal use, in order to try the app and see that it is working while developing it */
11-
public static void main(String[] args ) {
12+
public static void main(String[] args ) throws Exception {
1213
if (args.length < 1) {
1314
System.out.println("Please send an argument with the string you want to sense for emotion");
1415
System.exit(0);
@@ -19,13 +20,14 @@ public static void main(String[] args ) {
1920
}
2021

2122
/** Use this method in order to analyze any text for emotions */
22-
public static EmotionalState textToEmotion(String text) {
23+
public static EmotionalState textToEmotion(String text) throws Exception {
2324

2425
EmotionalState sentenceState = new EmotionalState();
2526
try {
2627
sentenceState = Empathyscope.getInstance().feel(text);
2728
} catch (IOException e) {
2829
e.printStackTrace();
30+
throw new Exception("TextToEmotion failed to start (probably due to failure of loading its internal files)");
2931
}
3032

3133
return sentenceState;

src/main/java/org/chaiware/emotion/AffectWord.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.chaiware.emotion;
22

3+
import java.util.StringJoiner;
4+
35
/**
46
* Represents one unit from the Lexicon: a word associated with
5-
* emotional meaning, and it's emotional weights and valence.
7+
* emotional meaning, and its emotional weights and valence.
68
* <p>
79
* The Lexicon, which consists of several thousand words (with emoticons),
810
* associates these attributes to a word:
@@ -152,12 +154,12 @@ public void flipValence() {
152154
*
153155
* @return {@link AffectWord}, new duplicated object
154156
*/
157+
@Override
155158
public AffectWord clone() {
156159
AffectWord value = new AffectWord(word, generalWeight, happinessWeight,
157-
sadnessWeight, angerWeight, fearWeight, disgustWeight,
158-
surpriseWeight);
159-
value.setStartsWithEmoticon(startsWithEmoticon);
160+
sadnessWeight, angerWeight, fearWeight, disgustWeight, surpriseWeight);
160161

162+
value.setStartsWithEmoticon(startsWithEmoticon);
161163
return value;
162164
}
163165

@@ -334,7 +336,7 @@ public void setGeneralValence(int generalValence) {
334336
}
335337

336338
/**
337-
* Sets the boolean value which determines does a word have specific
339+
* Gets the boolean value which determines if a word has specific
338340
* emotional weight for emotion types defined by Ekman: happiness, sadness,
339341
* fear, anger, disgust, and surprise.
340342
*
@@ -345,20 +347,24 @@ public boolean isZeroEkman() {
345347
return getWeightSum() == 0;
346348
}
347349

348-
/**
349-
* @return a string representation of the different weights of the emotions
350-
*/
351-
public String toString() {
352-
return word + " " + generalWeight + " " + happinessWeight + " "
353-
+ sadnessWeight + " " + angerWeight + " " + fearWeight + " "
354-
+ disgustWeight + " " + surpriseWeight;
355-
}
356-
357350
private double getValenceSum() {
358351
return happinessWeight - sadnessWeight - angerWeight - fearWeight - disgustWeight;
359352
}
360353

361354
private double getWeightSum() {
362355
return happinessWeight + sadnessWeight + angerWeight + fearWeight + disgustWeight + surpriseWeight;
363356
}
357+
358+
/**
359+
* @return a string representation of the different weights of the emotions
360+
*/
361+
@Override
362+
public String toString() {
363+
StringJoiner sj = new StringJoiner(" ");
364+
sj.add(word).add(Double.toString(generalWeight)).add(Double.toString(happinessWeight));
365+
sj.add(Double.toString(sadnessWeight)).add(Double.toString(angerWeight)).add(Double.toString(fearWeight));
366+
sj.add(Double.toString(disgustWeight)).add(Double.toString(surpriseWeight));
367+
368+
return sj.toString();
369+
}
364370
}

src/main/java/org/chaiware/emotion/Emotion.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ public Emotion(double weight, int type) {
3535
/**
3636
* Compares weights of current object and the one from the argument.
3737
*
38-
* @param arg0 {@link Emotion} which is to compared to the current one
38+
* @param emotion {@link Emotion} which is to compared to the current one
3939
* @return integer representing the result
4040
*/
41-
public int compareTo(Emotion arg0) {
42-
int value = (int) (100 * (arg0.getWeight() - weight));
41+
@Override
42+
public int compareTo(Emotion emotion) {
43+
int value = (int) (100 * (emotion.getWeight() - weight));
4344
// make sure each emotion will be considered, even if it is weight-even with another one
4445
if (value == 0)
4546
return 1;

src/main/java/org/chaiware/emotion/EmotionalState.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.chaiware.emotion;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
import java.util.SortedSet;
6-
import java.util.TreeSet;
3+
import java.util.*;
74

85

96
/**
@@ -33,9 +30,7 @@ public class EmotionalState {
3330
private EmotionalState previous;
3431
private SortedSet<Emotion> emotions;
3532

36-
/**
37-
* Empty class constructor
38-
*/
33+
3934
public EmotionalState() {
4035
this("");
4136
}
@@ -57,7 +52,7 @@ public EmotionalState(String text) {
5752
* valence, and all of the emotional weights (in a form of a SortedSet).
5853
*
5954
* @param text {@link String} representing the text
60-
* @param emotions {@link SortedSet} containing all of the specific Ekman emotinal
55+
* @param emotions {@link SortedSet} containing all of the specific Ekman emotional
6156
* weights, defined by the {@link Emotion} class
6257
* @param generalWeight double representing the general emotional weight
6358
* @param valence int representing the emotinal valence
@@ -87,7 +82,7 @@ public Emotion getStrongestEmotion() {
8782
* @return list of emotions ({@link Emotion} instances) with the highest weight
8883
*/
8984
public List<Emotion> getFirstStrongestEmotions(int stop) {
90-
List<Emotion> value = new ArrayList<Emotion>();
85+
List<Emotion> value = new ArrayList();
9186
for (Emotion e : emotions) {
9287
if (stop <= 0) {
9388
break;
@@ -297,18 +292,23 @@ public String getText() {
297292
}
298293

299294
/**
300-
* Transforms emotional data into a descriptional sentence ('toString' method)
295+
* Transforms emotional data into a descriptive sentence ('toString' method)
301296
*
302297
* @return String description of a emotinal data
303298
*/
304299
@Override
305300
public String toString() {
306-
return "Text: " + text + "\nGeneral weight: " + generalWeight
307-
+ "\nValence: " + valence + "\nHappiness weight: "
308-
+ getHappinessWeight() + "\nSadness weight: "
309-
+ getSadnessWeight() + "\nAnger weight: " + getAngerWeight()
310-
+ "\nFear weight: " + getFearWeight() + "\nDisgust weight: "
311-
+ getDisgustWeight() + "\nSurprise weight: "
312-
+ getSurpriseWeight() + "\n";
301+
StringJoiner sj = new StringJoiner("\n");
302+
sj.add("Text: " + text);
303+
sj.add("General weight: " + generalWeight);
304+
sj.add("Valence: " + valence);
305+
sj.add("Happiness weight: " + getHappinessWeight());
306+
sj.add("Sadness weight: " + getSadnessWeight());
307+
sj.add("Anger weight: " + getAngerWeight());
308+
sj.add("Fear weight: " + getFearWeight());
309+
sj.add("Disgust weight: " + getDisgustWeight());
310+
sj.add("Surprise weight: " + getSurpriseWeight());
311+
312+
return sj.toString();
313313
}
314314
}

src/main/java/org/chaiware/emotion/Empathyscope.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
/**
1313
* Defines logic for transferring textual affect information, emotional
14-
* manifestations recognised in text into visual output.
14+
* manifestations recognised in text into visual output.<br/>
15+
* This class is a singleton.
1516
*/
1617
public class Empathyscope {
1718

@@ -37,8 +38,8 @@ public static Empathyscope getInstance() throws IOException {
3738
}
3839

3940
/**
40-
* Textual affect sensing behavior, the main NLP alghoritm which uses
41-
* Synesketch Lexicon and several heuristic rules.
41+
* Textual affect sensing behavior, the main NLP algorithm which uses
42+
* the Lexicon and several heuristic rules.
4243
*
4344
* @param text String representing the text to be analysed
4445
* @return {@link EmotionalState} which represents data recognised from the text
@@ -47,48 +48,48 @@ public static Empathyscope getInstance() throws IOException {
4748
public EmotionalState feel(String text) throws IOException {
4849

4950
text = text.replace('\n', ' ');
50-
List<AffectWord> affectWords = new ArrayList<AffectWord>();
51+
List<AffectWord> affectWords = new ArrayList();
5152
List<String> sentences = ParsingUtility.parseSentences(text);
5253

5354
for (String sentence : sentences) {
5455

5556
System.out.println("- " + sentence);
5657

57-
// we imploy 6 heuristic rules to adjust emotive weights of the words:
58+
// we imply 6 heuristic rules to adjust emotive weights of the words:
5859
// (1) more exclamation signs in a sentence => more intensive emotive weights
59-
double exclamationQoef = HeuristicsUtility.computeExclaminationQoef(sentence.toLowerCase());
60+
double exclamationQoef = HeuristicsUtility.computeExclamationQoef(sentence.toLowerCase());
6061

6162
// (2) an exclamation mark next to a question mark => emotion of surprise
62-
if (HeuristicsUtility.hasExclaminationQuestionMarks(sentence)) {
63+
if (HeuristicsUtility.hasExclamationQuestionMarks(sentence)) {
6364
AffectWord emoWordSurprise = new AffectWord("?!");
6465
emoWordSurprise.setSurpriseWeight(1.0);
6566
affectWords.add(emoWordSurprise);
6667
}
6768

6869
boolean hasNegation = false;
6970

70-
List<String> splittedWords = ParsingUtility.splitWords(sentence," ");
71+
List<String> splitWords = ParsingUtility.splitWords(sentence," ");
7172
String previousWord = "";
7273
String negation = "";
7374

74-
for (String splittedWord : splittedWords) {
75+
for (String splitWord : splitWords) {
7576

76-
AffectWord emoWord = lexUtil.getEmoticonAffectWord(splittedWord);
77+
AffectWord emoWord = lexUtil.getEmoticonAffectWord(splitWord);
7778
if (emoWord == null)
78-
emoWord = lexUtil.getEmoticonAffectWord(splittedWord.toLowerCase());
79+
emoWord = lexUtil.getEmoticonAffectWord(splitWord.toLowerCase());
7980

8081
if (emoWord != null) {
8182
// (3) more emoticons with more 'emotive' signs (e.g. :DDDD)
8283
// => more intensive emotive weights
8384

84-
double emoticonCoef = HeuristicsUtility.computeEmoticonQoef(splittedWord, emoWord);
85+
double emoticonCoef = HeuristicsUtility.computeEmoticonQoef(splitWord, emoWord);
8586
if (emoticonCoef == 1.0)
86-
emoticonCoef = HeuristicsUtility.computeEmoticonQoef(splittedWord.toLowerCase(), emoWord);
87+
emoticonCoef = HeuristicsUtility.computeEmoticonQoef(splitWord.toLowerCase(), emoWord);
8788
emoWord.adjustWeights(exclamationQoef * emoticonCoef);
8889
affectWords.add(emoWord);
8990
} else {
9091

91-
List<String> words = ParsingUtility.parseWords(splittedWord);
92+
List<String> words = ParsingUtility.parseWords(splitWord);
9293

9394
for (String word : words) {
9495

@@ -105,10 +106,10 @@ public EmotionalState feel(String text) throws IOException {
105106
if (emoWord != null) {
106107

107108
// (5) word is upper case => more intensive emotive weights
108-
double capsLockCoef = HeuristicsUtility.computeCapsLockQoef(word);
109+
double capsLockCoef = HeuristicsUtility.computeUpperCasedQoef(word);
109110

110111
// (6) previous word is a intensity modifier (e.g.
111-
// "extremly") => more intensive emotive weights
112+
// "extremely") => more intensive emotive weights
112113
double modifierCoef = HeuristicsUtility.computeModifier(previousWord);
113114

114115
// change the affect word!
@@ -130,18 +131,17 @@ public EmotionalState feel(String text) throws IOException {
130131
}
131132

132133
private EmotionalState createEmotionalState(String text, List<AffectWord> affectWords) {
133-
TreeSet<Emotion> emotions = new TreeSet<Emotion>();
134+
TreeSet<Emotion> emotions = new TreeSet();
134135
int generalValence = 0;
135-
double valence, generalWeight, happinessWeight, sadnessWeight, angerWeight, fearWeight, disgustWeight, surpriseWeight;
136-
137-
valence = 0.0;
138-
generalWeight = 0.0;
139-
happinessWeight = 0.0;
140-
sadnessWeight = 0.0;
141-
angerWeight = 0.0;
142-
fearWeight = 0.0;
143-
disgustWeight = 0.0;
144-
surpriseWeight = 0.0;
136+
137+
double valence = 0.0;
138+
double generalWeight = 0.0;
139+
double happinessWeight = 0.0;
140+
double sadnessWeight = 0.0;
141+
double angerWeight = 0.0;
142+
double fearWeight = 0.0;
143+
double disgustWeight = 0.0;
144+
double surpriseWeight = 0.0;
145145

146146
// compute weights. maximum weights for the particular emotion are taken.
147147
for (AffectWord affectWord : affectWords) {

src/main/java/org/chaiware/emotion/util/HeuristicsUtility.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class HeuristicsUtility {
1212

1313
/**
14-
* Computes emoticon qoef for the sentence. Qoef is based on number of
14+
* Computes emoticon qoef for the sentence. Qoef is based on the number of
1515
* important chars in an emotion (e.g. ')' in ':)))))' ).
1616
*
1717
* @param sentence {@link String} representing the sentence
@@ -31,7 +31,7 @@ public static double computeEmoticonCoefForSentence(String sentence) throws IOEx
3131
}
3232

3333
/**
34-
* Computes emoticon qoef for the word. Qoef is based on number of important
34+
* Computes emoticon qoef for the word. Qoef is based on number the of important
3535
* chars in an emotion (e.g. ')' in ':)))))' ).
3636
*
3737
* @param word {@link String} representing the word
@@ -41,8 +41,7 @@ public static double computeEmoticonCoefForSentence(String sentence) throws IOEx
4141
public static double computeEmoticonQoef(String word, AffectWord emoticon) {
4242
if (emoticon.startsWithEmoticon()) {
4343
String emotiveWord = emoticon.getWord();
44-
return 1.0 + (0.2 * countChars(word, emotiveWord.charAt(emotiveWord
45-
.length() - 1)));
44+
return 1.0 + (0.2 * countChars(word, emotiveWord.charAt(emotiveWord.length() - 1)));
4645
} else {
4746
return 1.0;
4847
}
@@ -79,35 +78,36 @@ public static double computeModifier(String word) throws IOException {
7978
* @param word {@link String} representing the word
8079
* @return double representing the upper case qoeficient
8180
*/
82-
public static double computeCapsLockQoef(String word) {
83-
if (isCapsLock(word))
81+
public static double computeUpperCasedQoef(String word) {
82+
if (isUpperCasedWord(word))
8483
return 1.5;
8584
else
8685
return 1.0;
8786
}
8887

8988
/**
90-
* Computes the exclamination qoef -- function of a number of '!' chars in a sentence.
89+
* Computes the exclamation qoef -- function of a number of '!' chars in a sentence.
9190
*
9291
* @param text {@link String} representing the sentence
93-
* @return double representing the exclamination qoef
92+
* @return double representing the exclamation qoef
9493
*/
95-
public static double computeExclaminationQoef(String text) {
94+
public static double computeExclamationQoef(String text) {
9695
return 1.0 + (0.2 * countChars(text, '!'));
9796
}
9897

9998
/**
100-
* Returns is there a "!?" or a "?!" in a sentece.
99+
* Returns is there a "!?" or a "?!" in a sentence.
101100
*
102101
* @param text {@link String} representing the sentence
103-
* @return boolean representing the existance of a "!?" or a "?!"
102+
* @return boolean representing the existence of a "!?" or a "?!"
104103
*/
105-
public static boolean hasExclaminationQuestionMarks(String text) {
104+
public static boolean hasExclamationQuestionMarks(String text) {
106105

107106
return text.contains("?!") || text.contains("!?");
108107
}
109108

110-
private static boolean isCapsLock(String word) {
109+
/** Returns true when all of the word is upper cased */
110+
private static boolean isUpperCasedWord(String word) {
111111
for (int i = 0; i < word.length(); i++) {
112112
if (Character.isLowerCase(word.charAt(i)))
113113
return false;

0 commit comments

Comments
 (0)