Smurf
文章50
标签0
分类6
题目

题目

题目

1. XML Rdf

Authoring guidelines:

  1. All elements must have an end tag. 标签有头有尾
  2. All elements must be cleanly nested (overlapping elements are not allowed). 所有元素必须不能重复
  3. All attribute values must be enclosed in quotation marks.
  4. Each document must have a unique first element, the root node.
  5. 大小写敏感
1
2
3
4
5
6
7
8
9
10
11
12
13
<book>
<title>Knowledge Graph</Title> <!--尾标签有误-->
<author>
<firstName>Guilin</firstName>
<lastName>Qi</lastName>
<email>gqi@seu.edu.cn</email>
This is some text inside an XML element.
</author>
<author>
<firstName>Tianxing<lastName>
</firstName>Wu</lastName> <!--嵌套出错-->
<email>tianxingwu@seu.edu.cn</email>
</author><!--缺少</book>-->
1
2
3
4
5
6
7
8
9
10
<!--Typed Literals:-->
“Beantown”^^xsd:string
“The Bay State” ^^xsd:string
<!--Plain literal and literals with language tags:-->
“France” “France”@en “France”@fr
“法国”@zh “Frankreich”@de
<!--Equalities for Literals:-->
“001”^^xsd:integer = “1”^^xsd:integer
“123.0”^^xsd:decimal = “00123”^^xsd:integer (based on datatype hierarchy)
<!--上面这两种形式等价,因为integer是decimal的父节点-->
  • Naming conflicts exist in different XML documents
1
2
3
4
5
6
XML document_1
<table>
<tr>
<td>Apples</td>
<td>Bananas</td></ tr>
</table>
  • Solution
1
2
3
4
5
6
7
XML document_1
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
  • 2.Naming conflicts exist in different XML documents
1
2
3
4
5
6
XML document_2
<table>
<name>African Coffee Table</name>
<width>8G< /width>
<length>120</length>
< /table>
  • answer
1
2
3
4
5
6
XML document_2
< f:table>
<f:name >African Coffee lable</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

image-20211103095753577

  • Does the datatype “德国” equals to “德国” @ zh ?
    • Answer:不相同,因为他们位于的层次结构不同

image-20211103095926352

  • URI 英文字符 IRI任何编码

Equalities for Literals:
“0013^^xsd:integer =“13”^^xsd:integer
“123.0^^xsd:decimal= “00123”1^^xsd:integer (based on data type hierarchy)

Exercise

Does the datatype“德国”equals to“德国”@ zh ?

  • 不一样

RDF表示N元关系

  • 用一个节点中介

  • 利用一个空节点

1
2
3
4
@prefix sw: <http://www.semanticweb.org/ontology-9/>
sw:John sw:is_a sw:professors;
sw:has_id sw:987654321;
sw:has_name sw:John Doe.

  • Represent the following sentence graphically by means of the blank node:
    Wikipedia said that Tolkien wrote Lord of the Rings.

  1. Please represent the following sentences graphically by means of blank nodes.
    Jonathon says cherries taste sweet.
    1月14日,国家药监局宣布酚酞片停止生产

image-20211103114451839

  • Given:
1
2
3
4
ex:happilyMarriedWith rdfs:subPropertyOf ex:isMarriedTo . 
ex:isMarriedTo rdfs:domain ex:Person .
ex:isMarriedTo rdfs:range ex:Person .
ex:pascal ex:happilyMarriedWith ex:lisa .
  • 可推出:
1
2
3
ex:pascal ex:isMarriedTo ex:lisa . 
ex:pascal rdf:type ex:Person .
ex:lisa rdf:type ex:Person .
  • What can be inferred from the following triples using RDFS semantics?
1
2
3
4
5
ex:Postgraduate_student rdfs:subClassOf ex:Student 
ex:Professor rdfs:subClassOf ex:Academic_staff
ex:Supervise rdfs:domain ex:Professor
ex:Supervise rdfs:range ex:Postgraduate_student
ex:John ex:Supervise ex:Mary
1
2
3
4
5
ex:John rdf:type ex:Professor 
ex:Mary rdf:type ex:Postgraduate_student

ex:John rdf:type ex:Academic_staff
ex:Mary rdf:type ex:Studentt
  • What can be inferred from the following triples using RDFS semantics?
1
2
3
4
5
6
7
8
9
ex:Undergraduate_student rdfs:subClassOf ex:Student .
ex:Postgraduate_student rdfs:subClassOf ex:Student .
ex:Professor rdfs:subClassOf ex: Academic_staff .
ex:Academic_staff rdfs:subClassOf ex:University_staff .
ex:Teach rdfs:domain ex:Academic_staff .
ex:Teach rdfs:range ex:Student .
ex:Supervise rdfs:domain ex:Professor .
ex:Supervise rdfs:range ex:Postgraduate_student .
ex:John ex:Supervise ex:Mary .
1
2
3
4
5
6
ex:Professor rdfs:subClassOf ex: University_staff .
ex:John rdf:type ex:Professor .
ex:Mary rdf:type ex:Postgraudate_student .
ex:John rdf:type ex:Academic_staff .
ex:John rdf:type ex:University_staff .
ex:Mary rdf:type ex:Student .

1
2
3
sw:John sw:is_a sw:professors . 
sw:John sw:has_name “John Doe” .
sw:John sw:has_id “987654321” .
1
2
3
sw:John sw:is_a sw:professors ; 
sw:has_name “John Doe” ;
sw:has_id “987654321” .
  • Use a RDF graph to represent the sentence “John is working in a company located in Sydney” (bland node can be considered).

  • Does the datatype “3.14”^^xsd:string equal to “+03.14”^^xsd:string? If not, why?

  • Answer:不是;这是字符串形式,字符串要考虑每个character,明显两个字符串不一样。

2. OWL

Equivalence between classes, individuals, and properties: 一些等价关系

1
2
3
exp:Athlete owl:equivalentClass exp:SportsPlayer . 
exp:obtain owl:equivalentProperty exp:acquire .
exp:SportsPlayerA owl:sameAs exp:Thomas .

Disjointness between classes: 类别不相交关系

1
exp:Man owl:disjointWith exp:Woman .

Intersection of classes: 交

1
2
exp:Mother rdf:type owl:class ; 
owl:intersectionOf (exp:Woman exp:Parent) .
  • 这里括号里面可以有更多类别

Negation of a class: 类别取反

1
2
exp:ABC rdf:type owl:class ; 
owl:complementOf exp:Meat .

Property definitions: object property and data property.对属性的定义(谓词)

1
2
ex:friendOf rdf:type owl:ObjectProperty . 
ex:age rdf:type owl:DataProperty .

Transitive property: 传递性

1
exp:ancestor rdf:type owl:TransitiveProperty .
  • Given triples
1
2
exp:Thmoas exp:ancestor exp:Jimmy . 
exp:Jimmy exp:ancestor exp:Michael .
  • Then what can we infer from them?
1
exp:Thmoas exp:ancestor exp:Michael .

Functional property:

1
exp:hasMother rdf:type owl:FunctionalProperty .
  • Please explain using natural language:
    Everyone has only one mother.

  • 谓词的函数特性表示他的range取值只能有一个

inverse functional property 具有反函数特性

1
exp:postgraduateSupervisor rdf:type owl:InverseFunctionalProperty .
  • Please explain using natural language:

Each post-graduate student has only one supervisor.

  • 这表示谓语的定义域只能有一个

Symmetric property

1
exp:friendOf rdf:type owl:SymmetricProperty .
  • Given a triple
1
exp:Thmoas exp:friendOf exp:Lisa .
  • Then what can we infer from it?
1
exp:Lisa exp:friendOf exp:Thmoas .

Inverse relations between properties

1
exp:ancestor owl:inverseOf exp:descendant .
  • Given a triple
1
exp:Thmoas exp:ancestor exp:Jimmy .
  • Then what can we infer from it?
1
exp:Jimmy exp:descendant exp:Thmoas .

Constraints on properties: universal quantifier ∀任意(谓词限定词)

1
2
3
exp:Person rdf:type owl:Restriction ; 
owl:onProperty exp:hasMother ; #表示Person可以充当hasMother的主语
owl:allValuesFrom exp:Women . #表示前一句谓语的range的取值范围
  • What do we know from the above triples?
  • If the subject of exp:hasMother comes from the class exp:Person, then the object can be only from the class exp:Women

Constraints on properties: existential quantifier ∃ 存在

1
2
3
exp:SemanticWebPapers rdf:type owl:Restriction ; 
owl:onProperty exp:publishedIn ;
owl:someValuesFrom exp:AAAIPapers . #存在一部分的发表在AAAIPapers
  • What do we know from the above triples?
  • If the subject of exp:publishedIn comes from the class exp:SemanticWebPapers, then the object may be (at least one) from the class exp:AAAIPapers.

  • A part of the Semantic Web papers were published in AAAI.

Constraints on properties: cardinalities

1
2
3
exp:Person rdf:type owl:Restriction ; 
owl:onProperty exp:hasParent ;
owl:cardinality “2”^^xsd:integer . #限定hasParent的range只能有两个
  • Please explain using natural language:
  • Each person should have two parents.

  • cardinality也可以换成owl:maxCardinality/ owl:minCardinality,表示最多或者最少

  • Giraffe is a kind of animal which only eats leaves.

1
2
3
4
5
6
7
8
9
10
@prefix sw: <http://semanticweb.org/> 
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .

sw:Giraffe rdfs:subClassOf _:x .
_:x rdfs:subClassOf sw:Animal ;
rdf:type owl:Restriction ;
owl:onProperty sw:eat ;
owl:allValuesFrom sw:Leaf .
  • Please write the corresponding RDF triples (in Turtle) of the following sentence: “Childless Persons are the persons who are not parents”. Note that classes “ChildlessPerson”, “Person”, “Parent” are defined in the namespace http://semanticweb.org/ with the prefix “SW”.
1
2
3
4
5
_:x0 rdf:type owl:class;
owl:complement owl:Parent.
_:x1 rdf:type owl:class;
owl:intersectionOf (sw:Person _:x0);
owl:equivalentClass sw:ChildlessPerson.
  • Exercise 1:The father of each person should be a man
1
2
3
exp:Person rdf:type owl:Restriction ; 
owl:onProperty exp:hasFather ;
owl:allValuesFrom exp:Men .
  • Please useRDF triples (in Turtle) to represent the following sentence: “Each human has only one biological mother who should be a woman” with the classes: “Human”and “Women”, and the property“biologicalMotherOf ”. Each class or property can be defined in the namespace http://semanticweb.org/ with the prefix “SW”.
1
2
3
4
5
6
7
@prefix sw: <http://semanticweb.org/> 
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
sw:biologicalMotherOf rdfs:domain sw:Women ;
rdfs:range sw:Human ;
rdf:type owl:InverscFunctionalProperty .

5) Please write the corresponding RDF triples (in Turtle) of the following sentence: “Grandpas are the men who are grandparents”. Specific classes are defined in the namespace
http://semanticweb.org/. Prefixes are given as follows:
@prefix sw: http://semanticweb.org/ .
@prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns# .
@prefix owl: http://www.w3.org/2002/07/owl# .

1
2
3
_:x  rdf:type  owl:class  ;
owl:intersectionOf (sw:Man sw:Grandparent) ;
owl:equivalentClass sw:Grandpa .

3. Propositional Logic

  • Example: Natural Language: Nigiri is a type of Sushi which has ingredient Rice and Fish
  • FOL:
    ∀x.[Nigiri(x) → Sushi(x) ∧ ∃y.[hasIngredient(x,y) ∧ Rice(y)] ∧
    ∃z.[hasIngredient(x,z) ∧ Fish(z)]]
  • Are the following valid OWL axioms? If not, why? (class: A,B,C,D, role: R)
  • A⨅(B⨆ ∃R)
  • (∀R.A⨆B)⨅A
  • (A⨅B)¬C⨆D

image-20211103120142002

  • Everybody knowing some actor has only envious friends

例1:$\exists$knows.Actor $\subseteq$​ $\forall$hasfriend.Envious

用一阶逻辑来改写:

image-20211013230758625

All students that attend only master courses.

例2:Student ⊓attendsCourse.MasterCourse

用一阶逻辑改写:
∀x (Student(x) ∧ ∀y(attendsCourse(x,y) ∧ MasterCourse(y)))

例3:自然语言解释:
All students that attend at least one lecture.

Student ⊑Attend.Lecture
用一阶逻辑改写:
∀x (Student(x) → ∃y(Attend(x,y) ∧ Lecture(y)))

Example

(1) Any student must attend at least one lecture.
Student ⊑ ∃Attend.Lecture
(2) John is a brother of Mary.
Brother(John,Mary)
(3) Parents are exactly those who are mothers or fathers.
*Parent ≡ Mother ⊔ Father

  • Exercise
  1. A Phd Student is a student who has a supervisor.

2.Animals are not vegetables.

  • Express the following sentences in Description Logics.

Li is a student of SEU. Any person who is enrolled in a university is a college student.

image-20211103115803638

  • Example: ¬Feamale ⊓ ∃haschild.Human ⊑ Human

image-20211013232918934

  • Modify:

image-20211013234500299

image-20211013234510570

  • Exercise:

  • Give a model for the ontology:

    • $\text{PhD}_\text{student} \sqsubseteq \text{Student}$​
    • $\text{Student} \sqsubseteq \neg\text{Employee}$​​, $\text{PhD}_\text{student}(\text{John})$
    • $\text{Emploee}(\text{Lisa}),\text{Marriedto}(\text{John},\text{Lisa})$​​​
  • Answer:

    • $\Delta=\{jo,l\}$
    • $I_I(\text{John})=jo$​​
    • $I_I(\text{Lisa})=l$​​
    • $I_C(\text{Emploee})={l}$​
    • $I_C(\text{PhD}_\text{student})={jo}$​​
    • $I_C(\text{Student})={jo}$​​
    • $I_R(\text{Marriedto})={}$
  • Inconsistency is often caused by modeling errors. 如果不一致,常常都无法找到一个model

    • Unicorn(beauty)
    • Unicorn ⊑ Fictitious
    • Unicorn ⊑ Animal
    • Animal ⊑¬Fictitious
  • A knowledge base is incoherent if a named class is equivalent to ⊥.

  • It usually also points to a modeling error. 即通过构造空集可以证明model的错误

    • Unicorn ⊑ Fictitious
    • Unicorn ⊑ Animal
    • Animal ⊓ Fictitious ⊑ ⊥
  • Express the following sentences in Description Logic.

  1. Every teacher must teach someone
    Teacher $\subseteq \exists$ Teach. Human
  2. Every finger is a bodypart and is a part-of hand.
    Finger $\subseteq$ BodyPart $\cap \exists$ PartOf. Hand
  3. Zhang is a teacher of SEU
    Teacher (Zhang), TeachIn(Zhang,SEU)
  • Give a model of the following ontology:
    PhD $_{\text {student }}$ U Undergraduatge $_{\text {student }} \sqsubseteq$ Student,
    PhD $_{\text {student }}($ John $)$,
    Undergraduatge $_{\text {student }}($ Jack $)$,
    Sister $($ Lisa,Jack $)$,
    Employee $($ Lisa $)$

    $I_{C}\left(\right.$ Undergraduatge $\left._{\text {student }}\right)=\{j a\}$
    $I_{C}\left(\right.$ Phd $\left._{\text {student }}\right)=\{j o\}$
    $I_{C}($ Student $)=\{j o, j a\}$
    $I_{R}($ Sister $)=\{(l, j a)\}$

  • Give a model of the following ontology

  • Professor⊑FacultyMember,

  • Professor(Guilin Qi),

  • hasAffiliation(Guilin Qi, SEU),

  • StudentOf(Zhang, Guilin Qi)

A model:

  • ∆={a,b,c}
  • I(Guilin Qi)=a, I(SEU)=b, I(Zhang)=c
  • I( Professor)={a}, I(FacultyMember)={a},
  • I(hasAffiliation)={(a,b)},
  • I(StudentOf)={(c,a)},

  • Give a model of the following ontology


  • Write the inferred axioms using description logic after conducting classification in forward reasoning on the following axioms.

    Endocarditis $\sqsubseteq$ Heart Disease
    Miocardial Infarction $\sqsubseteq$ Heart Disease
    Heart Disease $\sqsubseteq$ Disease
    Enterococcal Endocarditis $\sqsubseteq$ Endocarditis

  • Answer

    Miocardial_Infarction $\sqsubseteq$​​​​ Disease
    Endocarditis $\sqsubseteq$​​​​ Disease
    Enterococcal_Endocarditis $\sqsubseteq$​​​​ Heart_Disease
    Enterococcal_Endocarditis $\sqsubseteq$​​​​ Disease

image-20220104221111796

4. Knowledge Graph Reasoning

Example1:

image-20211014164036416

Example2:

image-20211014164229482

  • So what type of Endocarditis is?

image-20211014164651146

Example1:

image-20211014164734902

  • Classification is to compute all implicit subclass relations by applying rules on a TBox.

    利用TBox的规则计算所有子类的依赖关系

Example2:

  • Highvalue_Company ⊑ ∃beInvestedBy. Investment_Company
  • 高价值公司由投资公司投资。
  • Investment_Company ⊑ Financial_Institute
  • 投资公司属于金融机构
  • ∃beInvestedBy. Financial_Institute ⊑ Solvent_Company
  • 借助金融机构投资的公司都是具备偿还能力的 企业。

Answer:

  • Highvalue_Company ⊑ ∃beInvestedBy. Financial_Institute
  • Highvalue_Company ⊑ Solvent_Company

Example1:

  • Rule: If you are human, then you are mortal.
  • Human(x)→Mortal(x)
  • Question: Is Socrates a mortal?
  • Solution: Check whether Mortal(Scorates) or Human(Scorates) is true.

Example2:

image-20211020101305861

  • Query: Find all patients with heart diseases, i.e., Heart_Disease(x)
  • Rewriting: Endocarditis(x) ⋁ Miocardial_Infarction(x) ⋁ Coronary_disease(x)
  • 可以理解成重写问题

  • Given the following KG, use query rewriting on the query Person(x) (this query is used for checking whether x is a person) .

image-20211103152402394

Exercize:

Find all sports players with the following KG, i.e., rewrite the query Sports_Player(x).

image-20211020101450138

  • Query: Find all sports players
  • Rewriting: SportsPlayer(x) ⋁ BasketballPlayer(x) ⋁ FootballPlayer(x) ⋁ TennisPlayer(x)

Example:

  • PhDStudent ⊑ Student,

  • PhDStudent ⊑ Employee,

  • Student ⊑ ¬ Employee

    • 以上最终可以退出Student和Employee都是空集,所以找不到一个model
    • Inconsistent ontology: ontology without a model.

Example:

  • PhDStudent ⊑ Student,

  • PhDStudent ⊑ Employee,

  • Student ⊑ ¬ Employee,

  • PhDStudent(John)

    • 这里则产生了矛盾

Example: DICE ontology:

  • Brain $\sqsubseteq$ CentralNervousSystem $\sqcap$ $\exists$systempart.NervousSystem $\sqcap$ BodyPart $\sqcap$ $\exists$ region.HeadAndNeck $\sqcap$ $\forall$ region.HeadAndNeck
  • CentralNervousSystem $\sqsubseteq$​​​ NervousSystem
  • BodyPart $\sqsubseteq \neg$​​ NervousSystem or DisjointWith(BodyPart,NervousSystem)
    • Reasoning:
      • Brain $\sqsubseteq$​​​ Bodypart $\sqsubseteq$​​​ $\neg$​​ NervousSystem
      • Brain $\sqsubseteq$​​ CentralNervousSystem $\sqsubseteq$​​ NervousSystem
      • Brain = $\emptyset$
    • Brain是空集

Example from Foaf:

  • Person(timbl)
  • Homepage(timbl, http://w3.org/)
  • Homepage(w3c, http://w3c.org/)
  • Organization(w3c)
  • InverseFunctionalProperty(Homepage) (Homepage的domain只有一个值,所以矛盾)
  • DisjointWith(Organization, Person)

Example from OpenCyc:

  • ArtifactualFeatureType(PopulatedPlace)
  • ExistingStuffType(PopulatedPlace)
  • DisjointWith(ExistingobjectType,ExistingStuffType)
  • ArtifactualFeatureType $\sqsubseteq$​ ExistingObjectType

  • ExistingObjectType与ExistingStuddType不相交,但是却拥有相同的实例,所以矛盾

Exercise:
The following ontology is inconsistent, and remove one axiom or assertion in it to make it consistent.

  • $\text { Student } \sqsubseteq \neg \text { Employee }$ or $\text { Employee(John)}$ or $\text { PhD }_{\text {student }} \text { (John) }$
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
%%Facts
friend(joe,sue) .
friend(ann,sue) .
friend(sue,max) .
friend(max,ann) .

%% Rules
fof(X,Y) :- friend(X,Y)
fof(X,Z) :- friend(X,Y), fof(Y,Z)

%%Quiery 1
query(X) :- fof(X,ann)

%%Answer:
fof(sue,ann) .
fof(max,ann) .
fof(joe,ann) .
fof(ann,ann) .

Exercise

  • Rule:fatherinlawOf(X,Z) :- wifeOf(X,Y), fatherOf(Y,Z)

  • Fact: wifeOf (YaoMing,YeLi)

  • Fact: fatherOf (YeLi,YeFa)

    Who is the father-in-law of Yao Ming?

  • fatherinlawOf(YaoMing, YeFa)

5. Knowledge Graph Construction

image-20211027111243049

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@base <http://foo.example/DB/> . 
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

<People/ID=7> rdf:type <People> . #某条记录的类别
<People/ID=7> <People#ID> "7" . #具体属性
<People/ID=7> <People#fname> "Bob" .
<People/ID=7> <People#addr> "18" .
<People/ID=7> <People#ref-addr> <Addresses/ID=18> . #利用外键关联映射两张表的记录

<People/ID=8> rdf:type <People> .
<People/ID=8> <People#ID> "8" .
<People/ID=8> <People#fname> "Sue" .

<Addresses/ID=18> rdf:type <Addresses> .
<Addresses/ID=18> <Addresses#ID> "18" .
<Addresses/ID=18> <Addresses#city> "Cambridge" .
<Addresses/ID=18> <Addresses#state> "MA" .

image-20211027112228218

1
2
3
4
5
6
7
8
9
10
11
12
13
@base <http://foo.example/DB/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

<Student/ID=001> rdf:type <Student> .
<Student/ID=001> <Student#ID> "001" .
<Student/ID=001> <Student#sname> "Zhang" .
<Student/ID=001> <Student#major> "101" .
<Student/ID=001> <Student#ref-major> <Major/ID=101> .

<Major/ID=101> rdf:type <Major> .
<Major/ID=101> <Major#ID> "101" .
<Major/ID=101> <Major#mname> "CS" .
<Major/ID=101> <Major#address> "CS_Building" .

image-20211027114215875

Example: database view

image-20211027114357042

image-20211027114825313

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Example:
@prefix rr: <http:l//www.w3.org/ns/r2rml#> .
<TriplesMap1>
a rr:TriplesMap; #<TriplesMap1>前没有‘#’时要加这一句

rr:logicalTable [rr:tableName "Person"];

rr:subjectMap[
rr:template "http://www.ex.com/Person/ID={ID}";
#ID={ID} 直接{ID}都是可以的 看你自己怎么定义template
rr:class <http://www.ex.com/Person>;#表示从url拿class
];

rr:predicateObjectMap [
rr:predicate <http:7/www.ex.com/Person#NAME>; #表示从url拿predicate
rr:objectMap [rr:column "NAME"]; #表示从db中取
].

image-20211027115000107

image-20211027155406385

  • Set of RDF triples
1
2
3
4
5
6
7
8
<http://data.example.com/employee/7369> rdf:type ex:Employee.
<http://data.example.com/employee/7369> ex:name "SMITH".
<http://data.example.com/employee/7369> ex:department <http://data.example.com/department/10>.

<http://data.example.com/department/10> rdf:type ex:Department.
<http://data.example.com/department/10> ex:name "APPSERVER".
<http://data.example.com/department/10> ex:location "NEW YORK".
<http://data.example.com/department/10> ex:staff 1.
  • R2RML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@prefix rr: <http://www.w3.org/ns/r2rml#>.
@prefix ex: <http://example.com/ns#>.
<#TriplesMap1>
rr:logicalTable [ rr:tableName "EMP" ];

rr:subjectMap [
rr:template "http://data.example.com/employee/{EMPNO}";
rr:class ex:Employee;
];

rr:predicateObjectMap [
rr:predicate ex:name;
rr:objectMap [ rr:column "ENAME" ];
].

rr:predicateObjectMap [
rr:predicate ex:department; #与rdf对应
rr:objectMap [#定义宾语映射,在第二张表找
rr:parentTriplesMap <#TriplesMap2>; #去map2去找subject来当宾语
rr:joinCondition [#表示这些属性是相同的
rr:child "DEPTNO";#triple2就是child
rr:parent "DEPTNO";#triple1就是parent
];
];
].
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<#TriplesMap2> 
rr:logicalTable <#DeptTableView>;

rr:subjectMap [
rr:template "http://data.example.com/department/{DEPTNO}";
rr:class ex:Department;
];

rr:predicateObjectMap [
rr:predicate ex:name; #对于rdf的Property
rr:objectMap [ rr:column "DNAME" ];
];

rr:predicateObjectMap [
rr:predicate ex:location;
rr:objectMap [ rr:column "LOC" ];
];

rr:predicateObjectMap [
rr:predicate ex:staff;
rr:objectMap [ rr:column "STAFF" ];
].

image-20211027120354890

  • RDF Triples
1
<http://data.example.com/student/001> ex:name "Zhang". <http://data.example.com/student/002> ex:name "Wang".
1
2
3
4
5
6
7
8
9
10
11
12
@prefix rr:<http://www.w3.org/ns/r2rml#> .
@prefix rr:<http://example.com/ns#> .
<#TriplesMap1>
rr:logicalTable [rr:tabelName "RDB"];
rr:subjectMap[
rr:template "http://data.example.com/student/{ID}";
rr:class ex:Student;
];
rr:predicateObjectMap[
rr:predicate ex:name;
rr:objectMap [rr:column "Name"];
].
  • Please use direct mapping to map the following three relational tables to RDF triples,

    Given:

    @base http://foo.example/DB/ .

@prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Answer:

@base <http://foo.example/DB/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

<Student/ID=001> rdf:type <Student>.
<Student/ID=001> <Student#ID> “001”.
<Student/ID=001> <Student#sname> “Wang”.
<Student/ID=001> <Student# major> ”211”.
<Student/ID=001> <Student#ref-major> <Major/ID=211>.

<Course/ID=8> rdf:type <Course>.
<Course/ID=8> <Course#ID> “8”.
<Course/ID=8> <Course#cname> “KG”.
<Course/ID=8> <Course#ref-major> <Major/ID=211>.
<Course/ID=8> <Course#major> “211”.
<Course/ID=8> <Course#ref-major> <Major/ID=211>.

<Course/ID=9> rdf:type <Course>.
<Course/ID=9> <Course#ID> “9”.
<Course/ID=9> <Course#cname> “NLP”.
<Course/ID=9> <Course#major> “211”.
<Course/ID=9> <Course#ref-major> <Major/ID=211>.

<Major/ID=211> rdf:type <Major>.
<Major/ID=211> <Major#ID> “211”.
<Major/ID=211> <Major#mname> “AI”.
<Major/ID=211> <Major#address> “CS_Building”.

\2) Given a relational database and RDF triples, please write corresponding R2RML triples maps which can map the given RDB to RDF.

image-20220104224845301

RDF Triples

1
2
3
4
5
6
7
8
9
10
11
<http://data.example.com/student/001> rdf:type ex:Student ;

ex:id "001" ;
foaf:name "Wang" ;
ex:major "211" ;
ex:gender "Male".
<http://data.example.com/student/002> rdf:type ex:Student ;
ex:id "002" ;
foaf:name "Wu" ;
ex:major "201" ;
ex:gender "Female".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Answer:

@prefix rr: <http:l//www.w3.org/ns/r2rml#> .
@prefix ex: <http://example.com/ns#>.
@prefix foaf: < http://w3.org/ff#>.
<#TriplesMap1>

rr:logicalTable [rr:tableName “Student”]

rr:subjectMap[
rr:template “http://data.example.com/student/{ID}”;
rr:class ex:Student;
];

rr:predicateObjectMap[
rr:predicate ex:id;
rr:objectMap [rr:column “ID”];
];

rr:predicateObjectMap[
rr:predicate foaf:name;
rr:objectMap [rr:column “sname”];
];

rr:predicateObjectMap[
rr:predicate ex:mayjor;
rr:objectMap [rr:column “major”];
];

rr:predicateObjectMap[
rr:predicate ex:gender;
rr:objectMap [rr:column “gender”];
];



  • Given a relational database and RDF triples, please write corresponding R2RML triples maps which can map the given RDB to RDF.

image-20211103152446128

1
2
3
4
5
6
7
8
9
10
<http://data.example.com/student/001> rdf:type ex:Student ;
ex:id "001" ;
ex:name "Wang" ;
ex:major <http://data.example.com/major/211> .
<http://data.example.com/student/002> rdf:type ex:Student ;
ex:id "002" ;
ex:name "Wu" .
<http://data.example.com/major/211> rdf:type ex:Major ;
ex:name "AI" .

6. Triple Extraction from Relational Web Tables

image-20211110154111266

image-20211110154536838

image-20211110154544585

Exercise

  • Compute the Levenshtein distance between “kitten” and “sitting”.
    • 1+1+1=3
  • Compute the Jaccard similarity between “University of California, Davis” and “University of California, Berkeley”in word level.
  • Compute the Jaccard similarity between “English”and “England”based on character-level trigrams.
  • Please compute the Levenshtein distance between two strings “Saturday” and “Sunday”.

Answer:

Saturday→Sturday (delete a)

Sturday→Surday (delete t)

Surday→Sunday (replace r with n)

Thus, the Levenshtein distance is 3.

  • Please compute the Jaccard similarity between “French” and “France” based on character-level bi-grams.

Answer:

Segment “French” and “France” into bi-grams as follows:

French: {Fr, re, en, nc, ch}

France: {Fr, ra, an, nc, ce}

JaccardSimilarity=2/8=0.25

  • Please tell the difference between local disambiguation and global disambiguation in entity linking.

Answer:

Local disambiguation does not consider the effects of other referent entities in the same table/ document. With semantic associations between candidate referent entities, global disambiguation can correct mistakes of local disambiguation.

  • Why do we need candidate generation in entity linking?

Answer:

Candidate generation can significantly reduce the number of comparison between mentions and entities so that we no longer require to iterate through all entities in the knowledge base for each mention. It does improve the efficiency of entity linking.

7. Knowledge Graph Construction from Semi-Structured Data

image-20211117155204555

image-20211117155359253

  • subject: the article entity
    • Qiang Yang
  • predicate: the target relation
    • birthOnDate
  • object: the string captured by the brackets of the regular expression
    • 1964 births
  • 提取完之后需要做property
    • Note: Domain and Range checking is necessary.

image-20211117155639932

  • In the Wikipedia article page of “Mo Yan”, the categories as follows. Please extract facts from them in turtle using YAGO category maps without domain and range checking, and clarify the subsequent validation steps on the extracted fact.
  • YAGO category maps

image-20211117155941473

image-20211117102520453

image-20220105164122027

1
2
3
4
5
6
7
@prefix expr: <http://www.example.org/resource/>
@prefix expp: <http://www.example.org/property/>

expr: MoYan expp: bornOnDate "1955";
expp: hasWonPrice expr: Han Chinese Nobel,
expr: Nobel,
expr: Mao Dun Literature Prize.
  • 需要check property的 range和domain
  • type inference

  • Given the Wikipedia article page of “IntelliJ IDEA”, including the first sentence, categories, and the infobox. Please write correct types from them (directly use plain strings).

image-20211117115757471

  • 第一句话:integrated development environment(IDE)
  • categories:Free integrated development environments,Integrated development environments, Java development tools,Products, software
  • Infobox:Software

image-20211117221901039

Please extract a taxonomy from the following sentence (denote the answer as A is-a B):

  • IBM, AMD, and Intel are High-tech companies using nanotechnology for several years.
  • IBM is-a High-tech company
  • AMD is-a High-tech company
  • Intel is-a High-tech company
  • High-tech company is-a company
  • IBM is-a company
  • AMD is-a company
  • Intel is-a company

8. Unstructured Data

image-20211124104823150

image-20211124104949369

  • Assuming word with higher TF value and higher IDF value is more relevant to domain.
    • TF: Term frequency.
  • IDF: Inverse document frequency
Exercise——TF-IDF
  • Suppose there are 100 words in a document, and the word “cow” appears three times. The word “cow” has appeared in 1,000 documents, and the total number of documents is 10,000,000. What is the TF-IDF score of the word “cow”?
  • 例:假定《亚洲的网络技术》一文长度为1000个词,“亚洲”、“网络”、“技术”各出现20次,则这三个词的“词频”(TF)都为0.02。 然后,搜索Google发现,包含“的”字的网页共有250亿张(假定这就是中文网页总数),包含“亚洲”的网页共有62.3亿张,包含“网络”的网页为0.484亿张,包含“技术”的网页为0.973亿张。计算“亚洲”、“网络”、“技术”的TF-IDF值.

9. Knowledge Graph Alignment

image-20211202161306966

  • Example:

    • trigrams(nikon) $=\{$ nik, iko, kon $\}$
    • trigrams(nike) $=\{$ nik, ike $\}$
    • $\operatorname{sim}($​ nikon, nike $)=1 / 4$​
  • Compute the trigram based similarity between two strings University and Universe

  • trigrams(University)={Uni,niv,ive,ver,ers,rsi,sit,ity}

  • trigrams(Universe)={Uni,niv,ive,ver,ers,rse}

  • $\text{sim(University,Universe)}=5/8$

  • Compute the Jaccard similarity between the first-order neighbor classes of the classes Car and Automobile from different ontologies.

image-20211201110313663

  • StringSimilarity
    • len(book)=4,len(Textbook)=8
    • substring(book, Textbook)=book
    • len(substring)=4
    • strIngSimilarity(book, Textbook)=4/4=1
    • strIngSimilarity(Textbook, book)=4/8=1/2
  • Neighbor Class Set Similarity
    • Neighbor(book)={literature,volume}
    • Neighbor(Textbook)={volume,Engineering,Science}
    • Neighbor Class Set Similarity(book, Textbook)=1/2
    • Neighbor Class Set Similarity(Textbook, book)=1/3
  • Instance Set Similarity
    • Instance(book)={Red Sorghum,Introduction to Algorithm}
    • Instance(Textbook)={Red Sorghum}
    • Instance Set Similarity(book, Textbook)=1/2
    • Instance Set Similarity(Textbook, book)=1
  • Question: now we have String Similarity, Neighbor Class Set Similarity, Textual Context Similarity, and Instance Set Similarity, please tell which one belongs to the element-level matching techniques? Which one is a structure-level matching technique?

  • String Similarity,Textual Context Similarity element-level matching techniques

  • Neighbor Class Set Similarity, Instance Set Similarity structure-level matching technique

  • Compute the Jaccard similarity between the instance set of class “Book” and that of class “Textbook”.

image-20211231150900624

  • $A\cup B$:{Red Sorghum, The Republic of Wine, Data Structure, Introduction to Algorithm, Operation System}
  • $A\cap B$:{Data Structure, Introduction to Algorithm}
  • Jaccard = $\frac{|A\cap B|}{|A\cup B|}=\frac{2}{5}$​

  • Please tell the differences between the rdf:type relation and the general is-a relation.

    • general is-a relation be inter-class.
  • Given the Wikipedia article page of “NumPy”, including the first sentence, categories, and the infobox. Please write correct types from them (directly use plain strings).

image-20211222113819926

image-20211222113825979

image-20211222113833570

  • Answer

    • library, software, Free mathematics software, Free science software, Numerical analysis software for Linux, Numerical analysis software for MacOS, Numerical analysis software for Windows, Numerical programming languages, Python (programming language) scientific libraries, Software using the BSD license.
  • In the Wikipedia article page of “Tim Berners-Lee”, the categories are as follows. Please extract facts from them in turtle using YAGO category maps and the following prefixes without domain and range checking, and clarify the subsequent validation steps on the extracted fact.

image-20211222113235194

image-20211222113241159

image-20211222113245076

  • Answer

    • Extract facts

      1
      2
      3
      4
      5
      6
      expr:Tim Berners-Lee	expp:bornOnDate		"1955" ;
      expp:hasWonPrize expr:Turing Award ;
      expp:hasWonPrize expr:Roval Medal ;
      expp:hasWonPrize expr:Medal ;
      expp:hasWonPrize expr:Webby Award ;
      expp:hasWonPrize expr:Award ;
    • Validation

      • Check if expr:Tim Berners-Lee is in the domain of expp:bornOnDate and if "1955" is in the range of expp:bornOnDate;
      • Check if expr:Tim Berners-Lee is in the domain of expp:hasWonPrize and if expr:Turing Award expr:Roval Medal expr:Medal expr:Webby Award expr:Award is in the range of expp:hasWonPrize.
  • Extract is-a relations form the following sentence, and derive all is-a relations using the transitivity of the is-a relation (in RDF Triple)

    • We have rules for keeping animals, such as horses and other farm animals, especially cows and chickens.
  • Answer
    • farm animal $\to$ animal
    • horse $\to$ animal
    • cow $\to$ animal
    • chicken $\to$ animal
    • cow $\to$ farm animal
    • chicken $\to$ animal

  • Compute the TF-IDF values of knowledge, AI, Engineering with the statistics in the following tables, and rank them (Computing IDF using Ig).

  • Answer

    • Doc 1
    • Doc 2

    • Doc 3


  • Please compute the Levenshtein distance between two strings Saturday and Sunday.

  • Answer


  • Please compute the Jaccard similarity between “French” and “France” based on character-level bi-grams.
  • Answer
    • $\text{French} = \{Fr,re,en,nc,ch\}$
    • $\text{French} = \{Fr,ra,an,nc, ce\}$
    • $\text{Jaccard Similarity} = \frac{2}{5}$

  • Please tell the difference between local disambiguation and global disambiguation in entity linking.
    • Local Disambiguation (局部消歧)
      • Only use the contextual information of the given mention and target entity for disambiguation without considering the effects of other referent entities in the same table.
    • Global (Collective) Disambiguation (联合消歧)
      • Leverage the semantic associations between candidate referent entities to jointly disambiguate all entities for the mentions in a table.

  • Why do we need candidate generation in entity linking?
    • Identifying candidate referent entities of each string mention in table cells when given a knowledge base.
    • Select Top k entities for further disambiguation to accelerate.

10. Knowledge Graph Querying

image-20211208105032370

image-20211208105224660

image-20211208105746800

image-20211208110026967

  • Please rewrite the following triple pattern by using the same subject only once.
1
2
3
?a int:number 123789 .
?a int:pair 34567.
?a int:id 666777 .
1
2
3
?a int:number 123789;
int:pair 34567;
int:id 666777 .
  • Please rewrite the following triple pattern by using the same subject and predicate only once.
1
2
3
4
?a string:name ‘Bob’.
?a string:name ’Bobby’.
?a string:name ‘Boob’.
?a string:name ‘Bob_’.
1
?a string:name ‘Bob’,’Bobby’,‘Boob’,‘Bob_’.

Exercise
Given the dataset and the SPARQL query as follows, please write the query results.

1
2
3
4
5
6
7
8
# Graph: http://example/addresses
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<http://example/president25> foaf:givenName "Bill" .
<http://example/president25> foaf:familyName "McKinley" .
<http://example/president27> foaf:givenName "Bill" .
<http://example/president27> foaf:familyName "Taft" .
<http://example/president42> foaf:givenName "Bill" .
<http://example/president42> foaf:familyName "Clinton" .
1
2
3
4
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?family
WHERE { ?x foaf:givenName ?name .
?x foaf:familyName ?family .}
name family
“Bill” “McKinley”
“Bill” “Taft”
“Bill” “Clinton”

image-20211208112118772

  • Given the dataset and the SPARQL query as follows, please write the query results.
1
2
3
4
5
6
7
8
# Graph: http://example/addresses
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<http://example/president25> foaf:givenName "Bill" .
<http://example/president25> foaf:familyName "McKinley" .
<http://example/president27> foaf:givenName “Bob" .
<http://example/president27> foaf:id 159486.
<http://example/president42> foaf:givenName “Marry" .
<http://example/president42> foaf:familyName "Clinton" .
1
2
3
4
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?family
WHERE { ?x foaf:givenName ?name .
?x foaf:familyName ?family .}
name family
“Bill” “McKinley”
“Marry” “Clinton”
  • Matching RDF literals
1
2
3
4
5
6
7
@prefix dt: <http://example.org/datatype#> .
@prefix ns: <http://example.org/ns#> .
@prefix : <http://example.org/ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:x ns:p "cat"@en .
:y ns:p "42"^^xsd:integer .
:z ns:p "abc"^^dt:specialDatatype .
  • 第一种是找不到结果的,因为不加@是不一样的意义

image-20211208112234236

image-20211208113210921

image-20211208113243218

image-20211208113552720

name family
“Bill” “McKinley”

image-20211208163455567

  • Exercise
    • Given the dataset and the SPARQL query as follows, please write the query results.

image-20211208163522593

  • Answer
name family
“Bill” “McKinley”
“Bob”
“Marry”

image-20211208163647154

image-20211208163807370

image-20211208163833063

  • Please write the SPARQL query on “list all volcanos located in Italy or Norway” given the following data.
1
2
3
4
5
6
7
8
depedia:Mount_Etna	rdf:type	umbel-sc:Volcano;
rdfs:label "Etna";
p:location dbpedia:Italy.
depedia:Mount_Baker rdf:type umbel-sc:Volcano;
p:location dbpedia:United_States.
depedia:Beerenberg rdf:type umbel-sc:Volcano;
rdfs:label "Beerenberg"@en;
p:location dbpedia:Norway.
  • Answer
1
2
3
4
5
SELECT ?volcano rdf:type umbel-sc:Volcano.
WHERE {
{?Mount p:location dbpedia:Italy.}
UNION {?Mount p:location dbpedia:Norway.}
}

image-20211215095900388

image-20211215095921101

image-20211215100228264

image-20211215100406729

Exercise

  • Given the dataset and the SPARQL query as follows, please write the query results.
  • Dataset
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Default graph (stored at http://example.org/dft.ttl)
@prefix dc: <http://purl.org/dc/elements/1.1/> .
<http://example.org/bob> dc:publisher "Bob Hacker" .
<http://example.org/alice> dc:publisher "Alice Hacker" .

# Named graph: http://example.org/bob
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Bob" .
_:a foaf:mbox <mailto:bob@oldcorp.example.org> .

# Named graph: http://example.org/alice
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" .
_:a foaf:mbox <mailto:alice@work.example.org> .
  • SPARQL query
    • FROMNAMD可以省略,因为标明了GRAPH ?g
1
2
3
4
5
6
7
8
9
10
11
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?who ?g ?mbox
FROM <http://example.org/dft.ttl>
FROM NAMED <http://example.org/alice>
FROM NAMED <http://example.org/bob>
WHERE
{
?g dc:publisher ?who .
GRAPH ?g { ?x foaf:mbox ?mbox }
}
  • Answer:
who g mbox
“Bob Hacker” \http://example.org/bob \bob@oldcorp.example.org\
“Alice Hacker” \http://example.org/alice

image-20211215101912520

image-20211215102306783

image-20211215102537976

image-20211215104717764

image-20211215104955005

image-20211215105255654

  • Given the dataset and the SPARQL query as follows, please write the query results.

Q1

1
2
3
4
5
@prefix org: <http://example.com/ns#> .
_:a org:employeeName "Alice" .
_:a org:employeeId 12345 .
_:b org:employeeName "Bob" .
_:b org:employeeId 67890 .
1
2
3
4
5
6
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX org: <http://example.com/ns#>
DELETE {?person ?p ?o .}
where {?person org:employeeId ?id .
FILTER (?id > 50000)
?person ?p ?o .}
  • 删除后:
1
2
_:a org:employeeName "Alice" .
_:a org:employeeId 12345 .

2. Q2

1
2
3
4
5
6
7
8
@prefix org: <http://example.com/ns#> .
# Graph: http://person
_:a org:employeeName "Alice" .
_:a org:employeeId 12345 .
_:b org:employeeName "Bob" .
_:b org:employeeId 67890 .
# Graph: http://person2
_:c org:employeeId 13579
1
2
3
4
5
6
7
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX org: <http://example.com/ns#>
INSERT {GRAPH <http://person2> {?person ?p ?o .}}
where {Graph: <http://person>
{?person org:employeeId ?id .
FILTER (?id < 50000)
?person ?p ?o .}}
1
2
3
4
5
6
7
8
9
10
@prefix org: <http://example.com/ns#> .
# Graph: http://person
_:a org:employeeName "Alice" .
_:a org:employeeId 12345 .
_:b org:employeeName "Bob" .
_:b org:employeeId 67890 .
# Graph: http://person2
_:c org:employeeId 13579 .
_:a org:employeeName "Alice" .
_:a org:employeeId 12345 .

image-20211215110528044

image-20211215110550501

image-20211215110728620

image-20211215110734271

image-20211215110739530

  • Order the results.

image-20211215110814813

  • ASC for ascending (default) and DESC (e.g., DESC(?name)) for descending.

image-20211215110909379

LIMIT

  • limits the number of result:
    • 只返回5个

image-20211215111028802

OFFSET

  • position/index of the first reported results:

image-20211215111113085

  • Order of the result should be predictable (combine with ORDER BY)

BINDINGS

image-20211215111325636

image-20211215111347379

image-20211215111357307

VALUE

  • add data to the query directly.
    • 增加限制

image-20211215111516484

AGGREGATES

image-20211215111700791

  • Question: Please use natural language to explain this SPARQL query!
    • 查选课人数超过5人的课

image-20211215111848348

  • Question: Please use natural language to explain the above SPARQL queries!
  • Translate the following natural language sentences into SPARQL.

    • What are all the countries and their capitals in Africa?

      1
      2
      3
      4
      5
      # Given
      http://dbpedia.org/property/continent,
      http://dbpedia.org/resource/Africa,
      http://dbpedia.org/ontology/Country,
      http://dbpedia.org/ontology/capital.
      1
      2
      3
      4
      5
      6
      7
      8
      9
      PREFIX dbp: <http://dbpedia.org/>
      PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

      SELECT ?x ?y
      WHERE{
      ?x rdf:type dbp:ontology/Country .
      ?x dbp:property/continent dbp:resource/Africa .
      ?x dbp:ontology/capital ?y .
      }
    • What are the devices whose manufacturers are in Korea or Japan?

      1
      2
      3
      4
      5
      6
      #Given
      http://dbpedia.org/property/locationCountry,
      http://dbpedia.org/resource/Korea,
      http://dbpedia.org/resource/Japan,
      http://dbpedia.org/ontology/manufactury,
      http://dbpedia.org/ontology/Device.
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      PREFIX dbp: <http://dbpedia.org/>
      PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

      SELECT ?device
      WHERE{
      ?device rdf:type dbp:ontology/Device .
      ?device dbp:ontology/manufacturer ?location .
      {?location dbp:property/locationCountry dbp:resource/Japan .}
      UNION {?location dbp:property/locationCountry dbp:resource/Korea .}
      }
  • From the following RDF triples and the result of query, construct a SPARQL query.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    @prefix foaf: <http://xmlns.com/foaf/0.1/> . 
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

    _:a rdf:type foaf:Person .
    _:a foaf:name "Alice" .
    _:a foaf:mbox <mailto:alice@example.com> .
    _:a foaf:mbox <mailto:alice@work.example> .
    _:b rdf:type foaf:Person .
    _:b foaf:name "Bob" .

    | name | mbox |
    | :——— | :——————————————————————————- |
    | “Alice” | mailto:alice@example.com |
    | “Alice” | mailto:alice@work.example |
    | “Bob” | |

  • Answer

    1
    2
    3
    4
    5
    6
    7
    8
    9
    PREFIX foaf: <http://xmlns.com/foaf/0.1/>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

    SELECT ?name, ?mbox
    WHERE{
    ?x rdf:type foaf:Person ;
    foaf:name ?name .
    OPTIONAL {?x foaf:mbox ?mbox .}
    }
  • Given the dataset and query results as follows, please write the corresponding SPARQL Query.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    # Dataset
    dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ;
    rdfs:label "Etna" ;
    p:location dbpedia:Italy .
    dbpedia:Mount_Xiqiao rdf:type umbel-sc:Volcano ;
    rdfs:label "Mount_Xiqiao" ;
    p:location dbpedia:China .
    dbpedia:Beerenberg rdf:type umbel-sc:Volcano .
    rdfs:label "Beerenberg"@en ;
    p:location dbpedia:Norway .

    # SPARQL Results
    dbpedia:Mount_Etna rdfs:label "Etna" ;
    rdf:type myTypes:VolcanosOutsideChina.
    dbpedia:Beerenberg rdfs:label "Beerenberg"@en;
    rdf:type myTypes:VolcanosOutsideChina.
  • Answer TODO

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    CONSTRUCT {
    ?v rdfs:label ?name ;
    rdf:type myTypes:VolcanosOutsideChina .
    }
    WHERE {
    ?v rdf:type umbel-sc:Volcano;
    rdfs:label ?name.
    OPTIONAL {
    ?v p:location ?l . FILTER (?l = dbpedia:China)
    } FILTER (!BOUND(?l))
    }
  • From the following RDF triples and the result of query, construct a SPARQL query.

    @prefix foaf: http://xmlns.com/foaf/0.1/ .

    @prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns# .

    _:a rdf:type foaf:Person .

    _:a foaf:name “Alice” .

    _:a foaf:mbox alice@example.com .

    _:a foaf:mbox alice@work.example .

    _:b rdf:type foaf:Person .

    _:b foaf:name “Bob” .

image-20220105011822124

10. Cypher

image-20211215222102518

image-20211215222113317

1
2
3
4
5
6
CREATE(
node:Movie
{
title:"The American President"
}
)

image-20211215222313633

  • Create the relationship using cypher.

image-20211215222334213

1
2
3
4
CREATE
(n1:Person {name:'Oliver Stone'})
-[r:DIRECTED]
->(N2:Movie{title:"Wall Street"})
  • 查询所有节点
1
2
MATCH (n) 
RETURN n
  • 查询所有电影title
1
2
MATCH (movie:Movie)
RETURN movie.title
  • 名叫Oliver Stone相关联的事物标题
1
2
MATCH (director {name: 'Oliver Stone'})--(movie) 
RETURN movie.title
  • 与人物Oliver Stone相关联的电影标题
1
2
MATCH (:Person {name: 'Oliver Stone'})--(movie:Movie) 
RETURN movie.title
  • 与人物Oliver Stone相关联的关系类型
1
2
MATCH (:Person {name: 'Oliver Stone'})-[r]-(movie) 
RETURN type(r)
  • 出演名为‘Wall Street’电影的演员姓名
1
2
MATCH (wallstreet:Movie {title: 'Wall Street'})<-[:ACTED_IN]-(actor) 
RETURN actor.name
  • 参演或导演名为Wall Street的电影的所有人物
1
2
MATCH (wallstreet:Movie {title: 'Wall Street'})<-[:ACTED_IN|:DIRECTED]-(person:Person) 
RETURN person.name
  • 名为Wall Street的电影的中所有的角色
1
2
3
MATCH (wallstreet:Movie {title: 'Wall Street'})<-
[r:ACTED_IN]-(actor)
RETURN r.role
  • when nodes already exist, we can use MATCH first, then CREATE

image-20211215223827870

1
2
3
4
5
MATCH 
(charlie:Person {name: 'Charlie Sheen'}),
(rob:Person {name: 'Rob Reiner'})
CREATE
(rob)-[:TYPE INCLUDING A SPACE]->(charlie)
  • 查询参演名为‘The American President’的所有演员姓名

image-20211215224022480

1
2
3
4
MATCH
(person:Person)-[:ACTED_IN]
->(movie:Movie{title:"The American President"})
RETURN person.name
  • 查询包含角色’Card Fox’的电影名

image-20211215224512063

1
2
3
4
MATCH
(actor)-[:ACTED_{role:"Card Fox"}]
->(movie:Movie)
RETURN movie.title

image-20211215224917378

  • 删除名为UNKNOWN的节点
1
MATCH (n:Person {name: 'UNKNOWN’}) DELETE n
  • 删除数据库中所有节点及与其相连的关系
1
MATCH (n) DETACH DELETE n
  • 删除名为Andy的节点和与其相连的所有关系
1
MATCH (n {name: 'Andy’}) DETACH DELETE n
  • 删除Andy的所有KNOWS关系
1
MATCH (n {name: 'Andy'})-[r:KNOWS]->() DELETE r
  • 删除人物年龄为34岁的所有关系

image-20211215225242665

1
2
MATCH (person:Person {age:34})-[r]->()
DELETE r
  • Update a property
1
2
3
MATCH (n {name: 'Andy'}) 
SET n.age = toString(n.age)
RETURN n.name, n.age

image-20211215225919668

1
2
MATCH (n {name: 'Andy'}) 
SET n.position = 'Developer', n.surname = 'Taylor'
1
2
3
4
MATCH (p {name: 'Peter'}) 
SET p = {name: 'Peter Smith’,
position: 'Entrepreneur'}
RETURN p.name, p.age, p.position
  • 更新George的年龄为28

image-20211215230148658

1
2
3
MATCH (p {name:'George'})
SET p = {name: 'George'
age: 28}

image-20211215230420391

  • Write cypher to get the second graph
1
2
3
4
5
6
7
8
MATCH 
(actor1:Actor {name: 'Anthony Hopkins'}),
(actor2:Actor {name: 'Hitchcock'}),
(movie:Movie {title: 'Hitechcock'})
CREATE
(actor1)-[r:ACTS_IN]
->(movie)
DELETE actor2
  • Query all relationship types connected by actor Anthony Hopkins
1
2
3
MATCH
(actor:Actor {name: 'Anthony Hopkins'})-[r]-()
RETURN type(r)
  • Given the dataset and natural language query as follows, please write the corresponding Cypher Query.
    • Return the movie name containing the role “Bud Fox”

image-20211215115134978

  • Answer

    1
    2
    MATCH (actor) - [:ACTED_IN {role:'Bud Fox'}] -> (movie:Movie)
    RETURN movie.title

  • Delete all the relationships on the person who is 34 years old using Cypher.

    image-20211215211513803

  • Answer

    1
    2
    MATCH (n:Person {age: 34}) - [r] -> ()
    DELETE r
本文作者:Smurf
本文链接:http://example.com/2021/08/15/knowledge%20engineering/%E9%A2%98%E7%9B%AE/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可