获得保护基质

时间:2013-07-29 13:04:01

标签: sparql ontology owl protege

我的工作是关于推荐系统的图书馆书籍。作为输入我需要书籍分类本体。在我的本体论中对图书馆书籍进行了分类。这个分类有14个类别,除了兄弟类作者,书,伊斯本。书籍类中的个人是书的主题(约600个主题),作者类中的个人是名字的作者,也是isbn类。我用protege 4.1设计了这个本体。

我也收集了并且手动将部分归属于类别的书。对象属性是名称“hasSubject”与个别书类相关的类别。示例书“A”具有主题类别“S”和“F”以及...结果我想获得属于图书类别的矩阵。如果这本书属于一个类别,那么得到1,否则取值为0.就像这样:

     cat1   cat2   cat3   
book1   1      0      0   
book2   1      0      1   
book3   1      1      0  

在此示例中表示book1属于类别1且不属于类别2和3.如何使用sparql进行保护?

1 个答案:

答案 0 :(得分:5)

处理固定数量的类别

给出类似

的数据
@prefix : <http://example.org/books/> .

:book1 a :Book, :Cat1 .
:book2 a :Book, :Cat1, :Cat3 .
:book3 a :Book, :Cat1, :Cat2 .

您可以使用

之类的查询
prefix : <http://example.org/books/>

select ?individual
       (if(bound(?cat1),1,0) as ?Cat1)
       (if(bound(?cat2),1,0) as ?Cat2)
       (if(bound(?cat3),1,0) as ?Cat3)
where {
  ?individual a :Book .
  OPTIONAL { ?individual a :Cat1 . bind( ?individual as ?cat1 ) } 
  OPTIONAL { ?individual a :Cat2 . bind( ?individual as ?cat2 ) }
  OPTIONAL { ?individual a :Cat3 . bind( ?individual as ?cat3 ) }
}
order by ?book

其中绑定了某些变量(它们所绑定的特定值并不重要),具体取决于是否存在某些三元组以获得如下结果:

$ arq --data data.n3 --query matrix.sparql
-----------------------------------
| individual | Cat1 | Cat2 | Cat3 |
===================================
| :book1     | 1    | 0    | 0    |
| :book2     | 1    | 0    | 1    |
| :book3     | 1    | 1    | 0    |
-----------------------------------

处理任意数量的类别

这里的解决方案似乎在耶拿有效,但我不确定具体的结果是否得到保证。 (更新>基于此answers.semanticweb.com question and answer,SPARQL规范似乎确保此行为。)如果我们有更多的数据,例如,关于哪些事物是类别,哪些是书籍,例如

@prefix : <http://example.org/books/> .

:book1 a :Book, :Cat1 .
:book2 a :Book, :Cat1, :Cat3 .
:book3 a :Book, :Cat1, :Cat2 .

:Cat1 a :Category .
:Cat2 a :Category .
:Cat3 a :Category .

然后我们可以运行一个子查询,按顺序选择所有类别,然后为每本书计算一个字符串,表明该书是否在每个类别中。

prefix : <http://example.org/books/>

select ?book (group_concat(?isCat) as ?matrix) where { 
  { 
    select ?category where { 
      ?category a :Category 
    }
    order by ?category 
  }
  ?book a :Book .
  OPTIONAL { bind( 1 as ?isCat )              ?book a ?category . }
  OPTIONAL { bind( 0 as ?isCat ) NOT EXISTS { ?book a ?category } }
}
group by ?book
order by ?book

这有输出:

$ arq --data data.n3 --query matrix2.query
--------------------
| book   | matrix  |
====================
| :book1 | "1 0 0" |
| :book2 | "1 0 1" |
| :book3 | "1 1 0" |
--------------------

更接近问题中的输出,并处理任意数字类别。但是,它取决于每个?category以相同顺序处理的?book的值,而且我不确定这是否有保证。

我们甚至可以使用这种方法为表生成标题行。同样,这取决于每个?category以相同顺序处理的?book值,这可能无法保证,但似乎在Jena中有效。要获取类别标题,我们需要做的就是创建一个?book未绑定的行,?isCat的值表示特定类别:

prefix : <http://example.org/books/>

select ?book (group_concat(?isCat) as ?matrix) where { 
  { 
    select ?category where { 
      ?category a :Category 
    }
    order by ?category 
  }

  # This generates the header row where ?isCat is just
  # the category, so the group_concat gives headers.
  { 
    bind(?category as ?isCat) 
  }
  UNION 
  # This is the table as before
  {
    ?book a :Book .
    OPTIONAL { bind( 1 as ?isCat )              ?book a ?category . }
    OPTIONAL { bind( 0 as ?isCat ) NOT EXISTS { ?book a ?category } }
  }
}
group by ?book
order by ?book

我们得到了这个输出:

--------------------------------------------------------------------------------------------------------
| book   | matrix                                                                                      |
========================================================================================================
|        | "http://example.org/books/Cat1 http://example.org/books/Cat2 http://example.org/books/Cat3" |
| :book1 | "1 0 0"                                                                                     |
| :book2 | "1 0 1"                                                                                     |
| :book3 | "1 1 0"                                                                                     |
--------------------------------------------------------------------------------------------------------

使用某些字符串操作,您可以缩短用于类别的URI,或者扩展数组条目以获得正确的对齐。一种可能性是:

prefix : <http://example.org/books/>

select ?book (group_concat(?isCat) as ?categories) where { 
  { 
    select ?category
           (strafter(str(?category),"http://example.org/books/") as ?name)
     where { 
      ?category a :Category 
    }
    order by ?category 
  }

  { 
    bind(?name as ?isCat)
  }
  UNION 
  {
    ?book a :Book .
    # The string manipulation here takes the name of the category (which should
    # be at least two character), trims off the first character (string indexing
    # in XPath functions starts at 1), and replaces the rest with " ". The resulting
    # spaces are concatenated with "1" or "0" depending on whether the book is a
    # member of the category.  The resulting string has the same width as the
    #  category name, and makes for a nice table.
    OPTIONAL { bind( concat(replace(substr(?name,2),"."," "),"1") as ?isCat )              ?book a ?category . }
    OPTIONAL { bind( concat(replace(substr(?name,2),"."," "),"0") as ?isCat ) NOT EXISTS { ?book a ?category } }
  }
}
group by ?book
order by ?book

产生这个输出:

$ arq --data data.n3 --query matrix3.query
-----------------------------
| book   | categories       |
=============================
|        | "Cat1 Cat2 Cat3" |
| :book1 | "   1    0    0" |
| :book2 | "   1    0    1" |
| :book3 | "   1    1    0" |
-----------------------------

这几乎就是你在问题中所拥有的。