当您必须处理复杂的情况时,我的建议是:首先尝试进行探索性测试;让我们通过例子来看看:
假设您的源数据文件是:./table/table.csv
其中包含:
main index;web site;title, to translate
1;"ciao.ronda.com";"this is the first"
2;"miao.ronda.it";"this is the second"
3;"bao.ronda.uk";"this is the third"
step1:探索性测试查询qstar.sparql
:
SELECT *
FROM <file:table.csv#delimiter=%3B;>
WHERE {}
LIMIT 100
兰切尔示例:
#!/bin/bash -
table=./data/table.csv
query=./data/qstar.sparql
./bin/tarql --test --delimiter \; --header-row --verbose ${query} ${table}
结果:
$ ./launcher0.sh
--------------------------------------------------------
| main_index | web_site | title,_to_translate |
========================================================
| "1" | "ciao.ronda.com" | "this is the first" |
| "2" | "miao.ronda.it" | "this is the second" |
| "3" | "bao.ronda.uk" | "this is the third" |
--------------------------------------------------------
现在我们知道使用这些选项计算的第三列变量名称是:title,_to_translate
step2:测试 BIND 语句的语法是否支持使用收益变量名称(title,_to_translate
在我们的示例中)
这里我们需要一个基于示例BIND
的查询来理解问题;假设这是我们尝试使用 out 字段的查询:?title,_to_translate
SELECT ?homepage ?uri ?title_with_language_tag
WHERE {
BIND (?web_site AS ?homepage)
BIND (URI(CONCAT('http://website.com/ns#', ?main_index)) AS ?uri)
BIND (STRLANG(?title,_to_translate, 'en') AS ?title_with_language_tag)
}
结果:
$ ./launcher0.sh
com.hp.hpl.jena.query.QueryParseException: Lexical error at line 5, column 27. Encountered: "t" (116), after : "_"
at org.deri.tarql.TarqlParser.parse(TarqlParser.java:113)
简而言之,此查询包含不支持的词法错误ena.query.QueryParser
在这种情况下,与其继续与语言抗争,我更愿意采用一些解决方法
step3: 有一点解决方法的解决方案
让我们利用该选项-H --no-header-row CSV file has no header row; use variable names ?a, ?b, ...
并享受一个简单的解决方案;我们需要做的就是从我们的源数据文件的内容中删除第一行(这是一个简单的任务,您可以通过管道传输到流程或以您喜欢的方式执行)为了方便测试我复制了没有第一列的数据./data/table0-noheader.csv
。
现在同样的查询对解析器来说变得更容易了;
./data/query0.sparql
:
SELECT ?homepage ?uri ?title_with_language_tag
WHERE {
BIND (?a AS ?homepage)
BIND (URI(CONCAT('http://website.com/ns#', ?b)) AS ?uri)
BIND (STRLANG(?c, 'en') AS ?title_with_language_tag)
}
启动器-noheader.sh:
!/bin/bash -
table=./data/table0-noheader.csv
query=./data/query0.sparql
./bin/tarql --test --no-header-row --delimiter \; --header-row --verbose ${query} ${table}
结果:
$ ./launcher-noheader.sh
-------------------------------------------------------------------------------
| homepage | uri | title_with_language_tag |
===============================================================================
| "1" | <http://website.com/ns#ciao.ronda.com> | "this is the first"@en |
| "2" | <http://website.com/ns#miao.ronda.it> | "this is the second"@en |
| "3" | <http://website.com/ns#bao.ronda.uk> | "this is the third"@en |
-------------------------------------------------------------------------------
笔记
参考文档:
CSV/TSV 文件中的标题行、分隔符、引号和字符编码说明了表达选项的所有可能方式和组合:值得一读。
另一个有用的参考可能是:
SPARQL 1.1 Query Language中变量的可能名称