forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_movie.py
65 lines (57 loc) · 2.52 KB
/
display_movie.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from rdflib import Graph, Namespace
from rdflib.plugins.sparql import prepareQuery
def get_movie_details(movie_name):
# Load the RDF data
g = Graph()
g.parse("data/Movies.rdf")
# Define namespaces
rdf = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
rdfs = Namespace("http://www.w3.org/2000/01/rdf-schema#")
ont = Namespace("http://www.semanticweb.org/owl/owlapi/turtle#")
# Query RDF graph for movie information
query_text = """
SELECT ?year ?country ?genre ?actor
WHERE {
?movie rdf:type ont:Movie ;
rdfs:label ?title .
OPTIONAL { ?movie ont:hasYear ?year }
OPTIONAL { ?movie ont:hasCountry ?country }
OPTIONAL { ?movie ont:hasGenre ?genre }
OPTIONAL { ?movie ont:hasActor ?actor }
FILTER (?title = "%s")
}
""" % movie_name
query = prepareQuery(query_text, initNs={"rdf": rdf, "ont": ont , "rdfs": rdfs})
results = g.query(query)
# Check if any results are found
if len(results) == 0:
return None
else:
# Initialize lists to store genres and actors
genres = []
actors = []
year = None
country = None
# Iterate through results and accumulate genres and actors
for row in results:
_, _, genre, actor = row
if genre and genre.split("#")[1] not in genres:
genres.append(genre.split("#")[1])
if actor and actor.split("#")[1] not in actors:
actors.append(actor.split("#")[1])
if row.year and not year:
year = row.year
if row.country and not country:
country = row.country
# Print movie details
# print("Year:", year if year else "Not available")
# print("Country:", country if country else "Not available")
# print("Genres:", ', '.join(genres) if genres else "Not available")
# print("Actors:", ', '.join(actors) if actors else "Not available")
details = []
details.append("Year: " + (year if year else "Not available"))
details.append("Country: " + (country if country else "Not available"))
details.append("Genres: " + (', '.join(genres) if genres else "Not available"))
# details.append("Actors: " + (', '.join(actors) if actors else "Not available"))
details.append("Actors: " + (', '.join(actor.replace('_', ' ') for actor in actors) if actors else "Not available"))
return details