首页 \ 问答 \ 使用IPython Notebook(使用rdflib?)来记录SPARQL查询和结果(Using IPython Notebook (with rdflib?) to log SPARQL queries and results)

使用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?


原文:
更新时间:2022-04-11 12:04

最满意答案

您可以添加另一个同步文件夹,以便添加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 slash D:/VAGRANT/....

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)