Search in sources :

Example 76 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class ConfigurableAnalyzerAssembler method toFilterList.

private List<String> toFilterList(Resource list) {
    List<String> result = new ArrayList<>();
    Resource current = list;
    while (current != null && !current.equals(RDF.nil)) {
        Statement stmt = current.getProperty(RDF.first);
        if (stmt == null) {
            throw new TextIndexException("filter list not well formed");
        }
        RDFNode node = stmt.getObject();
        if (!node.isResource()) {
            throw new TextIndexException("filter is not a resource : " + node);
        }
        result.add(node.asResource().getURI());
        stmt = current.getProperty(RDF.rest);
        if (stmt == null) {
            throw new TextIndexException("filter list not terminated by rdf:nil");
        }
        node = stmt.getObject();
        if (!node.isResource()) {
            throw new TextIndexException("filter list node is not a resource : " + node);
        }
        current = node.asResource();
    }
    return result;
}
Also used : TextIndexException(org.apache.jena.query.text.TextIndexException) Statement(org.apache.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 77 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class DefineFiltersAssembler method open.

/*
    <#indexLucene> a text:TextIndexLucene ;
        text:directory <file:Lucene> ;
        text:entityMap <#entMap> ;
        text:defineAnalyzers (
            [text:addLang "sa-x-iast" ;
             text:analyzer [ . . . ]]
            [text:defineAnalyzer <#foo> ;
             text:analyzer [ . . . ]]
            [text:defineFilter <#bar> ;
             text:filter [ . . . ]]
            [text:defineTokenizer <#baz> ;
             text:tokenizer [ . . . ]]
        )
    */
public static boolean open(Assembler a, Resource list) {
    Resource current = list;
    while (current != null && !current.equals(RDF.nil)) {
        Statement firstStmt = current.getProperty(RDF.first);
        if (firstStmt == null) {
            throw new TextIndexException("parameter list not well formed: " + current);
        }
        RDFNode first = firstStmt.getObject();
        if (!first.isResource()) {
            throw new TextIndexException("parameter specification must be an anon resource : " + first);
        }
        // process the current list element to add an analyzer
        Resource adding = (Resource) first;
        if (adding.hasProperty(TextVocab.pFilter)) {
            Statement filterStmt = adding.getProperty(TextVocab.pFilter);
            RDFNode filterNode = filterStmt.getObject();
            if (!filterNode.isResource()) {
                throw new TextIndexException("addFilters text:filter must be a filter spec resource: " + filterNode);
            }
            // calls GenericFilterAssembler
            FilterSpec filterSpec = (FilterSpec) a.open((Resource) filterNode);
            if (adding.hasProperty(TextVocab.pDefFilter)) {
                Statement defStmt = adding.getProperty(TextVocab.pDefFilter);
                Resource id = defStmt.getResource();
                if (id.getURI() != null) {
                    ConfigurableAnalyzer.defineFilter(id.getURI(), filterSpec);
                } else {
                    throw new TextIndexException("text:defineFilters text:defineAnalyzer property must be a non-blank resource: " + adding);
                }
            } else {
                Log.warn("DefineFiltersAssembler", "Filter specified but no text:defineFilter so filter is not accessible!");
            }
        }
        Statement restStmt = current.getProperty(RDF.rest);
        if (restStmt == null) {
            throw new TextIndexException("parameter list not terminated by rdf:nil");
        }
        RDFNode rest = restStmt.getObject();
        if (!rest.isResource()) {
            throw new TextIndexException("parameter list node is not a resource : " + rest);
        }
        current = (Resource) rest;
    }
    return true;
}
Also used : TextIndexException(org.apache.jena.query.text.TextIndexException) Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) FilterSpec(org.apache.jena.query.text.assembler.GenericFilterAssembler.FilterSpec) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 78 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project jena by apache.

the class Params method getParamSpec.

protected static ParamSpec getParamSpec(Resource node) {
    Statement nameStmt = node.getProperty(TextVocab.pParamName);
    Statement valueStmt = node.getProperty(TextVocab.pParamValue);
    String name = getStringValue(nameStmt);
    String type = getType(node);
    String value = getStringValue(valueStmt);
    switch(type) {
        // String
        case TYPE_STRING:
            {
                if (value == null) {
                    throw new TextIndexException("Value for string param: " + name + " must not be empty!");
                }
                return new ParamSpec(name, value, String.class);
            }
        // java.io.FileReader
        case TYPE_FILE:
            {
                if (value == null) {
                    throw new TextIndexException("Value for file param must exist and must contain a file name.");
                }
                try {
                    // The analyzer is responsible for closing the file
                    Reader fileReader = new java.io.FileReader(value);
                    return new ParamSpec(name, fileReader, Reader.class);
                } catch (java.io.FileNotFoundException ex) {
                    throw new TextIndexException("File " + value + " for param " + name + " not found!");
                }
            }
        // org.apache.lucene.analysis.util.CharArraySet
        case TYPE_SET:
            {
                if (valueStmt == null) {
                    throw new TextIndexException("A set param spec must have a text:paramValue:" + node);
                }
                RDFNode valueNode = valueStmt.getObject();
                if (!valueNode.isResource()) {
                    throw new TextIndexException("A set param spec text:paramValue must be a list of strings: " + valueNode);
                }
                List<String> values = toStrings((Resource) valueNode);
                return new ParamSpec(name, new CharArraySet(values, false), CharArraySet.class);
            }
        // int
        case TYPE_INT:
            if (value == null) {
                throw new TextIndexException("Value for int param: " + name + " must not be empty!");
            }
            int n = ((Literal) valueStmt.getObject()).getInt();
            return new ParamSpec(name, n, int.class);
        // boolean
        case TYPE_BOOL:
            if (value == null) {
                throw new TextIndexException("Value for boolean param: " + name + " must not be empty!");
            }
            boolean b = ((Literal) valueStmt.getObject()).getBoolean();
            return new ParamSpec(name, b, boolean.class);
        // org.apache.lucene.analysis.Analyzer
        case TYPE_ANALYZER:
            if (valueStmt == null) {
                throw new TextIndexException("Analyzer param spec must have a text:paramValue:" + node);
            }
            RDFNode valueNode = valueStmt.getObject();
            if (!valueNode.isResource()) {
                throw new TextIndexException("Analyzer param spec text:paramValue must be an analyzer spec resource: " + valueNode);
            }
            Analyzer analyzer = (Analyzer) Assembler.general.open((Resource) valueNode);
            return new ParamSpec(name, analyzer, Analyzer.class);
        default:
            // there was no match
            Log.error("org.apache.jena.query.text.assembler.Params", "Unknown parameter type: " + type + " for param: " + name + " with value: " + value);
            break;
    }
    return null;
}
Also used : CharArraySet(org.apache.lucene.analysis.CharArraySet) TextIndexException(org.apache.jena.query.text.TextIndexException) Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) Reader(java.io.Reader) Analyzer(org.apache.lucene.analysis.Analyzer) Literal(org.apache.jena.rdf.model.Literal) ArrayList(java.util.ArrayList) List(java.util.List) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 79 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project Info-Evaluation by TechnionYP5777.

the class SqlRunner method getPersonalInfo.

public TableEntry getPersonalInfo(int wikiPageID) throws ClassNotFoundException, SQLException, IOException, ParseException {
    Extractor ext = new Extractor(wikiPageID);
    ext.executeQuery(QueryTypes.ABSTRACT_BY_WIKI_PAGE_ID);
    ResultSetRewindable results = ext.getResults();
    results.reset();
    QuerySolution solution = results.nextSolution();
    RDFNode overview = solution.get("abstract");
    String overviewStr = "No Abstract";
    if (overview != null)
        if (overview.isResource())
            overviewStr = (overview.asResource() + "").split("resource/")[1];
        else if (overview.isLiteral())
            overviewStr = (overview.asLiteral() + "").split("@")[0];
    ext.executeQuery(QueryTypes.BASIC_INFO_BY_WIKI_PAGE_ID);
    ResultSetRewindable basicInfoByIdResults = ext.getResults();
    basicInfoByIdResults.reset();
    TableEntry te = SqlTablesFiller.getBasicInfo(basicInfoByIdResults);
    TableEntry result = new TableEntry(te);
    result.setUrl(wikiURL + wikiPageID);
    result.setOverview(overviewStr);
    String photoLink = result.getPhotoLink();
    photoLink.replaceAll("'", "\'");
    int containsQuesmark = photoLink.indexOf("?");
    if (containsQuesmark != -1) {
        photoLink = photoLink.substring(0, containsQuesmark);
    }
    result.setPhotoLink(photoLink);
    return result;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Extractor(infoeval.main.WikiData.Extractor) ResultSetRewindable(org.apache.jena.query.ResultSetRewindable) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 80 with RDFNode

use of org.apache.jena.rdf.model.RDFNode in project Info-Evaluation by TechnionYP5777.

the class ExtractorTest method abstractByWikiPageIdTest.

@Ignore
@Test
public void abstractByWikiPageIdTest() throws Exception {
    int wikiPageID = Integer.parseInt((Jsoup.connect("https://en.wikipedia.org/w/api.php?action=query&titles=Shakira&prop=pageimages&format=xml&pithumbsize=350").get() + "").split("pageid=\"")[1].split("\"")[0]);
    Extractor extr = new Extractor(wikiPageID);
    extr.executeQuery(QueryTypes.ABSTRACT_BY_WIKI_PAGE_ID);
    ResultSetRewindable results = extr.getResults();
    results.reset();
    RDFNode overview = results.nextSolution().get("abstract");
    String overviewStr = "No Abstract";
    if (overview != null)
        if (overview.isResource())
            overviewStr = (overview.asResource() + "").split("resource/")[1];
        else if (overview.isLiteral())
            overviewStr = (overview.asLiteral() + "").split("@")[0];
    assert overviewStr.contains("is a Colombian singer, songwriter, dancer, record producer, choreographer, and model");
}
Also used : Extractor(infoeval.main.WikiData.Extractor) ResultSetRewindable(org.apache.jena.query.ResultSetRewindable) RDFNode(org.apache.jena.rdf.model.RDFNode) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

RDFNode (org.apache.jena.rdf.model.RDFNode)145 Resource (org.apache.jena.rdf.model.Resource)51 Model (org.apache.jena.rdf.model.Model)30 Statement (org.apache.jena.rdf.model.Statement)27 Test (org.junit.Test)26 ArrayList (java.util.ArrayList)22 Node (org.apache.jena.graph.Node)20 QuerySolution (org.apache.jena.query.QuerySolution)20 TextIndexException (org.apache.jena.query.text.TextIndexException)17 Property (org.apache.jena.rdf.model.Property)17 Literal (org.apache.jena.rdf.model.Literal)15 SecuredRDFNode (org.apache.jena.permissions.model.SecuredRDFNode)13 Dataset (org.apache.jena.query.Dataset)13 Triple (org.apache.jena.graph.Triple)12 QueryExecution (org.apache.jena.query.QueryExecution)10 StmtIterator (org.apache.jena.rdf.model.StmtIterator)9 Query (org.apache.jena.query.Query)8 NodeIterator (org.apache.jena.rdf.model.NodeIterator)8 RDFList (org.apache.jena.rdf.model.RDFList)8 AccessDeniedException (org.apache.jena.shared.AccessDeniedException)7