PrincessIris
PrincessIris

Reputation: 25

How to extract the language tags from Turtle RDF data?

I want to extract the language tags. I am unable to access the language tags and getting an error that "@" is not used in queries.

This is my data:

@prefix msterms: <http://materials.springer.com/terms/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .

<http://km.springer.com/smat-substances/AFQMYGSOCIHFSLIX> msterms:elementSystem "C-F-H-O" ;
    msterms:inChIKey "DTQVDTLACAAQTR-UHFFFAOYSA-N" ;
    msterms:molecularFormula "C<sub>2</sub>HF<sub>3</sub>O<sub>2</sub>" ;
    owl:sameAs <http://www.wikidata.org/entity/Q412033>,
        <https://echa.europa.eu/substance-information/-/substanceinfo/100.000.846> ;
    skos:altLabel "2,2,2-Trifluoroacetic acid",
        "تریفلورواستیک اسید"@azb,
        "трифлуороцетна киселина"@bg,
        "kyselina trifluoroctová"@cs,
        "三氟乙酸"@zh-sg,
        "三氟乙酸"@zh-tw ;
    skos:prefLabel "trifluoroacetic acid" .

How can I extract the language tags?

This is my code, error and query:

from rdflib import Graph

# Load the Turtle data into a Graph object
graph = Graph()
graph.parse("/content/demo.ttl", format="ttl")

# Define SPARQL query to extract language tags
query = """
SELECT ?lang
WHERE {
  <http://km.springer.com/smat-substances/AFQMYGSOCIHFSLIX> skos:altLabel ?altLabel@?lang .
}
"""

# Execute the SPARQL query
qres = graph.query(query)

for row in qres:
  altlabel = row.altLabel  # Access the language-tagged literal
  lang = altlabel.language  # Extract the language tag directly
  print(lang)

Error:

ParseException: Expected SelectQuery, found '@'  (at char 105), (line:4, col:84)

Upvotes: 1

Views: 60

Answers (1)

CerebralFart
CerebralFart

Reputation: 3490

The SPARQL-query you've provided isn't valid, specifically the section ?altLabel@?lang. Instead you could try the following to extract the language from a literal:

SELECT ?lang
WHERE {
  <http://km.springer.com/smat-substances/AFQMYGSOCIHFSLIX> skos:altLabel ?altLabel .
  BIND( LANG(?altLabel) AS ?lang)
}

Upvotes: 0

Related Questions