Search in sources :

Example 61 with Literal

use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.

the class RdfRepresentation method removeAllNaturalText.

@Override
public void removeAllNaturalText(String field, String... languages) {
    if (field == null) {
        throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
    } else if (field.isEmpty()) {
        throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
    }
    //        if(languages == null || languages.length == 0){
    //            languages = new String []{null};
    //        }
    IRI fieldIRI = new IRI(field);
    //get all the affected Literals
    Collection<Literal> toRemove = new ArrayList<Literal>();
    Iterator<Literal> it = new FilteringIterator<Literal>(graphNode.getLiterals(fieldIRI), new NaturalTextFilter(languages), Literal.class);
    while (it.hasNext()) {
        toRemove.add(it.next());
    }
    for (Literal l : toRemove) {
        graphNode.deleteProperty(fieldIRI, l);
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) NaturalTextFilter(org.apache.stanbol.entityhub.model.clerezza.impl.NaturalTextFilter) Literal(org.apache.clerezza.commons.rdf.Literal) FilteringIterator(org.apache.stanbol.entityhub.servicesapi.util.FilteringIterator) ArrayList(java.util.ArrayList)

Example 62 with Literal

use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.

the class RdfRepresentationTest method testTypedLiteralToValueConversion.

/**
     * {@link TypedLiteral}s are used to represent literal values for different
     * xsd dataTypes within Clerezza. This method tests if xsd dataTypes are
     * converted to the corresponding java types. 
     * This is dependent on the {@link LiteralFactory} implementation used by
     * the {@link RdfRepresentation} implementation.
     */
@SuppressWarnings("unchecked")
@Test
public void testTypedLiteralToValueConversion() {
    String field = "urn:test.RdfRepresentation:test.field";
    Integer integerValue = 5;
    Literal integerLiteral = literalFactory.createTypedLiteral(integerValue);
    Date dateValue = new Date();
    Literal dateLiteeral = literalFactory.createTypedLiteral(dateValue);
    Double doubleValue = Math.PI;
    Literal doubleLiteral = literalFactory.createTypedLiteral(doubleValue);
    String stringValue = "This is a string literal value";
    Literal stringLiteral = literalFactory.createTypedLiteral(stringValue);
    Representation rep = createRepresentation(null);
    Collection<Literal> typedLiterals = Arrays.asList(integerLiteral, doubleLiteral, stringLiteral, dateLiteeral);
    rep.add(field, typedLiterals);
    //now check that such values are available via TypedLiteral
    Iterator<Literal> typedLiteralValues = rep.get(field, Literal.class);
    int size = 0;
    while (typedLiteralValues.hasNext()) {
        assertTrue(typedLiterals.contains(typedLiteralValues.next()));
        size++;
    }
    assertTrue(typedLiterals.size() == size);
    //now check that the values are available via the java object types
    //1) integer
    Iterator<Integer> intValues = rep.get(field, Integer.class);
    assertTrue(intValues.hasNext());
    assertEquals(integerValue, intValues.next());
    assertFalse(intValues.hasNext());
    //2) double
    Iterator<Double> doubleValues = rep.get(field, Double.class);
    assertTrue(doubleValues.hasNext());
    assertEquals(doubleValue, doubleValues.next());
    assertFalse(doubleValues.hasNext());
    //3) string
    Iterator<String> stringValues = rep.get(field, String.class);
    assertTrue(stringValues.hasNext());
    assertEquals(stringValue, stringValues.next());
    assertFalse(stringValues.hasNext());
    //4) date
    Iterator<Date> dateValues = rep.get(field, Date.class);
    assertTrue(dateValues.hasNext());
    assertEquals(dateValue, dateValues.next());
    assertFalse(dateValues.hasNext());
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Date(java.util.Date) Literal(org.apache.clerezza.commons.rdf.Literal) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 63 with Literal

use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.

the class RdfResourceUtils method getLiteralValues.

/**
     * Extracts the literal values for {@link Literal} instances.
     *
     * @param literals the Iterator holding the literals
     * @return The collection with the literal values
     */
public static Collection<String> getLiteralValues(Iterator<Literal> literals) {
    Collection<String> results = new ArrayList<String>();
    while (literals.hasNext()) {
        Literal act = literals.next();
        results.add(act.getLexicalForm());
    }
    return results;
}
Also used : Literal(org.apache.clerezza.commons.rdf.Literal) ArrayList(java.util.ArrayList)

Example 64 with Literal

use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.

the class RdfRepresentationTest method testPlainLiteralToTextConversion.

/*--------------------------------------------------------------------------
     * Additional Tests for special Features of the Clerezza based implementation
     * 
     * This includes mainly support for additional types like PlainLiteral,
     * TypedLiteral, IRIs. The conversion to such types as well as getter for
     * such types.
     *--------------------------------------------------------------------------
     */
/**
     * {@link PlainLiteral} is used for natural language text in the Clerezza
     * RDF API. This tests if adding {@link PlainLiteral}s to the
     * {@link Representation#add(String, Object)} method makes them available
     * as {@link Text} instances via the {@link Representation} API (e.g. 
     * {@link Representation#get(String, String...)}).
     */
@Test
public void testPlainLiteralToTextConversion() {
    String field = "urn:test.RdfRepresentation:test.field";
    Literal noLangLiteral = new PlainLiteralImpl("A plain literal without Language");
    Literal enLiteral = new PlainLiteralImpl("An english literal", new Language("en"));
    Literal deLiteral = new PlainLiteralImpl("Ein Deutsches Literal", new Language("de"));
    Literal deATLiteral = new PlainLiteralImpl("Ein Topfen Verband hilft bei Zerrungen", new Language("de-AT"));
    Collection<Literal> plainLiterals = Arrays.asList(noLangLiteral, enLiteral, deLiteral, deATLiteral);
    Representation rep = createRepresentation(null);
    rep.add(field, plainLiterals);
    //now test, that the Plain Literals are available as natural language
    //tests via the Representation Interface!
    //1) one without a language
    Iterator<Text> noLangaugeTexts = rep.get(field, (String) null);
    assertTrue(noLangaugeTexts.hasNext());
    Text noLanguageText = noLangaugeTexts.next();
    assertEquals(noLangLiteral.getLexicalForm(), noLanguageText.getText());
    assertNull(noLanguageText.getLanguage());
    //only a single result
    assertFalse(noLangaugeTexts.hasNext());
    //2) one with a language
    Iterator<Text> enLangaugeTexts = rep.get(field, "en");
    assertTrue(enLangaugeTexts.hasNext());
    Text enLangageText = enLangaugeTexts.next();
    assertEquals(enLiteral.getLexicalForm(), enLangageText.getText());
    assertEquals(enLiteral.getLanguage().toString(), enLangageText.getLanguage());
    //only a single result
    assertFalse(enLangaugeTexts.hasNext());
    //3) test to get all natural language values
    Set<String> stringValues = new HashSet<String>();
    for (Literal plainLiteral : plainLiterals) {
        stringValues.add(plainLiteral.getLexicalForm());
    }
    Iterator<Text> texts = rep.getText(field);
    while (texts.hasNext()) {
        assertTrue(stringValues.remove(texts.next().getText()));
    }
    assertTrue(stringValues.isEmpty());
}
Also used : Language(org.apache.clerezza.commons.rdf.Language) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) Literal(org.apache.clerezza.commons.rdf.Literal) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) HashSet(java.util.HashSet) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 65 with Literal

use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.

the class ClerezzaRDFParser method handleStatement.

private void handleStatement(RDFDataset result, Triple t, Map<BlankNode, String> bNodeMap) {
    final String subject = getResourceValue(t.getSubject(), bNodeMap);
    final String predicate = getResourceValue(t.getPredicate(), bNodeMap);
    final RDFTerm object = t.getObject();
    if (object instanceof Literal) {
        final String value = ((Literal) object).getLexicalForm();
        final String language;
        final String datatype;
        datatype = getResourceValue(((Literal) object).getDataType(), bNodeMap);
        Language l = ((Literal) object).getLanguage();
        if (l == null) {
            language = null;
        } else {
            language = l.toString();
        }
        result.addTriple(subject, predicate, value, datatype, language);
        count++;
    } else {
        result.addTriple(subject, predicate, getResourceValue((BlankNodeOrIRI) object, bNodeMap));
        count++;
    }
}
Also used : Language(org.apache.clerezza.commons.rdf.Language) Literal(org.apache.clerezza.commons.rdf.Literal) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm)

Aggregations

Literal (org.apache.clerezza.commons.rdf.Literal)71 IRI (org.apache.clerezza.commons.rdf.IRI)35 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)35 Triple (org.apache.clerezza.commons.rdf.Triple)30 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)22 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)20 ArrayList (java.util.ArrayList)16 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)16 Language (org.apache.clerezza.commons.rdf.Language)12 Graph (org.apache.clerezza.commons.rdf.Graph)11 Test (org.junit.Test)10 HashSet (java.util.HashSet)9 Date (java.util.Date)8 Lock (java.util.concurrent.locks.Lock)6 Entity (org.apache.stanbol.enhancer.engines.entitylinking.Entity)5 HashMap (java.util.HashMap)4 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)4 NoConvertorException (org.apache.clerezza.rdf.core.NoConvertorException)4 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)4 Collection (java.util.Collection)3