使用IPython Notebook(使用rdflib?)来记录SPARQL查询和结果(Using IPython Notebook (with rdflib?) to log SPARQL queries and results)
我想使用IPython Notebook记录SPARQL查询以及这些查询的结果。
由于任何命令行工具都可以通过“bang”从IPython Notebook调用,我当然可以运行:
!arq --data dcterms.ttl --query test1.rq
或者使用roqet,我甚至可以在命令本身中嵌入一个简短的查询:
!roqet -i sparql -e ’SELECT * WHERE { ?s ?p ?o }’ -D dcterms.rdf
arq或roqet都不接受多行SPARQL查询作为参数。 任何长于一行的查询都必须存储在文件中(例如,如上所述的“test1.rq”)。
更好的方法是直接在IPython Notebook单元中定义SPARQL查询,在那里可以轻松地克隆和调整它们。 以下作品:
In [4]: myquery = """ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> CONSTRUCT WHERE {?s rdf:type ?o} """ In [5]: def turtleme(myquery): import rdflib g = rdflib.Graph() g.parse('dcam.rdf') results = g.query(myquery) print results.serialize(format="turtle") In [6]: turtleme(myquery) Out [6]: @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xml: <http://www.w3.org/XML/1998/namespace> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://purl.org/dc/dcam/VocabularyEncodingScheme> a rdfs:Class . <http://purl.org/dc/dcam/memberOf> a rdf:Property .
但是,我没有看到传递指定要查询的数据源的SPARQL查询的方法,例如:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> CONSTRUCT FROM <dcterms.ttl> FROM <dcam.ttl> WHERE {?s rdf:type ?o}
或者,至少要改进功能,以便它至少需要一个文件名作为参数,如
turtleme('dcam.ttl', myquery)
我已经搜索了谷歌的热门搜索,以获得使用带有SPARQL的IPython Notebook但没有找到的例子。 对于专为数据探索而设计的环境而言,这似乎是一个明显的用途 我发现真正有效的唯一方法是运行arq,但是人们需要这样做
!cat test3.rq
将查询粘贴到IPython Notebook中,它完成了记录数据探索过程的功能,但查询必须与笔记本并行编辑,作为单独的文件。 我的目标是让初学者轻松使用SPARQL探索RDF数据并在笔记本中记录他们的探索。 肯定有更好的办法!
更新:
@Joshua Taylor,@ AndyS指出这些命令接受多行查询作为参数。 这在bash提示符下工作正常,但遗憾的是不在IPython Notebook中,它会抛出一个SyntaxError:
In [5]: !arq --data dcam.ttl ' PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX dcam: <http://purl.org/dc/dcam/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?s ?p ?o WHERE { ?s ?p ?o . }' Out [5]: File "<ipython-input-5-c9328c1c0c64>", line 2 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ^ SyntaxError: invalid syntax
如果我在第1行中退出行尾,就像在
In [5]: !arq --data dcam.ttl '\ ... Out [5]: File "<ipython-input-18-313c556abc1d>", line 2 PREFIX dcam: <http://purl.org/dc/dcam/> ^ SyntaxError: invalid syntax
但是,我无法通过转义行的所有末尾来执行整个命令。
所以问题可能不在于arq和roqet如何在线处理查询,而是如何将arq和roqet命令行传递给IPython Notebook?
I want to use IPython Notebook to record SPARQL queries together with the results of those queries.
Since any command-line tool can be called from IPython Notebook with a "bang", I can of course run:
!arq --data dcterms.ttl --query test1.rq
or with roqet, I can even embed a short query in the command itself:
!roqet -i sparql -e ’SELECT * WHERE { ?s ?p ?o }’ -D dcterms.rdf
Neither arq or roqet accept multi-line SPARQL queries as arguments. Any query longer than a one-liner must be stored in a file (e.g., "test1.rq" as above).
Far better would be to define SPARQL queries directly in IPython Notebook cells, where they could easily be cloned and tweaked. The following works:
In [4]: myquery = """ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> CONSTRUCT WHERE {?s rdf:type ?o} """ In [5]: def turtleme(myquery): import rdflib g = rdflib.Graph() g.parse('dcam.rdf') results = g.query(myquery) print results.serialize(format="turtle") In [6]: turtleme(myquery) Out [6]: @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xml: <http://www.w3.org/XML/1998/namespace> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . <http://purl.org/dc/dcam/VocabularyEncodingScheme> a rdfs:Class . <http://purl.org/dc/dcam/memberOf> a rdf:Property .
However, I do not see a way to pass a SPARQL query that specifies the data sources to be queried, such as:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> CONSTRUCT FROM <dcterms.ttl> FROM <dcam.ttl> WHERE {?s rdf:type ?o}
or, at a minimum, to improve the function so that it will take at least one filename as an argument, as in
turtleme('dcam.ttl', myquery)
I have scoured Google hits for examples of using IPython Notebook with SPARQL but find none. It seems like an obvious use for an environment designed for data exploration. The only method I have found that really works is to run arq, but then one needs to do
!cat test3.rq
to paste the query into IPython Notebook, which fulfills the function of documenting the process of exploring data, but queries must all be edited, in parallel to the notebook, as separate files. My objective is to make it easy for beginning students to explore RDF data using SPARQL and record their explorations in the notebook. There must be a better way!
UPDATE:
@Joshua Taylor, @AndyS point out that the commands accept multiline queries as arguments. This works fine at the bash prompt but unfortunately not in IPython Notebook, which throws a SyntaxError:
In [5]: !arq --data dcam.ttl ' PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX dcam: <http://purl.org/dc/dcam/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?s ?p ?o WHERE { ?s ?p ?o . }' Out [5]: File "<ipython-input-5-c9328c1c0c64>", line 2 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ^ SyntaxError: invalid syntax
If I escape the end of line in line 1, as in
In [5]: !arq --data dcam.ttl '\ ... Out [5]: File "<ipython-input-18-313c556abc1d>", line 2 PREFIX dcam: <http://purl.org/dc/dcam/> ^ SyntaxError: invalid syntax
However, I cannot get the entire command to execute by escaping all of the ends of line.
So perhaps the problem lies not with how arq and roqet handle queries in-line but with how those arq and roqet command lines get passed to IPython Notebook?
原文:
最满意答案
您可以添加另一个同步文件夹,以便添加Vagrantfile
config.vm.synced_folder "D:\\VAGRANT\\domains\\domain.com\\web", "<path where you deploy files in the vm>"
注意:我没有使用windows,所以不确定如何在文件中设置路径尝试使用
\\
或正斜杠D:/VAGRANT/....
You can add another sync folder so you can add in your Vagrantfile
config.vm.synced_folder "D:\\VAGRANT\\domains\\domain.com\\web", "<path where you deploy files in the vm>"
note : I am not using windows so not exactly sure how the path should be set in the file try with
\\
or forward slashD:/VAGRANT/....
相关问答
更多-
cmd命令显示文件夹所有文件是什么?[2023-09-24]
dir -
您可以使用dirname(__FILE__)代替__DIR__ you can use dirname(__FILE__) instead of __DIR__
-
Python os.path.dir(Python os.path.dir)[2021-11-27]
为了清楚起见,os.path.join适用于文本字符串。 它实际上并没有以任何复杂的方式将它们与底层操作系统进行比较。 这对于效率和当您想要创建与当前正在执行的计算机无关的路径时非常有用。 正如Burhan Khalid所说,glob是实现你真正想要的最佳方式。 如果你想出于某种原因想要使用os库,那么你可以做一些事情,比如使用os.walk获取所有可能性然后过滤掉那些。 Just to be clear, os.path.join works on text strings. It does not ac ... -
此链接http://msdn.microsoft.com/en-us/library/a2zdt10t(v=vs.90).aspx可能会对您感兴趣 我不喜欢改变我的整个系统配置,只是为了建立一个项目。 最有趣的部分是最后一个评论: 此页面无法提及如何进入此对话框: 从物业经理,双击属性页。 点击树控件中“常用属性”下的“用户宏”。 This link http://msdn.microsoft.com/en-us/library/a2zdt10t(v=vs.90).aspx could interest y ...
-
PHP basename(__DIR__)在某些服务器上返回_DIR_(PHP basename( __DIR__ ) is returning _DIR_ on some servers)[2022-11-26]
__DIR__是在PHP 5.3中引入的。 仔细检查你的PHP版本。 参考: http : //php.net/manual/en/language.constants.predefined.php __DIR__ is introduced in PHP 5.3 . Double check your PHP version . Reference: http://php.net/manual/en/language.constants.predefined.php -
搜索: 定义( “PHP_DIR”,REAL_PUBLIC_PATH “/../ ClixScript_PHP_DOCS /”); 用。。。来代替: 定义( “PHP_DIR”,REAL_PUBLIC_PATH “/../../ ClixScript_PHP_DOCS /”); Search: define("PHP_DIR",REAL_PUBLIC_PATH."/../ClixScript_PHP_DOCS/"); Replace with: define("PHP_DIR",REAL_PUBLIC_PA ...
-
经过大量研究后,我发现.htaccess文件中的这两行(必要的重定向)导致页面不再加载: RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 为什么? 因为子域www.relocation.dev不在我的hosts文件中。 主机文件行应如下所示: 192.168.56.102 relocation.dev www.relocation.dev 现在一切正常。 谢谢你@ v2p的时 ...
-
当你使用define('Head', __DIR__ .'/views/head.php'); 你已经在定义中硬编码了斜杠。 但是默认情况下,Windows使用\作为默认目录分隔符,因此__DIR__将在Windows中的路径中使用\ (可以使用/作为替代,但它应该不是问题)。 如果希望它们保持一致,可以执行以下操作: define('Head', __DIR__ .DIRECTORY_SEPARATOR ."views".DIRECTORY_SEPARATOR ."head.php"); When yo ...
-
您可以在.htaccess使用auto_prepend_file PS: $_SERVER['DOCUMENT_ROOT']应该可以使用(在共享主机上) You can use auto_prepend_file in your .htaccess PS: $_SERVER['DOCUMENT_ROOT'] should work (on shared hostings too)
-
您可以添加另一个同步文件夹,以便添加Vagrantfile config.vm.synced_folder "D:\\VAGRANT\\domains\\domain.com\\web", "
" 注意:我没有使用windows,所以不确定如何在文件中设置路径尝试使用\\或正斜杠D:/VAGRANT/.... You can add another sync folder so you can add in your Vagran ...