英文:
Get the most frequent pairs that share a special value
问题
这是我第一次使用SPARQL,我正在为电影和演员创建一个本体。
-
我有
Movie
和Actor
类,并且每个电影实例都有演员。 -
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
这个查询将返回三个值:
- 男演员
- 女演员
- 他们一起工作的电影数量
英文:
This is my first time with SPARQL and I’m working on an ontology for movies and actors.
-
I have
Movie
andActor
classes, and each instance of movie has actors. -
The
Actor
class has the object propertyhasGender
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: <https://....../>
SELECT ?Actor (count (?Movie) as ?MoviesCount)
WHERE {
?Movie ontology:hasActor ?Actor.
?Actor ontology:hasGender ?Gender
filter regex (str(?Gender), "Male")
}
Group by ?Actor
Order by desc (?MoviesCount)
I wrote query return the most frequent Female Actor:
PREFIX ontology: <https://....../>
SELECT ?Actor (count (?Movie) as ?MoviesCount)
WHERE {
?Movie ontology:hasActor ?Actor.
?Actor ontology:hasGender ?Gender
filter regex (str(?Gender), "Female")
}
Group by ?Actor
Order by desc (?MoviesCount)
But that is not what I need.
I need only one query which should return 3 values:
- The Actor
- The Actress
- 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), "Female") .
FILTER regex(str(?male), "Male") .
}
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 <the URI of your "Female" concept> .
# if it’s a string:
?actress ontology:hasGender "Female" .
# if it’s a language-tagged string:
?actress ontology:hasGender "Female"@en .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论