fredlib: FRED Python library

The Fred Python library enables using FRED from Python. It provides functions for getting a Fred graph from a sentence and for managing such a graph for getting useful information such as the set of class and instance nodes, the set of role edges, the set of events and so on.

Installing fredlib



Download fredlib.py and copy it in your project folder or in the Python libraries folder. fredlib depends on the following packages:

Please be sure that such packages are installed before using fredlib.

Using fredlib



Write in your python shell or script:

    #import fredlib
    sentence = “This is my sentence” #write here your sentence
    filename = “myfile.rdf” #a file for storing the Fred’s rdf graph
    g = fredlib.getFredGraph(fredlib.preprocessText(sentence),filename)
preprocessText() performs a preprocessing of the sentence to remove unsupported characters (e.g. #) and to convert abbreviations (e.g. “I’m” into “I am”). This step can be skipped by substituting the last row with.

    g = fredlib.getFredGraph(sentence,filename)
g is an object of the class FredGraph that contains the RDF graph (accessible by g.rdf) and a suite of utility methods. The RDF graph can be managed through the rdflib library. We simplify the access to graph elements by exporting a collection of utility methods. This collection is described below by a set of examples.
  • Getting the set of nodes
    for n in g.getNodes():
        print n
    print the set of URIs of all nodes in g.

  • Getting the set of class nodes
    for n in g.getClassNodes():
        print n
    print the set of URIs of all class nodes in g. Class nodes are all nodes that are rdf:Type of other nodes.

  • Getting the set of individual nodes
    for n in g.getIndividualNodes():
        print n
    print the set of URIs of all individual nodes in g. Individual nodes are all nodes that are not class nodes.

  • Getting the set of event nodes
    for n in g.getEventNodes():
        print n
    print the set of URIs of all event nodes in g. Event nodes are all nodes that have a class fred:Event as their rdf:Type or as rdfs:subClassOf of their rdf:Type. Events and Situations are usually referred as "frames" in the NLP terminology.

  • Getting the set of situation nodes
    for n in g.getSituationNodes():
        print n
    print the set of URIs of all situation nodes in g. Situation nodes are all nodes that have a class boxer:Situation as their rdf:Type. Events and Situations are usually referred as "frames" in the NLP terminology.

  • Getting the set of named entity nodes
    for n in g.getNamedEntityNodes():
        print n
    print the set of URIs of all named entity nodes in g. Named entities are nodes that correspond to the proper name of some entity (e.g. person, organization, place).

  • Getting the set of skolemized entity nodes
    for n in g.getSkolemizedEntityNodes():
        print n
    print the set of URIs of all skolemized entity nodes in g. Skolemized entities are nodes whose name is assigned to the system to refer to some specific individual (e.g. a person, an object or an animal).

  • Getting the set of quality nodes
    for n in g.getQualityNodes():
        print n
    print the set of URIs of all quality nodes in g. Qualities are all nodes that are fred:hasQuality of other nodes.

  • Getting the set of literal nodes
    for n in g.getLiteralNodes():
        print n
    print the set of URIs of all literal nodes in g. Literals are nodes that represent data, for example a quantity, a date, a number etc.

  • Getting the set of event class nodes
    for n in g.getEventClasses():
        print n
    print the set of URIs of all class nodes that correspond to events in g. They correspond to all class nodes that are (directly or indirectly) rdfs:subClassOf the node fred:Event.

  • Getting the set of class nodes that represent general concepts
    for n in g.getOtherConceptsClasses():
        print n
    print the set of URIs of all concept nodes in g that do not correspond to events.

  • Getting information about nodes
    ns = g.getInfoNodes()
    for n in ns:
        print n, ns[n].LogicalType, ns[n].IndividualType, ns[n].ClassType, ns[n].Provenance
    print the list of nodes in g. For each node it prints its URI and its LogicalType, ClassType (None if it is an individual), IndividualType (None if it is a class) and ResourceType. getInfoNodes()returns a dictionary whose keys are node URIs and values are objects of the class FredNode that contain LogicalType, ClassType, IndividualType and Provenance of each node. Possible values for these attributes (they are Enum types) are:

    Possible values for LogicalType:

    • Class
    • Individual

    Possible values for ClassType:

    • Event
    • OtherConcepts

    Possible values for IndividualType:

    • Situation
    • Event
    • NamedEntity
    • SkolemizedEntity
    • Quality
    • Literal

    Possible values for Provenance:

    • Fred
    • Dbpedia
    • Verbnet
    • Wordnet
    • Schemaorg
    • Dolce
  • Getting the set of edges
    for (a,b,c) in g.getEdges():
        print a,b,c
    print the set of all triples in g. A triple is composed by the URI of the source node, the URI of the relation and the URI of the destination node.

  • Getting information about edges
    es = g.getInfoEdges()
    for e in es:
        print e, es[e].Type
              
    print the set of all triples in g with the type of each triple. getInfoEdges()returns a dictionary whose keys are triples and values are objects of the class FredEdge that contain the Type of each edge. Types are defined in terms of EdgeMotifs. Possible values for Type are:
    • Identity
    • Type
    • SubClass
    • Equivalence
    • Role
    • Modality
    • Negation
    • Property

    Other information about edges can be retrieved using motifs (see next section)

Operations with motifs



Motifs enable retrieving structural information about FRED graphs. There are four kinds of motifs: EdgeMotifs, PathMotifs, ClusterMotifs and NaryMotifs. EdgeMotifs can be used for retrieving particular types of edges. PathMotifs correspond to pairs of nodes that are connected by a path with some constraints. ClusterMotifs correspond to clusters of related nodes (e.g. all equivalent nodes). NaryMotifs represent structures with a "root" node connected to a set of nodes, each of them with some role.

  • Getting all 'owl:sameAs' edges (corresponding to EdgeMotif Identity)
    for (a,b,c) in g.getEdgeMotif(EdgeMotif.Identity):
        print a,b,c
    print the set of triples with predicate 'owl:sameAs'

  • Getting all 'rdf:type' edges (corresponding to EdgeMotif Type)
    for (a,b,c) in g.getEdgeMotif(EdgeMotif.Type):
        print a,b,c
    print the set of triples with predicate 'rdf:type'

  • Getting all 'rdfs:subClassOf' edges (corresponding to EdgeMotif SubClass)
    for (a,b,c) in g.getEdgeMotif(EdgeMotif.SubClass):
        print a,b,c
    print the set of triples with predicate 'rdfs:subClassOf'

  • Getting all 'owl:equivalentClass' edges (corresponding to EdgeMotif Equivalence)
    for (a,b,c) in g.getEdgeMotif(EdgeMotif.Equivalence):
        print a,b,c
    print the set of triples with predicate 'owl:equivalentClass'

  • Getting the set of role edges (EdgeMotif Role)
    for (a,b,c) in g.getEdgeMotif(EdgeMotif.Role):
    	print a,b,c
    print the set of all role edges in g. A role edge has an event node as a source and connects it to entities that have some role in such an event.

  • Getting the set of non role edges
    edges = g.getEdges()
    edges.difference(g.getEdgeMotif(EdgeMotif.Role))
    for (a,b,c) in edges:
        print a,b,c
    print the set of all non-role edges in g. A non-role edge is an edge that is not a role edge.

  • Getting all negations (corresponding to EdgeMotif Negation)
    for (a,b,c) in g.getEdgeMotif(EdgeMotif.Negation):
        print a,b,c
    print the set of triples with predicate 'boxing:hasTruthValue', i.e. the set of triples that correspond to negation.

  • Getting all modality edges (corresponding to EdgeMotif Modality)
    for (a,b,c) in g.getEdgeMotif(EdgeMotif.Modality):
        print a,b,c
    print the set of triples with predicate 'boxing:hasModality', i.e. edges that link to modalities.

  • Getting all property edges (corresponding to EdgeMotif Property)
    for (a,b) in g.getEdgeMotif(EdgeMotif.Property):
        print a,b
    print the set of triples that do not correspond to any of the previously defined EdgeMotifs.

  • Getting all occurrences of PathMotif Type
    for (a,b) in g.getPathMotif(PathMotif.Type):
        print a,b
    print all pairs of nodes that are connected by a path with a first edge 'rdf:type' and an optional sequence of edges 'rdfs:subClassOf' of any length.

  • Getting all occurrences of PathMotif SubClass
    for (a,b,c) in g.getEdgeMotif(PathMotif.SubClass):
        print a,b,c
    print all pairs of nodes that are connected by any sequence of edges 'rdfs:subClassOf'.

  • Getting all occurrences of ClusterMotif Identity
    for cluster in g.getClusterMotif(ClusterMotif.Identity):
        print cluster
    print a list of set of nodes that are identical (owl:sameAs of other nodes in the same cluster - edge direction is not considered).

  • Getting all occurrences of ClusterMotif Equivalence
    for cluster in g.getClusterMotif(ClusterMotif.Equivalence):
        print cluster
    print a list of set of concepts that are identical (owl:equivalentClass of other nodes in the same cluster - edge direction is not considered).

  • Getting all occurrences of ClusterMotif IdentityEquivalence
    for cluster in g.getClusterMotif(ClusterMotif.IdentityEquivalence):
        print cluster
    print a list of set of nodes that are identical or equivalent (owl:sameAs or owl:equivalentClass of other nodes in the same cluster - edge direction is not considered).

  • Getting all occurrences of NaryMotif of the kind Event
    motif_occurrences = g.getNaryMotif(NaryMotif.Event)
    for event in motif_occurrences:
    	roles = motif_occurrences[event]
    	print event,roles
    print a list of occurrences of the N-ARY motif concerning Events. Every occurrence is identified by a key (the event node) and has as a value an object of the class NaryMotifOccurrence that extends the class dict. The dictionary associates roles to corresponding nodes. Roles are defined in an enumerative class. Possible values are:
    • Agent
    • Patient
    • Theme
    • Location
    • Time
    • Involve
    • Declared
    • VNOblique
    • LocOblique
    • ConjOblique
    • Extended
    • Associated
    The class NaryMotifOccurrence contains methods for getting the node that has an agentive role in the event (getAgentiveRole()), the node that has a passive role in the event (getPassiveRole()) and the set of nodes that have oblique role (getObliqueRoles()).

  • Getting all occurrences of NaryMotif of the kind Situation
    motif_occurrences = g.getNaryMotif(NaryMotif.Situation)
    for event in motif_occurrences:
    	roles = motif_occurrences[event]
    	print event,roles
    print a list of occurrences of the N-ARY motif concerning Situation. Occurrences are listed as for the N-ARY motif Event.

  • Getting all occurrences of NaryMotif that correspond to events but are not frames
    motif_occurrences = g.getNaryMotif(NaryMotif.OtherEvents)
    for event in motif_occurrences:
    	roles = motif_occurrences[event]
    	print event,roles
    print a list of occurrences of the N-ARY motif concerning events that are not frames. Occurrences are listed as for the N-ARY motif Event.

Powered by

K~tools Tagme Apache Stanbol C&C tools