Skip to content

Commit 2e8f90d

Browse files
Code minor fixes. Documentation improvement with markdown integration in Javadoc, README.md as Javadoc docs overview description.
1 parent 56e7e14 commit 2e8f90d

File tree

11 files changed

+132
-71
lines changed

11 files changed

+132
-71
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*.iml
12
/.gradle
23
/.idea
34
/build

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ from the [original project](https://github.com/cjhutto/vaderSentiment) by
1212
the paper's author C.J. Hutto. It's the same algorithm as an improved
1313
tool by extensive rewriting with **relevant changes**:
1414

15-
- Android ready.
16-
- API and package names breaking changes.
17-
- Java 1.7 compatible.
18-
- Performance improvements (e.g., `LinkedList` where's better O() than
15+
- [x] Android ready.
16+
- [x] API and package names breaking changes.
17+
- [x] Java 1.7 compatible.
18+
- [x] Performance improvements (e.g., `LinkedList` where's better O() than
1919
`ArrayList`).
2020

2121
**In progress**
2222

23-
- Multi-language (refer to section [Languages](#languages)).
23+
- [ ] Multi-language (refer to section [Languages](#languages)).
2424

2525
## Repository
2626

@@ -50,8 +50,7 @@ https://github.com/nunoachenriques/vader-sentiment-analysis/releases
5050
1. Get the code from the repository (clone or download).
5151

5252
2. Change to the package root directory and `./gradlew installDist`.
53-
Notice: remember to change `gradle.properties` accordingly to your
54-
JDK home for 1.7 version compatibility.
53+
NOTICE: check [Troubleshooting](#troubleshooting) for Java version 1.7 compatibility.
5554

5655
3. The JAR packages will be in `build/install/vader-sentiment-analysis`
5756
directory.
@@ -130,11 +129,21 @@ https://github.com/nunoachenriques/vader-sentiment-analysis/releases
130129
1. Get the code from the repository (clone or download).
131130

132131
2. Change to the package root directory and `./gradlew javadoc`.
133-
Notice: remember to change `gradle.properties` accordingly to your
134-
JDK home for 1.7 version compatibility.
132+
NOTICE: check [Troubleshooting](#troubleshooting) for Java version 1.7 compatibility.
135133

136134
3. The docs will be in `build/docs/javadoc` directory.
137135

136+
## Troubleshooting
137+
138+
### Java 1.7 compatibility
139+
140+
1. Install OpenJDK 7.
141+
2. Suffix Gradle command-line with `-Dorg.gradle.java.home=/path_to_jdk_7` such as (Debian GNU/Linux):
142+
143+
```shell
144+
./gradlew test -Dorg.gradle.java.home=/usr/lib/jvm/java-7-openjdk-amd64/
145+
```
146+
138147
## License
139148

140149
Copyright 2017 Nuno A. C. Henriques [http://nunoachenriques.net/]

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.0
1+
2.0.1

bin/markdowntohtml.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2017 Nuno A. C. Henriques [nunoachenriques.net]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* markdowntohtml.js
19+
*
20+
* Converts a Markdown text file to an HTML one.
21+
* Depends: Showdown [https://github.com/showdownjs/showdown] JavaScript library v1.8.6
22+
* Depends: jjs (Java 8)
23+
* Arguments: arguments[0] = Markdown file name.
24+
* Use case: jjs -scripting showdown.min.js markdowntohtml.js -- README.md > README.html
25+
* @author Nuno A. C. Henriques [nunoachenriques.net]
26+
*/
27+
28+
var converter = new showdown.Converter();
29+
converter.setFlavor('github');
30+
converter.setOption('simpleLineBreaks', false);
31+
converter.setOption('openLinksInNewWindow', true);
32+
converter.setOption('taskLists', true);
33+
print(converter.makeHtml(readFully(arguments[0])));

bin/showdown.min.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ sourceCompatibility = 1.7
55
targetCompatibility = 1.7
66
version = file('VERSION').getText()
77
String applicationName = 'VADER Sentiment Analysis in Java'
8+
String readmeMDFile = "${projectDir}/README.md"
9+
String readmeHTMLFile = "${buildDir}/README.html"
10+
String overviewFile = "${buildDir}/overview.html"
811

912
println "${applicationName} ${version}"
1013

@@ -25,6 +28,44 @@ test {
2528
exclude 'net/nunoachenriques/vader/text/Tokenizer*'
2629
}
2730

31+
// JAVADOC
32+
33+
javadoc {
34+
dependsOn = ['javadocOverview']
35+
title = "${applicationName}" + ' API'
36+
options.overview = "${overviewFile}"
37+
}
38+
39+
task buildDirectory {
40+
project.getBuildDir().mkdirs()
41+
}
42+
43+
task markdownToHTML(type: Exec) {
44+
description = 'Converts a Markdown text file to an HTML one.'
45+
commandLine 'jjs', '-scripting', 'bin/showdown.min.js', 'bin/markdowntohtml.js', '--', "${->javadocOverview.mdFileName()}"
46+
}
47+
48+
task javadocOverview(dependsOn: ['buildDirectory', 'markdownToHTML']) {
49+
description = 'Generates the overview.html for the Javadoc API documentation.'
50+
ext.mdFileName = { return "${readmeMDFile}" } // arg1 for markdownToHTML
51+
markdownToHTML.standardOutput = new FileOutputStream("${readmeHTMLFile}") // arg2
52+
doLast { // Complete overview.html with the head and tail HTML tags.
53+
def overviewOutput = new File("${overviewFile}")
54+
overviewOutput.text = '''\
55+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
56+
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
57+
<head>
58+
<title>''' + "${applicationName}" + ''' API</title>
59+
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
60+
</head>
61+
<body>
62+
''' + file("${readmeHTMLFile}").getText() + '''
63+
</body>
64+
</html>
65+
'''
66+
}
67+
}
68+
2869
// EXTRA PACKAGING FOR RELEASE DISTRIBUTION
2970

3071
task release(dependsOn: ['test', 'distZip', 'sourcesZip', 'javadocZip'])

gradle.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/main/java/net/nunoachenriques/vader/Constant.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
*/
2424
class Constant {
2525

26-
// Private constructor to avoid instantiation.
2726
private Constant() {
28-
// Void!
2927
}
3028

3129
// TODO check SentimentAnalysis for missing constants!

src/main/java/net/nunoachenriques/vader/SentimentAnalysis.java

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
import java.util.Map;
3030

3131
/**
32-
* The SentimentAnalysis class is the main class for VADER Sentiment Analysis.
33-
* Since version 2.0 is multi-language ready and Android ready! Use cases:
32+
* <p>The SentimentAnalysis class is the main class for VADER Sentiment Analysis.
33+
* Since version 2.0 is multi-language ready and Android ready! Use cases:</p>
34+
*
3435
* <h2>I. Several samples, same language.</h2>
3536
* <pre>
3637
* ...
37-
* {@code
38+
* <code>
3839
* SentimentAnalysis sa = new SentimentAnalysis(new TokenizerEnglish(), new English());
3940
* Map<String,Float> sp;
4041
* String s1 = "VADER is smart, handsome, and funny!";
@@ -44,13 +45,14 @@
4445
* System.out.println(s1 + " *** " + sp.toString());
4546
* sp = sa.getSentimentAnalysis(s2);
4647
* System.out.println(s2 + " *** " + sp.toString());
47-
* }
48+
* </code>
4849
* ...
4950
* </pre>
51+
*
5052
* <h2>II. Several samples, multiple languages.</h2>
5153
* <pre>
5254
* ...
53-
* {@code
55+
* <code>
5456
* SentimentAnalysis sa = new SentimentAnalysis();
5557
* Map<String,Float> sp;
5658
* String s1 = "VADER is smart, handsome, and funny!";
@@ -60,15 +62,12 @@
6062
* System.out.println(s1 + " *** " + sp.toString());
6163
* sp = sa.getSentimentAnalysis(s2, new TokenizerPortuguese(), new Portuguese());
6264
* System.out.println(s2 + " *** " + sp.toString());
63-
* }
65+
* </code>
6466
* ...
6567
* </pre>
6668
*
6769
* @author Nuno A. C. Henriques [nunoachenriques.net]
68-
* @see
69-
* <a href="http://comp.social.gatech.edu/papers/icwsm14.vader.hutto.pdf">VADER:
70-
* A Parsimonious Rule-based Model for Sentiment Analysis of Social Media
71-
* Text</a>
70+
* @see <a href="http://comp.social.gatech.edu/papers/icwsm14.vader.hutto.pdf" target="_blank">VADER: A Parsimonious Rule-based Model for Sentiment Analysis of Social Media Text</a>
7271
*/
7372
public class SentimentAnalysis {
7473

@@ -84,6 +83,7 @@ public class SentimentAnalysis {
8483
/**
8584
* Default constructor with all parameters {@code null}.
8685
*/
86+
@SuppressWarnings("WeakerAccess")
8787
public SentimentAnalysis() {
8888
text = null;
8989
language = null;
@@ -92,13 +92,15 @@ public SentimentAnalysis() {
9292
}
9393

9494
/**
95-
* Default constructor with two required parameters: tokenizer and language.
95+
* Sets two required parameters: tokenizer and language. All the others
96+
* initialize as {@code null}.
9697
*
9798
* @param l The text {@link Language}
9899
* (e.g., {@link net.nunoachenriques.vader.lexicon.English}).
99100
* @param t The text {@link Tokenizer} to be used
100101
* (e.g., {@link net.nunoachenriques.vader.text.TokenizerEnglish}).
101102
*/
103+
@SuppressWarnings("WeakerAccess")
102104
public SentimentAnalysis(Language l, Tokenizer t) {
103105
text = null;
104106
language = l;
@@ -300,23 +302,20 @@ private float checkForIdioms(float currentValence, int i) {
300302
final Map<String, Float> boosterDictionary = language.getBoosterDictionary();
301303
final Map<String, Float> sentimentLadenIdioms = language.getSentimentLadenIdioms();
302304

303-
List<String> leftGramSequences = new ArrayList<String>() {
304-
{
305-
add(leftBiGramFromCurrent);
306-
add(leftTriGramFromCurrent);
307-
add(leftBiGramFromOnePrevious);
308-
add(leftTriGramFromOnePrevious);
309-
add(leftBiGramFromTwoPrevious);
310-
}
311-
};
305+
List<String> leftGramSequences = new ArrayList<String>() {{
306+
add(leftBiGramFromCurrent);
307+
add(leftTriGramFromCurrent);
308+
add(leftBiGramFromOnePrevious);
309+
add(leftTriGramFromOnePrevious);
310+
add(leftBiGramFromTwoPrevious);
311+
}};
312312

313313
for (String leftGramSequence : leftGramSequences) {
314314
if (sentimentLadenIdioms.containsKey(leftGramSequence)) {
315315
currentValence = sentimentLadenIdioms.get(leftGramSequence);
316316
break;
317317
}
318318
}
319-
320319
if (wordsAndEmoticons.size() - 1 > i) {
321320
final String rightBiGramFromCurrent = String.format("%s %s", wordsAndEmoticons.get(i), wordsAndEmoticons.get(i + 1));
322321
if (sentimentLadenIdioms.containsKey(rightBiGramFromCurrent)) {
@@ -329,11 +328,9 @@ private float checkForIdioms(float currentValence, int i) {
329328
currentValence = sentimentLadenIdioms.get(rightTriGramFromCurrent);
330329
}
331330
}
332-
333331
if (boosterDictionary.containsKey(leftBiGramFromTwoPrevious) || boosterDictionary.containsKey(leftBiGramFromOnePrevious)) {
334332
currentValence += -0.293f; // TODO review Language and English.DAMPENER_WORD_DECREMENT;
335333
}
336-
337334
return currentValence;
338335
}
339336

@@ -387,23 +384,19 @@ private Map<String, Float> polarityScores(List<Float> currentSentimentState) {
387384
final float normalizedNegativePolarity = roundDecimal(Math.abs(negativeSentimentScore / normalizationFactor), 3);
388385
final float normalizedNeutralPolarity = roundDecimal(Math.abs(neutralSentimentCount / normalizationFactor), 3);
389386
final float normalizedCompoundPolarity = roundDecimal(compoundPolarity, 4);
390-
return new HashMap<String, Float>() {
391-
{
392-
put("compound", normalizedCompoundPolarity);
393-
put("positive", normalizedPositivePolarity);
394-
put("negative", normalizedNegativePolarity);
395-
put("neutral", normalizedNeutralPolarity);
396-
}
397-
};
387+
return new HashMap<String, Float>() {{
388+
put("compound", normalizedCompoundPolarity);
389+
put("positive", normalizedPositivePolarity);
390+
put("negative", normalizedNegativePolarity);
391+
put("neutral", normalizedNeutralPolarity);
392+
}};
398393
} else {
399-
return new HashMap<String, Float>() {
400-
{
401-
put("compound", 0.0f);
402-
put("positive", 0.0f);
403-
put("negative", 0.0f);
404-
put("neutral", 0.0f);
405-
}
406-
};
394+
return new HashMap<String, Float>() {{
395+
put("compound", 0.0f);
396+
put("positive", 0.0f);
397+
put("negative", 0.0f);
398+
put("neutral", 0.0f);
399+
}};
407400
}
408401
}
409402

@@ -455,10 +448,7 @@ private boolean hasAtLeast(List<String> tokenList) {
455448
// TODO English language dependent!
456449
if (tokenList.contains("least")) {
457450
int index = tokenList.indexOf("least");
458-
if (index > 0 && tokenList.get(index - 1).equals("at")) {
459-
460-
return true;
461-
}
451+
return index > 0 && tokenList.get(index - 1).equals("at");
462452
}
463453
return false;
464454
}
@@ -468,7 +458,6 @@ private boolean hasContraction(List<String> tokenList) {
468458

469459
// TODO English language dependent!
470460
if (s.endsWith("n't")) {
471-
472461
return true;
473462
}
474463
}

src/main/java/net/nunoachenriques/vader/lexicon/English.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@
3232
* original Python implementation.
3333
*
3434
* @author Nuno A. C. Henriques [nunoachenriques.net]
35-
* @see <a href="http://www.nltk.org/_modules/nltk/sentiment/vader.html">NLTK
36-
* source</a>
37-
* @see
38-
* <a href="https://github.com/cjhutto/vaderSentiment/blob/master/vaderSentiment/vaderSentiment.py">
39-
* vaderSentiment original Python source</a>
35+
* @see <a href="http://www.nltk.org/_modules/nltk/sentiment/vader.html" target="_blank">NLTK source</a>
36+
* @see <a href="https://github.com/cjhutto/vaderSentiment/blob/master/vaderSentiment/vaderSentiment.py target="_blank"">vaderSentiment original Python source</a>
4037
*/
4138
public final class English
4239
implements Language {
@@ -112,7 +109,6 @@ private static Map<String, Float> createBoosterDictionary() {
112109
m.put("more", BOOSTER_WORD_INCREMENT);
113110
m.put("considerably", BOOSTER_WORD_INCREMENT);
114111
m.put("fabulously", BOOSTER_WORD_INCREMENT);
115-
m.put("sort of", DAMPENER_WORD_DECREMENT);
116112
m.put("hardly", DAMPENER_WORD_DECREMENT);
117113
m.put("very", BOOSTER_WORD_INCREMENT);
118114
m.put("sortof", DAMPENER_WORD_DECREMENT);
@@ -178,11 +174,7 @@ private static Map<String, Float> getWordValenceDictionary(String filename) {
178174
return lexDictionary;
179175
}
180176

181-
/**
182-
* Default constructor.
183-
*/
184177
public English() {
185-
// Void.
186178
}
187179

188180
@Override

src/main/java/net/nunoachenriques/vader/text/TokenizerEnglish.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ public class TokenizerEnglish
5151
*/
5252
private static final Pattern PUNCTUATION_EXCLUDE_CONTRACTION_PATTERN = Pattern.compile("[\\p{Punct}&&[^.']]|(?<=(^|\\s|\\p{Punct}))[.']|[.'](?=($|\\s|\\p{Punct}))");
5353

54-
/**
55-
* Default constructor.
56-
*/
5754
public TokenizerEnglish() {
58-
// Void.
5955
}
6056

6157
@Override

0 commit comments

Comments
 (0)