【问题标题】:Column name with spaces in tarqltarql 中带空格的列名
【发布时间】:2016-10-30 11:09:34
【问题描述】:

我正在使用 tarql (https://github.com/tarql/tarql) - 使用 sparql 语法 - 将 CSV 数据转换为 RDF 三元组。

我有一个列名“网站”。如何使用 BIND 函数绑定到变量?我尝试了很多方法,但我没有找到解决方案:

BIND (?web site AS ?homepage)
BIND (?"web site" AS ?homepage)
BIND (?'web site' AS ?homepage)
BIND (?web\ site AS ?homepage)

都导致解析错误。

【问题讨论】:

  • 根据github.com/tarql/tarql/blob/master/src/main/java/org/deri/tarql/…应该是下划线,即BIND (?web_site AS ?homepage)。这对你有用吗?
  • 是的,我可以转义','吗?
  • 什么意思?列名中的逗号?如果您查看源代码(github.com/tarql/tarql/blob/master/src/main/java/org/deri/tarql/…),空格、连字符和问号将被下划线替换,所有其他取决于 Apache Jena 是否可以解析字符串。但也有一个 FIXME 条目,即某些字符可能不允许。
  • 是的,我的意思是像这样的标题:“column1;column2;column3,以及 column3;column4”
  • 鉴于分隔符是;,要转换为变量的字符串将是“column3, and something to column3”。这将导致replace 行产生“column3,_and_something_to_column3”。我不知道来自 Apache Jena 的 Var.alloc 是否可以解析这个字符串。不过,如果它不起作用,你应该得到一个例外。

标签: csv sparql rdf triples


【解决方案1】:

当你必须处理复杂的情况时,我的建议是:首先尝试进行探索性测试;让我们通过例子来看看:

假设您的源数据文件是:./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

lancher 示例:

#!/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 不支持的词法错误

在这种情况下,与其继续与语言斗争,我更愿意采用一些解决方法

第 3 步:有一点解决方法的解决方案

让我们利用选项-H --no-header-row CSV file has no header row; use variable names ?a, ?b, ... 并享受一个简单的解决方案;我们需要做的就是从我们的源数据文件的内容中删除第一行(这是一项简单的任务,您可以通过管道传输到流程或以您喜欢的方式执行)为了方便测试我复制的数据没有@中的第一列987654337@.

现在同样的查询对解析器来说变得更容易了; ./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)
  }

launcher-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  |
-------------------------------------------------------------------------------

注意

  1. 参考文档: Header row, delimiters, quotes and character encoding in CSV/TSV files 说明了表达选项的所有可能方式和组合:值得一读。

  2. 另一个有用的参考可能是: SPARQL 1.1 查询语言中的Possible names for variables

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    相关资源
    最近更新 更多