Search in sources :

Example 41 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testGetNaturalTextWithNoLanguage.

@Test
public void testGetNaturalTextWithNoLanguage() {
    String field = "urn:the.field:used.for.this.Test";
    Representation rep = initNaturalLanguageTest(field);
    // Note that also String values need to be converted to texts with no language
    Set<String> textSet = new HashSet<String>(NL_TEST_all);
    Text text;
    Iterator<Text> noLangTexts = rep.get(field, (String) null);
    assertNotNull(noLangTexts);
    while (noLangTexts.hasNext()) {
        text = noLangTexts.next();
        assertNull(text.getLanguage());
        assertTrue(textSet.remove(text.getText()));
    }
    // check that both text where found
    assertTrue(textSet.size() == 4);
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 42 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testStringArrayToTextConversion.

/**
     * Checks if {@link Text} instances are correctly generated for String[]. This test depends also on the
     * correct implementation of the {@link Text#equals(Object)} method
     */
@Test
public void testStringArrayToTextConversion() {
    String field = "urn:the.field:used.for.this.Test";
    ValueFactory vf = getValueFactory();
    Representation rep = createRepresentation(null);
    // test conversion of String[] with language as second element
    String[] textWithLang = new String[] { "Test text with language", "en" };
    rep.add(field, textWithLang);
    Iterator<Text> refs = rep.get(field, (String[]) null);
    assertTrue(refs.hasNext());
    Text test = refs.next();
    assertEquals(textWithLang[1], test.getLanguage());
    assertEquals(textWithLang[0], test.getText());
    assertFalse(refs.hasNext());
    // test multiple adds do not generate duplicate References
    rep.add(field, textWithLang);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test adding a equivalent reference
    rep.add(field, vf.createText(textWithLang[0], textWithLang[1]));
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test removing
    rep.remove(field, textWithLang);
    assertFalse(rep.get(field).hasNext());
    // test conversion of String[] with only one element (default language)
    String[] textWithoutLang = new String[] { "Test text without language" };
    rep.add(field, textWithoutLang);
    refs = rep.get(field, (String[]) null);
    assertTrue(refs.hasNext());
    test = refs.next();
    assertNull(test.getLanguage());
    assertEquals(textWithoutLang[0], test.getText());
    assertFalse(refs.hasNext());
    // test multiple adds do not generate duplicate References
    rep.add(field, textWithoutLang);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test adding a equivalent reference
    rep.add(field, vf.createText(textWithoutLang[0]));
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test removing
    rep.remove(field, textWithoutLang);
    assertFalse(rep.get(field).hasNext());
    // test conversion of String[] with null as second element (default language)
    String[] textWithDefaultLang = new String[] { "Test text with default language", null };
    rep.add(field, textWithDefaultLang);
    refs = rep.get(field, (String[]) null);
    assertTrue(refs.hasNext());
    test = refs.next();
    assertNull(test.getLanguage());
    assertEquals(textWithDefaultLang[0], test.getText());
    assertFalse(refs.hasNext());
    // test multiple adds do not generate duplicate References
    rep.add(field, textWithDefaultLang);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test adding a equivalent reference
    rep.add(field, vf.createText(textWithDefaultLang[0], null));
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test removing
    rep.remove(field, textWithDefaultLang);
    assertFalse(rep.get(field).hasNext());
    // finally test if additional Elements are correctly ignored
    String[] ignoreAdditionalElements = new String[] { "Test if additional elements are ignored", "en", "ignored1", "ignored2", null, "ignored4" };
    String[] sameText = new String[] { "Test if additional elements are ignored", "en" };
    rep.add(field, ignoreAdditionalElements);
    refs = rep.get(field, (String[]) null);
    assertTrue(refs.hasNext());
    test = refs.next();
    assertEquals(ignoreAdditionalElements[1], test.getLanguage());
    assertEquals(ignoreAdditionalElements[0], test.getText());
    assertFalse(refs.hasNext());
    // test multiple adds do not generate duplicate References
    rep.add(field, ignoreAdditionalElements);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test if an Array with only the first two elements generate the same Text
    rep.add(field, sameText);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test removing
    rep.remove(field, ignoreAdditionalElements);
    assertFalse(rep.get(field).hasNext());
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) Test(org.junit.Test)

Example 43 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testRemoveAllTextsWithNullLanguage.

@Test
public void testRemoveAllTextsWithNullLanguage() {
    String field = "urn:the.field:used.for.this.Test";
    Representation rep = initNaturalLanguageTest(field);
    Set<String> textSet = new HashSet<String>(NL_TEST_all);
    // test removal of null language
    rep.removeAllNaturalText(field, (String) null);
    for (Iterator<Text> texts = rep.getText(field); texts.hasNext(); textSet.remove(texts.next().getText())) ;
    assertTrue(textSet.size() == 2);
    assertTrue(textSet.remove(NL_TEST_noLang));
    // and this should be the stringTest
    assertTrue(textSet.remove(NL_TEST_string));
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 44 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testAddNaturalLanguageTextForMultipleLanguages.

/**
     * Tests the feature to add one and the same natural language text for multiple languages
     */
public void testAddNaturalLanguageTextForMultipleLanguages() {
    String field = "urn:the.field:used.for.this.Test";
    Representation rep = createRepresentation(null);
    // add the same label for multiple language
    String[] languageArray = new String[] { "en", "de", null };
    rep.addNaturalText(field, NL_TEST_noLang, languageArray);
    Set<String> languages = new HashSet<String>(Arrays.asList(languageArray));
    Iterator<Text> texts = rep.get(field, (String[]) null);
    while (texts.hasNext()) {
        Text text = texts.next();
        assertTrue(languages.remove(text.getLanguage()));
        assertEquals(NL_TEST_noLang, text.getText());
    }
    assertTrue(languages.isEmpty());
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) HashSet(java.util.HashSet)

Example 45 with Text

use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.

the class RepresentationTest method testGetNaturalTextWithLanguage.

@Test
public void testGetNaturalTextWithLanguage() {
    String field = "urn:the.field:used.for.this.Test";
    Representation rep = initNaturalLanguageTest(field);
    // test de texts
    Iterator<Text> deTexts = rep.get(field, "de");
    assertNotNull(deTexts);
    // there is one German text in the test set
    assertTrue(deTexts.hasNext());
    Text text = deTexts.next();
    // "de" lang
    assertEquals(text.getLanguage(), "de");
    // the de lang text
    assertEquals(text.getText(), NL_TEST_de);
    // only one Result
    assertFalse(deTexts.hasNext());
    // test en labels (2 results)
    Iterator<Text> enTexts = rep.get(field, "en");
    assertNotNull(enTexts);
    Set<String> textSet = new HashSet<String>(Arrays.asList(NL_TEST_en, NL_TEST_en2));
    while (enTexts.hasNext()) {
        text = enTexts.next();
        assertEquals("en", text.getLanguage());
        // remove the found
        assertTrue(textSet.remove(text.getText()));
    }
    // all texts found
    assertTrue(textSet.isEmpty());
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Text (org.apache.stanbol.entityhub.servicesapi.model.Text)50 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)32 Test (org.junit.Test)24 HashSet (java.util.HashSet)14 Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)12 ArrayList (java.util.ArrayList)11 IRI (org.apache.clerezza.commons.rdf.IRI)6 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)4 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)4 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)4 RepresentationTest (org.apache.stanbol.entityhub.test.model.RepresentationTest)4 Graph (org.apache.clerezza.commons.rdf.Graph)3 Language (org.apache.clerezza.commons.rdf.Language)3 Literal (org.apache.clerezza.commons.rdf.Literal)3 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)3 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)3 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)3 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)3 URI (java.net.URI)2 URL (java.net.URL)2