Skip to content

Commit

Permalink
#20 integration tests and README
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jul 4, 2021
1 parent a728b13 commit 68f5289
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 1 deletion.
73 changes: 72 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,85 @@ implementation group: 'io.imagineobjects.web', name: 'linguin-ai-java', version:

The releases are also available on [Github Packages](https://github.com/imagineobjects/linguin-ai-java/packages)!

### Usage

For a given text, the library returns an instance of ``Languages``, representing the possible languages of the given text. Each ``Language`` has a
``code`` and a ``confidence``. Call ``.bestMatch()`` to get the most probable ``Language``.

### Single detection

To detect the Language of a single text:

```java
final LinguinAi linguin = new RestLinguinAi("API-TOKEN-HERE");
final Languages detected = linguin.detect("What language is this?");
System.out.println("Possible languages: ")
for(final Language language : detected) {
System.out.println("Code: " + language.code());
System.out.println("Confidence: " + language.confidence());
}
System.out.println("Most probable language: " + detected.bestMatch().code());
```

### Bulk Detection

You can detect the possible ``Languages`` of multpile texts at the same time. It will return an instance of
``BulkDetection`` which is an iterable of ``Languages``. Each ``Languages`` in the iterable represents the possible languages
of the text given at the same index.

```java
final LinguinAi linguinAi = new RestLinguinAi("API-TOKEN-HERE");
final BulkDetection bulk = linguinAi.bulkDetect(
"What's up??",
"Woher kommst du?",
"Eu vin din Romania.",
"La langue Francaise..."
);
final Iterator<Languages> languages = bulk.iterator();
languages.next().bestMatch().code(); // "en"
languages.next().bestMatch().code(); // "de"
languages.next().bestMatch().code(); // "ro"
languages.next().bestMatch().code(); // "fr"
```

### Account Status

You can fetch the account Status, containing info about the API limits of your account:

```java
final LinguinAi linguinAi = new RestLinguinAi("API-TOKEN-HERE");
final Status status = linguinAi.status();
status.dailyLimit(); //100
status.detectionsToday(); //5
status.remainingToday(); //95
```

### Supported Languages

You can get all the supported languages of the API like this:

```java
final LinguinAi linguinAi = new RestLinguinAi("API-TOKEN-HERE");
final SupportedLanguages supported = linguiAi.languages();
for(final SupportedLanguage language : supported) {
System.out.println("Code: " + language.code());
System.out.println("English names: " + Arrays.toString(language.englishNames()));
System.out.println("Native names: " + Arrays.toString(language.nativeNames()));
}
```

### Exceptions

At the moment the library will throw ``IllegalStateException`` if the API responds with any
status code other than ``200 OK``.

### Contributing

If you would like to contribute, just open an issue or a PR.

Make sure the maven build:

``$mvn clean install -Pcheckstyle,itcases``
``$mvn clean install -Pcheckstyle,itcases -Dlinguin-ai-token=<YOUR_API_TOKEN>``

passes before making a PR. [Checkstyle](http://checkstyle.sourceforge.net/) will make sure
you're following our code style and guidlines.
70 changes: 70 additions & 0 deletions src/test/java/io/imagineobjects/linguinai/BulkDetectionITCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.imagineobjects.linguinai;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Iterator;

/**
* RestLinguinAi can detect languages in bulk.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public final class BulkDetectionITCase {

/**
* Linguin.ai API Token.
* @checkstyle StaticVariableName (5 lines)
*/
private static String TOKEN;

/**
* Read the API token from maven cmd param.
*/
@BeforeClass
public static void init() {
String token = System.getProperty("linguin-api-token");
if(token == null || token.isBlank()) {
throw new IllegalStateException(
"Please specify the linguin-api-token parameter!"
);
}
TOKEN = token;
}


/**
* It can detect English, German, Romanian and French in bulk.
*/
@Test
public void detectsInBulk() {
final LinguinAi linguinAi = new RestLinguinAi(TOKEN);
final BulkDetection bulk = linguinAi.bulkDetect(
"What's up??",
"Woher kommst du?",
"Eu vin din Romania.",
"La langue Francaise..."
);
final Iterator<Languages> languages = bulk.iterator();
MatcherAssert.assertThat(
languages.next().bestMatch().code(),
Matchers.equalTo("en")
);
MatcherAssert.assertThat(
languages.next().bestMatch().code(),
Matchers.equalTo("de")
);
MatcherAssert.assertThat(
languages.next().bestMatch().code(),
Matchers.equalTo("ro")
);
MatcherAssert.assertThat(
languages.next().bestMatch().code(),
Matchers.equalTo("fr")
);
}

}
83 changes: 83 additions & 0 deletions src/test/java/io/imagineobjects/linguinai/SingleDetectITCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.imagineobjects.linguinai;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* RestLinguinAi can detect a single language.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public final class SingleDetectITCase {

/**
* Linguin.ai API Token.
* @checkstyle StaticVariableName (5 lines)
*/
private static String TOKEN;

/**
* Read the API token from maven cmd param.
*/
@BeforeClass
public static void init() {
String token = System.getProperty("linguin-api-token");
if(token == null || token.isBlank()) {
throw new IllegalStateException(
"Please specify the linguin-api-token parameter!"
);
}
TOKEN = token;
}

/**
* It can detect English.
*/
@Test
public void detectsEnglish() {
final LinguinAi linguinAi = new RestLinguinAi(TOKEN);
MatcherAssert.assertThat(
linguinAi.detect("What's up??").bestMatch().code(),
Matchers.equalTo("en")
);
}

/**
* It can detect German.
*/
@Test
public void detectsGerman() {
final LinguinAi linguinAi = new RestLinguinAi(TOKEN);
MatcherAssert.assertThat(
linguinAi.detect("Woher kommst du?").bestMatch().code(),
Matchers.equalTo("de")
);
}

/**
* It can detect Romanian.
*/
@Test
public void detectsRomanian() {
final LinguinAi linguinAi = new RestLinguinAi(TOKEN);
MatcherAssert.assertThat(
linguinAi.detect("Eu vin din Romania.").bestMatch().code(),
Matchers.equalTo("ro")
);
}

/**
* It can detect French.
*/
@Test
public void detectsFrench() {
final LinguinAi linguinAi = new RestLinguinAi(TOKEN);
MatcherAssert.assertThat(
linguinAi.detect("La langue Francaise...").bestMatch().code(),
Matchers.equalTo("fr")
);
}
}
49 changes: 49 additions & 0 deletions src/test/java/io/imagineobjects/linguinai/StatusITCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.imagineobjects.linguinai;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.BeforeClass;
import org.junit.Test;

/**
* RestLinguinAi can return the account status.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public final class StatusITCase {

/**
* Linguin.ai API Token.
* @checkstyle StaticVariableName (5 lines)
*/
private static String TOKEN;

/**
* Read the API token from maven cmd param.
*/
@BeforeClass
public static void init() {
String token = System.getProperty("linguin-api-token");
if(token == null || token.isBlank()) {
throw new IllegalStateException(
"Please specify the linguin-api-token parameter!"
);
}
TOKEN = token;
}

/**
* It can return the Status.
*/
@Test
public void returnsStatus() {
final LinguinAi linguinAi = new RestLinguinAi(TOKEN);
final Status status = linguinAi.status();
MatcherAssert.assertThat(
status.dailyLimit(),
Matchers.equalTo(100)
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.imagineobjects.linguinai;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Arrays;

/**
* RestLinguinAi can return the SupportedLanguages.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public final class SupportedLanguagesITCase {

/**
* Linguin.ai API Token.
* @checkstyle StaticVariableName (5 lines)
*/
private static String TOKEN;

/**
* Read the API token from maven cmd param.
*/
@BeforeClass
public static void init() {
String token = System.getProperty("linguin-api-token");
if(token == null || token.isBlank()) {
throw new IllegalStateException(
"Please specify the linguin-api-token parameter!"
);
}
TOKEN = token;
}

/**
* Can return them.
*/
@Test
public void returnsSupportedLanguages() {
final LinguinAi linguinAi = new RestLinguinAi(TOKEN);
final SupportedLanguages languages = linguinAi.languages();
final SupportedLanguage first = languages.iterator().next();
MatcherAssert.assertThat(
first.code(),
Matchers.equalTo("ab")
);
MatcherAssert.assertThat(
Arrays.toString(first.englishNames()),
Matchers.equalTo("[Abkhazian]")
);
MatcherAssert.assertThat(
Arrays.toString(first.nativeNames()),
Matchers.equalTo("[аҧсуа бызшәа, аҧсшәа]")
);
}

}

0 comments on commit 68f5289

Please sign in to comment.