获取共享特殊值的最常见的配对。

huangapple go评论37阅读模式
英文:

Get the most frequent pairs that share a special value

问题

这是我第一次使用SPARQL,我正在为电影和演员创建一个本体。

  • 我有MovieActor类,并且每个电影实例都有演员。

  • Actor类有一个对象属性hasGender,用于指示演员是男性还是女性。

我需要一个查询来返回最频繁一起工作的男演员和女演员的配对。

我编写了一个查询来返回最频繁的男演员:

PREFIX ontology: <https://....../>
SELECT ?MaleActor ?FemaleActor (count(?Movie) as ?MoviesCount)
WHERE {
    ?Movie ontology:hasActor ?MaleActor.
    ?Movie ontology:hasActor ?FemaleActor.
    ?MaleActor ontology:hasGender "Male".
    ?FemaleActor ontology:hasGender "Female".
    FILTER (?MaleActor != ?FemaleActor)
}
GROUP BY ?MaleActor ?FemaleActor
ORDER BY DESC(?MoviesCount)
LIMIT 1

这个查询将返回三个值:

  1. 男演员
  2. 女演员
  3. 他们一起工作的电影数量
英文:

This is my first time with SPARQL and I’m working on an ontology for movies and actors.

  • I have Movie and Actor classes, and each instance of movie has actors.

  • The Actor class has the object property hasGender which tells if an actor is Male or Female.

I need a query to return the pairs of male and female actors who worked together most frequently.

I wrote query return the most frequent Male Actor:

PREFIX ontology: &lt;https://....../&gt;
SELECT ?Actor (count (?Movie) as ?MoviesCount)
WHERE { 
  	?Movie ontology:hasActor ?Actor.
  	?Actor ontology:hasGender ?Gender
  filter regex (str(?Gender), &quot;Male&quot;)
 }
Group by ?Actor
Order by desc (?MoviesCount)

I wrote query return the most frequent Female Actor:

PREFIX ontology: &lt;https://....../&gt;
SELECT ?Actor (count (?Movie) as ?MoviesCount)
WHERE { 
  	?Movie ontology:hasActor ?Actor.
  	?Actor ontology:hasGender ?Gender
  filter regex (str(?Gender), &quot;Female&quot;)
 }
Group by ?Actor
Order by desc (?MoviesCount)

But that is not what I need.

I need only one query which should return 3 values:

  1. The Actor
  2. The Actress
  3. The Movies count which the pair worked on together

答案1

得分: 1

你可以通过将查询变量?Actor设定为特定性别(例如,?actor?actress)来合并两个查询。

SELECT ?actress ?actor (COUNT(?movie) as ?movie_count)

WHERE { 

  ?movie 
    ontology:hasActor ?actress ;
    ontology:hasActor ?actor .

  ?actress ontology:hasGender ?female .
  ?actor ontology:hasGender ?male .

  FILTER regex(str(?female), "Female") .
  FILTER regex(str(?male), "Male") .
  
}
GROUP BY ?actress ?actor
ORDER BY DESC (?movie_count)

而不是使用FILTER,你可以直接指定性别值:

# 如果它是一个对象:
?actress ontology:hasGender <你的“Female”概念的URI> .

# 如果它是一个字符串:
?actress ontology:hasGender "Female" .

# 如果它是一个带有语言标签的字符串:
?actress ontology:hasGender "Female"@en .
英文:

You can combine both of your queries by making the query variable ?Actor gender-specific (e.g., ?actor and ?actress).

SELECT ?actress ?actor (COUNT(?movie) as ?movie_count)

WHERE { 

  ?movie 
    ontology:hasActor ?actress ;
    ontology:hasActor ?actor .

  ?actress ontology:hasGender ?female .
  ?actor ontology:hasGender ?male .

  FILTER regex(str(?female), &quot;Female&quot;) .
  FILTER regex(str(?male), &quot;Male&quot;) .
  
}
GROUP BY ?actress ?actor
ORDER BY DESC (?movie_count)

Instead of using a FILTER, you can directly specify the gender value:

# if it’s an object:
?actress ontology:hasGender &lt;the URI of your &quot;Female&quot; concept&gt; .

# if it’s a string:
?actress ontology:hasGender &quot;Female&quot; .

# if it’s a language-tagged string:
?actress ontology:hasGender &quot;Female&quot;@en .

huangapple
  • 本文由 发表于 2023年4月17日 00:07:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定