Merge branch 'master' into master

This commit is contained in:
Ahmed Tawila
2017-07-08 06:07:52 +03:00
committed by GitHub
319 changed files with 7100 additions and 2322 deletions
-1
View File
@@ -1 +0,0 @@
Out of the night that covers me
-10
View File
@@ -1,10 +0,0 @@
GOOD good morning /
GOOD good evening /
GOOD have a good day /
GOOD nice party! /
GOOD fine pants /
BAD nightmare volcano in the sea /
BAD darkest sky /
BAD greed and waste /
BAD army attacks /
BAD bomb explodes /
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+13 -6
View File
@@ -338,12 +338,6 @@
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
<!-- OpenNLP -->
<dependency>
<groupId>org.apache.opennlp</groupId>
<artifactId>opennlp-tools</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -366,6 +360,18 @@
<artifactId>groovy-all</artifactId>
<version>2.4.10</version>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility-proxy</artifactId>
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<multiverse.version>0.7.0</multiverse.version>
@@ -397,6 +403,7 @@
<junit.version>4.12</junit.version>
<java-lsh.version>0.10</java-lsh.version>
<pact.version>3.5.0</pact.version>
<awaitility.version>3.0.0</awaitility.version>
</properties>
</project>
@@ -0,0 +1,50 @@
package com.baeldung.awaitility;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
public class AsyncService {
private final int DELAY = 1000;
private final int INIT_DELAY = 2000;
private AtomicLong value = new AtomicLong(0);
private Executor executor = Executors.newFixedThreadPool(4);
private volatile boolean initialized = false;
public void initialize() {
executor.execute(() -> {
sleep(INIT_DELAY);
initialized = true;
});
}
public boolean isInitialized() {
return initialized;
}
public void addValue(long val) {
if (!isInitialized()) {
throw new IllegalStateException("Service is not initialized");
}
executor.execute(() -> {
sleep(DELAY);
value.addAndGet(val);
});
}
public long getValue() {
if (!isInitialized()) {
throw new IllegalStateException("Service is not initialized");
}
return value.longValue();
}
private void sleep(int delay) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,35 @@
package com.baeldung.commons.beanutils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CourseEntity {
private String name;
private List<String> codes;
private Map<String, Student> students = new HashMap<String, Student>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getCodes() {
return codes;
}
public void setCodes(List<String> codes) {
this.codes = codes;
}
public void setStudent(String id, Student student) {
students.put(id, student);
}
public Student getStudent(String enrolledId) {
return students.get(enrolledId);
}
}
@@ -3,32 +3,38 @@ package com.baeldung.commons.beanutils;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class CourseService {
public static void setValues(Course course, String name, List<String> codes)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
// Setting the simple properties
PropertyUtils.setSimpleProperty(course, "name", name);
PropertyUtils.setSimpleProperty(course, "codes", codes);
}
public static void setIndexedValue(Course course, int codeIndex, String code)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
// Setting the indexed properties
PropertyUtils.setIndexedProperty(course, "codes[" + codeIndex + "]", code);
}
public static void setMappedValue(Course course, String enrollId, Student student)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
// Setting the mapped properties
PropertyUtils.setMappedProperty(course, "enrolledStudent(" + enrollId + ")", student);
}
public static String getNestedValue(Course course, String enrollId, String nestedPropertyName)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
return (String) PropertyUtils.getNestedProperty(
course, "enrolledStudent(" + enrollId + ")." + nestedPropertyName);
}
public static void copyProperties(Course course, CourseEntity courseEntity)
throws IllegalAccessException, InvocationTargetException {
BeanUtils.copyProperties(course, courseEntity);
}
}
@@ -0,0 +1,47 @@
package com.baeldung.commons.collectionutil;
public class Address {
private String locality;
private String city;
private String zip;
public String getLocality() {
return locality;
}
public void setLocality(String locality) {
this.locality = locality;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Address [locality=").append(locality).append(", city=").append(city).append(", zip=").append(zip).append("]");
return builder.toString();
}
public Address(String locality, String city, String zip) {
super();
this.locality = locality;
this.city = city;
this.zip = zip;
}
}
@@ -0,0 +1,118 @@
package com.baeldung.commons.collectionutil;
public class Customer implements Comparable<Customer> {
private Integer id;
private String name;
private Long phone;
private String locality;
private String city;
private String zip;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getPhone() {
return phone;
}
public void setPhone(Long phone) {
this.phone = phone;
}
public String getLocality() {
return locality;
}
public void setLocality(String locality) {
this.locality = locality;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public Customer(Integer id, String name, Long phone, String locality, String city, String zip) {
super();
this.id = id;
this.name = name;
this.phone = phone;
this.locality = locality;
this.city = city;
this.zip = zip;
}
public Customer(Integer id, String name, Long phone) {
super();
this.id = id;
this.name = name;
this.phone = phone;
}
public Customer(String name) {
super();
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public int compareTo(Customer o) {
return this.name.compareTo(o.getName());
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Customer [id=").append(id).append(", name=").append(name).append(", phone=").append(phone).append("]");
return builder.toString();
}
}
@@ -1,188 +0,0 @@
package com.baeldung.opennlp;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.logging.Logger;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.chunker.ChunkerModel;
import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.doccat.DoccatFactory;
import opennlp.tools.doccat.DoccatModel;
import opennlp.tools.doccat.DocumentCategorizerME;
import opennlp.tools.doccat.DocumentSample;
import opennlp.tools.doccat.DocumentSampleStream;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSSample;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.tokenize.Tokenizer;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.tokenize.WhitespaceTokenizer;
import opennlp.tools.util.InputStreamFactory;
import opennlp.tools.util.InvalidFormatException;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;
import opennlp.tools.util.Span;
import opennlp.tools.util.TrainingParameters;
public class OpenNLP {
private final static Logger LOGGER = Logger.getLogger(OpenNLP.class.getName());
private final static String text = buildString();
private final static String sentence[] = new String[] { "James", "Jordan", "live", "in", "Oklahoma", "city", "." };
private DoccatModel docCatModel;
public static void main(String[] args) {
new OpenNLP();
}
public static String buildString(){
StringBuilder sb = new StringBuilder();
sb.append("To get to the south:");
sb.append(" Go to the store.");
sb.append(" Buy a compass.");
sb.append(" Use the compass.");
sb.append(" Then walk to the south.");
return sb.toString();
}
public OpenNLP() {
try {
sentenceDetector();
tokenizer();
nameFinder();
locationFinder();
trainDocumentCategorizer();
documentCategorizer();
partOfSpeechTagger();
chunker();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sentenceDetector() throws InvalidFormatException, IOException {
InputStream is = new FileInputStream("OpenNLP/en-sent.bin");
SentenceModel model = new SentenceModel(is);
SentenceDetectorME sdetector = new SentenceDetectorME(model);
String sentences[] = sdetector.sentDetect(text);
for (String sentence : sentences) {
LOGGER.info(sentence);
}
is.close();
}
public void tokenizer() throws InvalidFormatException, IOException {
InputStream is = new FileInputStream("OpenNLP/en-token.bin");
TokenizerModel model = new TokenizerModel(is);
Tokenizer tokenizer = new TokenizerME(model);
String tokens[] = tokenizer.tokenize(text);
for (String token : tokens) {
LOGGER.info(token);
}
is.close();
}
public static void nameFinder() throws IOException {
InputStream is = new FileInputStream("OpenNLP/en-ner-person.bin");
TokenNameFinderModel model = new TokenNameFinderModel(is);
is.close();
NameFinderME nameFinder = new NameFinderME(model);
Span nameSpans[] = nameFinder.find(sentence);
String[] names = Span.spansToStrings(nameSpans, sentence);
Arrays.stream(names).forEach(LOGGER::info);
for (String name : names) {
LOGGER.info(name);
}
}
public static void locationFinder() throws IOException {
InputStream is = new FileInputStream("OpenNLP/en-ner-location.bin");
TokenNameFinderModel model = new TokenNameFinderModel(is);
is.close();
NameFinderME nameFinder = new NameFinderME(model);
Span locationSpans[] = nameFinder.find(sentence);
String[] locations = Span.spansToStrings(locationSpans, sentence);
Arrays.stream(locations).forEach(LOGGER::info);
for (String location : locations) {
LOGGER.info(location);
}
}
public void trainDocumentCategorizer() {
try {
InputStreamFactory isf = new InputStreamFactory() {
public InputStream createInputStream() throws IOException {
return new FileInputStream("OpenNLP/doc-cat.train");
}
};
ObjectStream<String> lineStream = new PlainTextByLineStream(isf, "UTF-8");
ObjectStream<DocumentSample> sampleStream = new DocumentSampleStream(lineStream);
DoccatFactory docCatFactory = new DoccatFactory();
docCatModel = DocumentCategorizerME.train("en", sampleStream, TrainingParameters.defaultParams(), docCatFactory);
} catch (IOException e) {
e.printStackTrace();
}
}
public void documentCategorizer() {
DocumentCategorizerME myCategorizer = new DocumentCategorizerME(docCatModel);
double[] outcomes = myCategorizer.categorize(sentence);
String category = myCategorizer.getBestCategory(outcomes);
if (category.equalsIgnoreCase("GOOD")) {
LOGGER.info("Document is positive :) ");
} else {
LOGGER.info("Document is negative :( ");
}
}
public static void partOfSpeechTagger() throws IOException {
try {
POSModel posModel = new POSModelLoader().load(new File("OpenNLP/en-pos-maxent.bin"));
POSTaggerME posTaggerME = new POSTaggerME(posModel);
InputStreamFactory isf = new InputStreamFactory() {
public InputStream createInputStream() throws IOException {
return new FileInputStream("OpenNLP/PartOfSpeechTag.txt");
}
};
ObjectStream<String> lineStream = new PlainTextByLineStream(isf, "UTF-8");
String line;
while ((line = lineStream.read()) != null) {
String whitespaceTokenizerLine[] = WhitespaceTokenizer.INSTANCE.tokenize(line);
String[] tags = posTaggerME.tag(whitespaceTokenizerLine);
POSSample posSample = new POSSample(whitespaceTokenizerLine, tags);
LOGGER.info(posSample.toString());
}
lineStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void chunker() throws IOException {
InputStream is = new FileInputStream("OpenNLP/en-chunker.bin");
ChunkerModel cModel = new ChunkerModel(is);
ChunkerME chunkerME = new ChunkerME(cModel);
String[] taggedSentence = new String[] {"Out", "of", "the", "night", "that", "covers", "me"};
String pos[] = new String[] { "IN", "IN", "DT", "NN", "WDT", "VBZ", "PRP"};
String chunks[] = chunkerME.chunk(taggedSentence, pos);
for (String chunk : chunks) {
LOGGER.info(chunk);
}
}
}
@@ -0,0 +1,84 @@
package com.baeldung.awaitility;
import static org.awaitility.Awaitility.await;
import static org.awaitility.Awaitility.fieldIn;
import static org.awaitility.Awaitility.given;
import static org.awaitility.proxy.AwaitilityClassProxy.to;
import static org.hamcrest.Matchers.equalTo;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import org.junit.Before;
import org.junit.Test;
public class AsyncServiceUnitTest {
private AsyncService asyncService;
@Before
public void setUp() {
asyncService = new AsyncService();
}
@Test
public void givenAsyncService_whenInitialize_thenInitOccurs1() {
asyncService.initialize();
Callable<Boolean> isInitialized = () -> asyncService.isInitialized();
await().until(isInitialized);
}
@Test
public void givenAsyncService_whenInitialize_thenInitOccurs2() {
asyncService.initialize();
Callable<Boolean> isInitialized = () -> asyncService.isInitialized();
await().atLeast(Duration.ONE_HUNDRED_MILLISECONDS)
.atMost(Duration.FIVE_SECONDS)
.with().pollInterval(Duration.ONE_HUNDRED_MILLISECONDS)
.until(isInitialized);
}
@Test
public void givenAsyncService_whenInitialize_thenInitOccurs_withDefualts() {
Awaitility.setDefaultPollInterval(10, TimeUnit.MILLISECONDS);
Awaitility.setDefaultPollDelay(Duration.ZERO);
Awaitility.setDefaultTimeout(Duration.ONE_MINUTE);
asyncService.initialize();
await().until(() -> asyncService.isInitialized());
}
@Test
public void givenAsyncService_whenInitialize_thenInitOccurs_withProxy() {
asyncService.initialize();
await().untilCall(to(asyncService).isInitialized(), equalTo(true));
}
@Test
public void givenAsyncService_whenInitialize_thenInitOccurs3() {
asyncService.initialize();
await().until(fieldIn(asyncService)
.ofType(boolean.class)
.andWithName("initialized"), equalTo(true));
}
@Test
public void givenValue_whenAddValue_thenValueAdded() {
asyncService.initialize();
await().until(() -> asyncService.isInitialized());
long value = 5;
asyncService.addValue(value);
await().until(asyncService::getValue, equalTo(value));
}
@Test
public void givenAsyncService_whenGetValue_thenExceptionIgnored() {
asyncService.initialize();
given().ignoreException(IllegalStateException.class)
.await()
.atMost(Duration.FIVE_SECONDS)
.atLeast(Duration.FIVE_HUNDRED_MILLISECONDS)
.until(asyncService::getValue, equalTo(0L));
}
}
@@ -8,31 +8,46 @@ import org.junit.Assert;
import org.junit.Test;
public class CourseServiceTest {
@Test
public void givenCourse_whenSetValuesUsingPropertyUtil_thenReturnSetValues()
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Course course = new Course();
String name = "Computer Science";
List<String> codes = Arrays.asList("CS", "CS01");
CourseService.setValues(course, name, codes);
Assert.assertEquals(name, course.getName());
Assert.assertEquals(2, course.getCodes().size());
Assert.assertEquals("CS", course.getCodes().get(0));
CourseService.setIndexedValue(course, 1, "CS02");
Assert.assertEquals("CS02", course.getCodes().get(1));
Student student = new Student();
String studentName = "Joe";
student.setName(studentName);
CourseService.setMappedValue(course, "ST-1", student);
Assert.assertEquals(student, course.getEnrolledStudent("ST-1"));
String accessedStudentName = CourseService.getNestedValue(course, "ST-1", "name");
Assert.assertEquals(studentName, accessedStudentName);
}
@Test
public void givenCopyProperties_whenCopyCourseToCourseEntity_thenCopyPropertyWithSameName()
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Course course = new Course();
course.setName("Computer Science");
course.setCodes(Arrays.asList("CS"));
course.setEnrolledStudent("ST-1", new Student());
CourseEntity courseEntity = new CourseEntity();
CourseService.copyProperties(course, courseEntity);
Assert.assertEquals(course.getName(), courseEntity.getName());
Assert.assertEquals(course.getCodes(), courseEntity.getCodes());
Assert.assertNull(courseEntity.getStudent("ST-1"));
}
}
@@ -0,0 +1,49 @@
package com.baeldung.commons.collections;
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.commons.collections4.bidimap.DualLinkedHashBidiMap;
import org.apache.commons.collections4.bidimap.DualTreeBidiMap;
import org.apache.commons.collections4.bidimap.TreeBidiMap;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Created by smatt on 03/07/2017.
*/
public class BidiMapUnitTest {
@Test
public void givenKeyValue_whenPut_thenAddEntryToMap() {
BidiMap<String, String> map = new DualHashBidiMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
assertEquals(map.size(), 2);
}
@Test
public void whenInverseBidiMap_thenInverseKeyValue() {
BidiMap<String, String> map = new DualHashBidiMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
BidiMap<String, String> rMap = map.inverseBidiMap();
assertTrue(rMap.containsKey("value1") && rMap.containsKey("value2"));
}
@Test
public void givenValue_whenRemoveValue_thenRemoveMatchingMapEntry() {
BidiMap<String, String> map = new DualHashBidiMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.removeValue("value2");
assertFalse(map.containsKey("key2"));
}
@Test
public void givenValue_whenGetKey_thenMappedKey() {
BidiMap<String, String> map = new DualHashBidiMap<>();
map.put("key1", "value1");
assertEquals(map.getKey("value1"), "key1");
}
}
@@ -0,0 +1,113 @@
package com.baeldung.commons.collections;
import com.baeldung.commons.collectionutil.Address;
import com.baeldung.commons.collectionutil.Customer;
import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.Transformer;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class CollectionUtilsGuideTest {
Customer customer1 = new Customer(1, "Daniel", 123456l, "locality1", "city1", "1234");
Customer customer4 = new Customer(4, "Bob", 456789l, "locality4", "city4", "4567");
List<Customer> list1, list2, list3, linkedList1;
@Before
public void setup() {
Customer customer2 = new Customer(2, "Fredrik", 234567l, "locality2", "city2", "2345");
Customer customer3 = new Customer(3, "Kyle", 345678l, "locality3", "city3", "3456");
Customer customer5 = new Customer(5, "Cat", 567890l, "locality5", "city5", "5678");
Customer customer6 = new Customer(6, "John", 678901l, "locality6", "city6", "6789");
list1 = Arrays.asList(customer1, customer2, customer3);
list2 = Arrays.asList(customer4, customer5, customer6);
list3 = Arrays.asList(customer1, customer2);
linkedList1 = new LinkedList<>(list1);
}
@Test
public void givenList_whenAddIgnoreNull_thenNoNullAdded() {
CollectionUtils.addIgnoreNull(list1, null);
assertFalse(list1.contains(null));
}
@Test
public void givenTwoSortedLists_whenCollated_thenSorted() {
List<Customer> sortedList = CollectionUtils.collate(list1, list2);
assertEquals(6, sortedList.size());
assertTrue(sortedList.get(0).getName().equals("Bob"));
assertTrue(sortedList.get(2).getName().equals("Daniel"));
}
@Test
public void givenListOfCustomers_whenTransformed_thenListOfAddress() {
Collection<Address> addressCol = CollectionUtils.collect(list1, customer -> {
return new Address(customer.getLocality(), customer.getCity(), customer.getZip());
});
List<Address> addressList = new ArrayList<>(addressCol);
assertTrue(addressList.size() == 3);
assertTrue(addressList.get(0).getLocality().equals("locality1"));
}
@Test
public void givenCustomerList_whenFiltered_thenCorrectSize() {
boolean isModified = CollectionUtils.filter(linkedList1, customer -> Arrays.asList("Daniel","Kyle").contains(customer.getName()));
//filterInverse does the opposite. It removes the element from the list if the Predicate returns true
//select and selectRejected work the same way except that they do not remove elements from the given collection and return a new collection
assertTrue(isModified && linkedList1.size() == 2);
}
@Test
public void givenNonEmptyList_whenCheckedIsNotEmpty_thenTrue() {
List<Customer> emptyList = new ArrayList<>();
List<Customer> nullList = null;
//Very handy at times where we want to check if a collection is not null and not empty too.
//isNotEmpty does the opposite. Handy because using ! operator on isEmpty makes it missable while reading
assertTrue(CollectionUtils.isNotEmpty(list1));
assertTrue(CollectionUtils.isEmpty(nullList));
assertTrue(CollectionUtils.isEmpty(emptyList));
}
@Test
public void givenCustomerListAndASubcollection_whenChecked_thenTrue() {
assertTrue(CollectionUtils.isSubCollection(list3, list1));
}
@Test
public void givenTwoLists_whenIntersected_thenCheckSize() {
Collection<Customer> intersection = CollectionUtils.intersection(list1, list3);
assertTrue(intersection.size() == 2);
}
@Test
public void givenTwoLists_whenSubtracted_thenCheckElementNotPresentInA() {
Collection<Customer> result = CollectionUtils.subtract(list1, list3);
assertFalse(result.contains(customer1));
}
@Test
public void givenTwoLists_whenUnioned_thenCheckElementPresentInResult() {
Collection<Customer> union = CollectionUtils.union(list1, list2);
assertTrue(union.contains(customer1));
assertTrue(union.contains(customer4));
}
}
@@ -1,151 +0,0 @@
package com.baeldung.opennlp;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.chunker.ChunkerModel;
import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.doccat.DoccatFactory;
import opennlp.tools.doccat.DoccatModel;
import opennlp.tools.doccat.DocumentCategorizerME;
import opennlp.tools.doccat.DocumentSample;
import opennlp.tools.doccat.DocumentSampleStream;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSSample;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.tokenize.WhitespaceTokenizer;
import opennlp.tools.util.InputStreamFactory;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;
import opennlp.tools.util.Span;
import opennlp.tools.util.TrainingParameters;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.Assert.assertEquals;
public class OpenNLPTests {
private final static String text = "To get to the south: Go to the store. Buy a compass. Use the compass. Then walk to the south.";
private final static String sentence[] = new String[]{"James", "Jordan", "live", "in", "Oklahoma", "city", "."};
@Test
public void givenText_WhenDetectSentences_ThenCountSentences() {
InputStream is;
SentenceModel model;
try {
is = new FileInputStream("OpenNLP/en-sent.bin");
model = new SentenceModel(is);
SentenceDetectorME sdetector = new SentenceDetectorME(model);
String sentences[] = sdetector.sentDetect(text);
assertEquals(4, sentences.length);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void givenText_WhenDetectTokens_ThenVerifyNames() {
InputStream is;
TokenNameFinderModel model;
try {
is = new FileInputStream("OpenNLP/en-ner-person.bin");
model = new TokenNameFinderModel(is);
is.close();
NameFinderME nameFinder = new NameFinderME(model);
Span nameSpans[] = nameFinder.find(sentence);
String[] names = Span.spansToStrings(nameSpans, sentence);
assertEquals(1, names.length);
assertEquals("James Jordan", names[0]);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void givenText_WhenDetectTokens_ThenVerifyLocations() {
InputStream is;
TokenNameFinderModel model;
try {
is = new FileInputStream("OpenNLP/en-ner-location.bin");
model = new TokenNameFinderModel(is);
is.close();
NameFinderME nameFinder = new NameFinderME(model);
Span locationSpans[] = nameFinder.find(sentence);
String[] locations = Span.spansToStrings(locationSpans, sentence);
assertEquals(1, locations.length);
assertEquals("Oklahoma", locations[0]);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void givenText_WhenCategorizeDocument_ThenVerifyDocumentContent() {
DoccatModel docCatModel;
try {
InputStreamFactory isf = new InputStreamFactory() {
public InputStream createInputStream() throws IOException {
return new FileInputStream("OpenNLP/doc-cat.train");
}
};
ObjectStream<String> lineStream = new PlainTextByLineStream(isf, "UTF-8");
ObjectStream<DocumentSample> sampleStream = new DocumentSampleStream(lineStream);
DoccatFactory docCatFactory = new DoccatFactory();
docCatModel = DocumentCategorizerME.train("en", sampleStream, TrainingParameters.defaultParams(), docCatFactory);
DocumentCategorizerME myCategorizer = new DocumentCategorizerME(docCatModel);
double[] outcomes = myCategorizer.categorize(sentence);
String category = myCategorizer.getBestCategory(outcomes);
assertEquals("GOOD", category);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void givenText_WhenTagDocument_ThenVerifyTaggedString() {
try {
POSModel posModel = new POSModelLoader().load(new File("OpenNLP/en-pos-maxent.bin"));
POSTaggerME posTaggerME = new POSTaggerME(posModel);
InputStreamFactory isf = new InputStreamFactory() {
public InputStream createInputStream() throws IOException {
return new FileInputStream("OpenNLP/PartOfSpeechTag.txt");
}
};
ObjectStream<String> lineStream = new PlainTextByLineStream(isf, "UTF-8");
String line;
while ((line = lineStream.read()) != null) {
String whitespaceTokenizerLine[] = WhitespaceTokenizer.INSTANCE.tokenize(line);
String[] tags = posTaggerME.tag(whitespaceTokenizerLine);
POSSample posSample = new POSSample(whitespaceTokenizerLine, tags);
assertEquals("Out_IN of_IN the_DT night_NN that_WDT covers_VBZ me_PRP", posSample.toString());
}
lineStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void givenText_WhenChunked_ThenCountChunks() {
try {
InputStream is = new FileInputStream("OpenNLP/en-chunker.bin");
ChunkerModel cModel = new ChunkerModel(is);
ChunkerME chunkerME = new ChunkerME(cModel);
String pos[] = new String[]{"NNP", "NNP", "NNP", "POS", "NNP", "NN", "VBD"};
String chunks[] = chunkerME.chunk(sentence, pos);
assertEquals(7, chunks.length);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -13,12 +13,12 @@ public class StrBuilderTest {
Assert.assertEquals(new StrBuilder("new StrBuilder!"), strBuilder);
}
@Test
public void whenCleared_thenEmpty() {
StrBuilder strBuilder = new StrBuilder("example StrBuilder!");
strBuilder.clear();
Assert.assertEquals(new StrBuilder(""), strBuilder);
}
}
}