Search in sources :

Example 1 with Node

use of org.ontoware.rdf2go.model.node.Node in project stanbol by apache.

the class RDF2GoUtils method urifyBlankNodes.

public static void urifyBlankNodes(Model model) {
    HashMap<BlankNode, URI> nodeMap = new HashMap<BlankNode, URI>();
    Model add = RDF2Go.getModelFactory().createModel();
    add.open();
    Model remove = RDF2Go.getModelFactory().createModel();
    remove.open();
    for (Statement stmt : model) {
        RDFTerm subj = stmt.getSubject();
        URI pred = stmt.getPredicate();
        Node obj = stmt.getObject();
        boolean match = false;
        if (subj instanceof BlankNode) {
            match = true;
            URI newSubj = nodeMap.get(subj);
            if (newSubj == null) {
                newSubj = URIGenerator.createNewRandomUniqueURI();
                nodeMap.put(subj.asBlankNode(), newSubj);
            }
            subj = newSubj;
        }
        if (obj instanceof BlankNode) {
            match = true;
            URI newObj = nodeMap.get(obj);
            if (newObj == null) {
                newObj = URIGenerator.createNewRandomUniqueURI();
                nodeMap.put(obj.asBlankNode(), newObj);
            }
            obj = newObj;
        }
        if (match) {
            remove.addStatement(stmt);
            add.addStatement(subj, pred, obj);
        }
    }
    ClosableIterator<Statement> addIt = add.iterator();
    ClosableIterator<Statement> removeIt = remove.iterator();
    model.update(new DiffImpl(addIt, removeIt));
    addIt.close();
    removeIt.close();
    add.close();
    remove.close();
}
Also used : HashMap(java.util.HashMap) Statement(org.ontoware.rdf2go.model.Statement) Node(org.ontoware.rdf2go.model.node.Node) BlankNode(org.ontoware.rdf2go.model.node.BlankNode) BlankNode(org.ontoware.rdf2go.model.node.BlankNode) Model(org.ontoware.rdf2go.model.Model) DiffImpl(org.ontoware.rdf2go.model.impl.DiffImpl) RDFTerm(org.ontoware.rdf2go.model.node.RDFTerm) URI(org.ontoware.rdf2go.model.node.URI)

Example 2 with Node

use of org.ontoware.rdf2go.model.node.Node in project stanbol by apache.

the class MetaxaCore method getText.

/**
     * Returns a documents plain text if contained in the given extracted
     * metadata.
     *
     * @param model
     *            a {@link Model} with the extracted metadata
     * @return a {@link String} with the plain text content or {@code null} if
     *         no plain text was contained in the extracted metadata
     */
public static String getText(Model model) {
    String result = null;
    ClosableIterator<Statement> statements = null;
    try {
        statements = model.findStatements(Variable.ANY, NIE.plainTextContent, Variable.ANY);
        StringBuilder text = new StringBuilder(10000);
        while (statements.hasNext()) {
            Statement statement = statements.next();
            Node value = statement.getObject();
            if (value instanceof Literal) {
                text.append(((Literal) value).getValue());
            }
        }
        result = text.toString().trim();
        if (result.length() == 0) {
            result = null;
        }
    } finally {
        if (statements != null) {
            statements.close();
        }
    }
    return result;
}
Also used : Statement(org.ontoware.rdf2go.model.Statement) Node(org.ontoware.rdf2go.model.node.Node) Literal(org.ontoware.rdf2go.model.node.Literal)

Aggregations

Statement (org.ontoware.rdf2go.model.Statement)2 Node (org.ontoware.rdf2go.model.node.Node)2 HashMap (java.util.HashMap)1 Model (org.ontoware.rdf2go.model.Model)1 DiffImpl (org.ontoware.rdf2go.model.impl.DiffImpl)1 BlankNode (org.ontoware.rdf2go.model.node.BlankNode)1 Literal (org.ontoware.rdf2go.model.node.Literal)1 RDFTerm (org.ontoware.rdf2go.model.node.RDFTerm)1 URI (org.ontoware.rdf2go.model.node.URI)1