use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class RdfRepresentation method removeTypedLiteral.
protected void removeTypedLiteral(IRI field, Object object) {
Literal literal;
try {
literal = RdfResourceUtils.createLiteral(object);
} catch (NoConvertorException e) {
log.info("No Converter for value type " + object.getClass() + " (parsed for field " + field + ") use toString() Method to get String representation");
literal = RdfResourceUtils.createLiteral(object.toString(), null);
}
graphNode.deleteProperty(field, literal);
}
use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class RdfResourceUtils method getLiteralValues.
/**
* Extracts the literal values for the given list of languages (<code>null</code>
* is supported).
* <p>
* Multiple languages are supported by this method to allow parsing
* <code>null</code> in addition to a language. This is often used by applications
* to search for literals in a given language in addition to literals with no
* defined language.
* <p>
* As a convenience this methods adds literals with a language tag to the
* front of the list and literals with no language tag to the end.
*
* @param literals the iterator over the literals
* @param languages the array of languages (<code>null</code> is supported).
* @return The collection with all the literal values.
*/
public static List<String> getLiteralValues(Iterator<Literal> literals, String... languages) {
//permits null element!
Set<Language> languageSet = new HashSet<Language>();
for (String lang : languages) {
languageSet.add(getLanguage(lang));
}
boolean containsNull = languageSet.contains(null);
List<String> results = new ArrayList<String>();
while (literals.hasNext()) {
Literal act = literals.next();
if (act.getLanguage() != null) {
if (languageSet.contains(act.getLanguage())) {
//add to front
results.add(0, act.getLexicalForm());
}
} else if (containsNull) {
//add also all types Literals, because the do not define an language!
//append to the end
results.add(act.getLexicalForm());
}
}
return results;
}
use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class RdfRepresentationTest method testTypedLiteralToTextConversion.
/**
* {@link TypedLiteral}s are used to represent literal values for different
* xsd dataTypes within Clerezza. This method tests of {@link TypedLiteral}s
* with the data type xsd:string are correctly treated like {@link String}
* values. This tests especially if they are treated as natural language
* texts without language.
*/
@Test
public void testTypedLiteralToTextConversion() {
String field = "urn:test.RdfRepresentation:test.field";
Literal stringLiteral = literalFactory.createTypedLiteral("This is a stirng value");
//also add an integer to test that other typed literals are not used as texts
Literal integerLiteral = literalFactory.createTypedLiteral(new Integer(5));
Representation rep = createRepresentation(null);
rep.add(field, Arrays.asList(stringLiteral, integerLiteral));
//test if the literal is returned when asking for natural language text without language
Iterator<Text> noLangTexts = rep.get(field, (String) null);
assertTrue(noLangTexts.hasNext());
assertEquals(stringLiteral.getLexicalForm(), noLangTexts.next().getText());
assertFalse(noLangTexts.hasNext());
//test that string literals are returned when asking for all natural language text values
Iterator<Text> texts = rep.getText(field);
assertTrue(texts.hasNext());
assertEquals(stringLiteral.getLexicalForm(), texts.next().getText());
assertFalse(texts.hasNext());
}
use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class ClerezzaRuleStore method findRulesByName.
@Override
public RuleList findRulesByName(String term) {
String sparql = "SELECT ?recipe ?rule ?description " + "WHERE { " + "?recipe " + Symbols.hasRule + " ?rule . " + "?rule " + Symbols.ruleName + " ?name . " + "?rule " + Symbols.description + " ?description . " + "FILTER (regex(?name, \"" + term + "\", \"i\"))" + "}";
List<IRI> recipeIDs = listRecipeIDs();
Graph[] tripleCollections = new Graph[recipeIDs.size()];
for (int i = 0; i < tripleCollections.length; i++) {
tripleCollections[i] = tcManager.getGraph(recipeIDs.get(i));
}
UnionGraph unionGraph = new UnionGraph(tripleCollections);
RuleList matchingRules = new RuleList();
try {
SelectQuery query = (SelectQuery) QueryParser.getInstance().parse(sparql);
ResultSet resultSet = tcManager.executeSparqlQuery(query, unionGraph);
while (resultSet.hasNext()) {
SolutionMapping solutionMapping = resultSet.next();
IRI recipeID = (IRI) solutionMapping.get("recipe");
IRI ruleID = (IRI) solutionMapping.get("rule");
Literal description = (Literal) solutionMapping.get("description");
try {
Recipe recipe = getRecipe(recipeID);
Rule rule = new RecipeRule(recipe, getRule(recipe, ruleID));
if (description != null) {
rule.setDescription(description.getLexicalForm());
}
matchingRules.add(rule);
} catch (NoSuchRecipeException e) {
// in this case go on in the iteration by fetching other matching recipes
} catch (RecipeConstructionException e) {
// in this case go on in the iteration by fetching other matching recipes
} catch (NoSuchRuleInRecipeException e) {
// in this case go on in the iteration by fetching other matching recipes
}
}
} catch (ParseException e) {
log.error("The sparql query contains errors: ", e);
}
return matchingRules;
}
use of org.apache.clerezza.commons.rdf.Literal in project stanbol by apache.
the class CeliLemmatizerEnhancementEngineTest method validateMorphoFeatureProperty.
/**
* [1..*] values of an {@link TypedLiteral} in the form {key=value}
* @param enhancements The graph with the enhancements
* @param textAnnotation the TextAnnotation to check
*/
private void validateMorphoFeatureProperty(Graph enhancements, BlankNodeOrIRI textAnnotation) {
//This taste checks for known morpho features of a given input (constant TERM)
Iterator<Triple> morphoFeatureIterator = enhancements.filter(textAnnotation, RDF_TYPE, null);
assertTrue("No POS Morpho Feature value found for TextAnnotation " + textAnnotation + "!", morphoFeatureIterator.hasNext());
while (morphoFeatureIterator.hasNext()) {
RDFTerm morphoFeature = morphoFeatureIterator.next().getObject();
assertTrue("Morpho Feature value are expected of typed literal", morphoFeature instanceof IRI);
String feature = ((IRI) morphoFeature).getUnicodeString();
assertFalse("Morpho Feature MUST NOT be empty", feature.isEmpty());
if (feature.startsWith(OLIA_NAMESPACE)) {
String key = feature.substring(OLIA_NAMESPACE.length());
LexicalCategory cat = LexicalCategory.valueOf(key);
assertTrue("Part of Speech of " + TERM + " should be " + LexicalCategory.Noun, (cat == LexicalCategory.Noun));
}
}
morphoFeatureIterator = enhancements.filter(textAnnotation, CeliMorphoFeatures.HAS_GENDER, null);
assertTrue("No Gender Morpho Feature value found for TextAnnotation " + textAnnotation + "!", morphoFeatureIterator.hasNext());
if (morphoFeatureIterator.hasNext()) {
RDFTerm morphoFeature = morphoFeatureIterator.next().getObject();
assertTrue("Morpho Feature value are expected of typed literal", morphoFeature instanceof IRI);
String feature = ((IRI) morphoFeature).getUnicodeString();
assertFalse("Morpho Feature MUST NOT be empty", feature.isEmpty());
if (feature.startsWith(OLIA_NAMESPACE)) {
String key = feature.substring(OLIA_NAMESPACE.length());
Gender cat = Gender.valueOf(key);
assertTrue("Gender of " + TERM + " should be " + Gender.Feminine, (cat == Gender.Feminine));
}
}
morphoFeatureIterator = enhancements.filter(textAnnotation, CeliMorphoFeatures.HAS_NUMBER, null);
assertTrue("No Number Morpho Feature value found for TextAnnotation " + textAnnotation + "!", morphoFeatureIterator.hasNext());
if (morphoFeatureIterator.hasNext()) {
RDFTerm morphoFeature = morphoFeatureIterator.next().getObject();
assertTrue("Morpho Feature value are expected of typed literal", morphoFeature instanceof IRI);
String feature = ((IRI) morphoFeature).getUnicodeString();
assertFalse("Morpho Feature MUST NOT be empty", feature.isEmpty());
if (feature.startsWith(OLIA_NAMESPACE)) {
String key = feature.substring(OLIA_NAMESPACE.length());
NumberFeature cat = NumberFeature.valueOf(key);
assertTrue("Number of " + TERM + " should be " + Gender.Feminine, (cat == NumberFeature.Singular));
}
}
morphoFeatureIterator = enhancements.filter(textAnnotation, CeliLemmatizerEnhancementEngine.hasLemmaForm, null);
assertTrue("No Number Morpho Feature value found for TextAnnotation " + textAnnotation + "!", morphoFeatureIterator.hasNext());
if (morphoFeatureIterator.hasNext()) {
RDFTerm morphoFeature = morphoFeatureIterator.next().getObject();
assertTrue("Lemma Forms value are expected of type Literal", morphoFeature instanceof Literal);
assertFalse("Lemma forms MUST NOT be empty", ((Literal) morphoFeature).getLexicalForm().isEmpty());
String feature = ((Literal) morphoFeature).getLexicalForm();
assertTrue("Lemma of " + TERM + " should be " + TERM, (feature.equals(TERM)));
}
}
Aggregations