예제 #1
0
# -*- coding:utf-8 -*-

from modules.simplegraph import SimpleGraph
bg = SimpleGraph()
bg.load('.semprog/chapter2/business_triples.csv')
# すべての投資銀行を探す
ibanks = [t[0] for t in bg.triples((None, 'industry', 'Investment Banking'))]
print ibanks
# 投資銀行からの政治献金
bank_contrib = {}
for b in ibanks:
    bank_contrib[b] = [t[0] for t in bg.triples((None, 'contributor', b))]
for b, contribs in bank_contrib.items():
    for contrib in contribs:
        print [t[2] for t in bg.triples((contrib, None, None))]
# ソフトウェア会社の所在地を表示する
sw = [t[0] for t in bg.triples((None, 'industry', 'Computer software'))]
print sw
locations = {}
for company in sw:
    for t in bg.triples((company, 'headquarters', None)):
        print t[2]
        locations[t[2]] = locations.setdefault(t[2], 0) + 1
# 3社以上のソフトウェア会社が存在するロケーションを表示する
print [loc for loc, c in locations.items() if c >= 3]
예제 #2
0
# -*- coding:utf-8 -*-

from modules.simplegraph import SimpleGraph

cg = SimpleGraph()
cg.load(".semprog/chapter2/celeb_triples.csv")
"""
Justin Timberlakeさんの交友関係を表示
"""
jt_relations = [t[0] for t in cg.triples((None, "with", "Justin Timberlake"))]
print jt_relations
for rel in jt_relations:
    print [t[2] for t in cg.triples((rel, "with", None))]
"""
Britney Spearsさんの主演映画を表示
"""
bs_movies = [t[2] for t in cg.triples(("Britney Spears", "starred_in", None))]
print bs_movies
"""
グラフ中の主演俳優すべてを表示
複数の映画で主演している俳優を重複表示させないためにsetを使用する(setは重複する要素を取り除いたコレクション)
"""
movie_stars = set()
for t in cg.triples((None, "starred_in", None)):
    movie_stars.add(t[0])
print movie_stars
"""
一人以上の映画スター(主演俳優)とデートしたセレブリティは誰か?
"""
for movie_star in movie_stars:
    relations = [t[0] for t in cg.triples((None, "with", movie_star))]
예제 #3
0
from modules.simplegraph import SimpleGraph

movie_graph = SimpleGraph()
movie_graph.add(('blade_runner', 'name', 'Blade Runner'))
movie_graph.add(('blade_runner', 'directed_by', 'ridley_scott'))
#movie_graph.add(('blade_runner','directed_by','hogehoge'))
movie_graph.add(('ridley_scott', 'name', 'Ridley Scott'))
print list(movie_graph.triples(('blade_runner', 'directed_by', None)))
print list(movie_graph.triples((None, 'name', None)))
print movie_graph.value('blade_runner', 'directed_by', None)