Example #1
0
    def from_ova(
        cls,
        obj: ova.Node,
        nlp: t.Optional[t.Callable[[str], t.Any]] = None,
    ) -> t.Optional[SchemeNode]:
        """Generate SchemeNode object from OVA Node object."""

        ova_type = obj["type"]
        ova_scheme = obj["text"]

        if ova_type in aif2scheme:
            scheme = aif2scheme[t.cast(aif.SchemeType, ova_type)]

            if scheme and (found_scheme :=
                           text2scheme[type(scheme)].get(ova_scheme)):
                scheme = found_scheme

            premise_descriptors = [
                str(node_id)
                for description, node_id in obj["descriptors"].items()
                if not description.lower().startswith("s_conclusion")
            ]

            timestamp = (dt.from_format(obj.get("date"), ova.DATE_FORMAT)
                         or pendulum.now())

            return cls(
                id=str(obj["id"]),
                metadata=Metadata(timestamp, timestamp),
                scheme=scheme,
                premise_descriptors=premise_descriptors,
            )
Example #2
0
    def from_aif(
        cls,
        obj: aif.Node,
        nlp: t.Optional[t.Callable[[str], t.Any]] = None,
    ) -> t.Optional[SchemeNode]:
        """Generate SchemeNode object from AIF Node object."""

        aif_type = obj["type"]
        aif_scheme: str = obj.get("scheme", obj["text"])

        if aif_type in aif2scheme:
            scheme = aif2scheme[t.cast(aif.SchemeType, aif_type)]

            # TODO: Handle formatting like capitalization, spaces, underscores, etc.
            # TODO: Araucaria does not use spaces between scheme names
            # aif_scheme = re.sub("([A-Z])", r" \1", aif_scheme)
            if scheme and (found_scheme :=
                           text2scheme[type(scheme)].get(aif_scheme)):
                scheme = found_scheme

            timestamp = (dt.from_format(obj.get("timestamp"), aif.DATE_FORMAT)
                         or pendulum.now())

            return cls(
                id=obj["nodeID"],
                metadata=Metadata(timestamp, timestamp),
                scheme=scheme,
            )
Example #3
0
    def from_sadface(
        cls,
        obj: sadface.Node,
        nlp: t.Optional[t.Callable[[str], t.Any]] = None,
    ) -> SchemeNode:
        """Generate SchemeNode object from SADFace Node object."""
        name = None

        if obj["name"] == "support":
            name = Support.DEFAULT
        elif obj["name"] == "attack":
            name = Attack.DEFAULT
        elif obj["name"] == "rephrase":
            name = Rephrase.DEFAULT
        elif obj["name"] == "preference":
            name = Preference.DEFAULT

        timestamp = pendulum.now()

        return cls(
            id=obj["id"],
            userdata=obj["metadata"],
            metadata=Metadata(timestamp, timestamp),
            scheme=name,
        )
Example #4
0
    def from_protobuf(
        cls,
        id: str,
        obj: graph_pb2.Node,
        resources: t.Mapping[str, Resource],
        participants: t.Mapping[str, Participant],
        reference_class: t.Type[Reference],
        nlp: t.Optional[t.Callable[[str], t.Any]] = None,
    ) -> SchemeNode:
        """Generate SchemeNode object from OVA Node object."""

        scheme_type = obj.scheme.WhichOneof("type")
        scheme = None

        if scheme_type is None:
            scheme = None
        elif scheme_type == "support":
            scheme = protobuf2support[obj.scheme.support]
        elif scheme_type == "attack":
            scheme = protobuf2attack[obj.scheme.attack]
        elif scheme_type == "rephrase":
            scheme = protobuf2rephrase[obj.scheme.rephrase]
        elif scheme_type == "preference":
            scheme = protobuf2preference[obj.scheme.preference]

        return cls(
            scheme,
            list(obj.scheme.premise_descriptors),
            Metadata.from_protobuf(obj.metadata),
            dict(obj.userdata.items()),
            id=id,
        )
Example #5
0
    def from_aml(
        cls,
        obj: et.Element,
        nlp: t.Optional[t.Callable[[str], t.Any]] = None,
        refutation=False,
    ) -> SchemeNode:
        """Generate SchemeNode object from AML Node format. obj is a AML "PROP" element."""

        # get id of PROP
        if "identifier" in obj.attrib:
            id = obj.get("identifier")
        else:
            id = None

        # read owners of PROP
        owner_list = obj.findall("OWNER")
        owners_lst = []
        if not owner_list:
            # if not empty, do something
            for owner in owner_list:
                owners_lst.append(owner.get("name"))
            owners = {"owners": ", ".join(owners_lst)}
        else:
            owners = {}

        # create timestamp
        timestamp = pendulum.now()

        # get scheme name
        scheme = None
        if refutation:
            scheme = Attack.DEFAULT
        else:
            inscheme = obj.find("INSCHEME")
            if inscheme is not None:  # if INSCHEME element is available
                # get scheme
                aml_scheme = inscheme.attrib["scheme"]
                contains_scheme = False
                for supp_scheme in Support:
                    if supp_scheme.value.lower().replace(
                            " ", "") in aml_scheme.lower().replace(" ", ""):
                        scheme = supp_scheme
                        contains_scheme = True
                        break
                if not contains_scheme:
                    scheme = Support.DEFAULT
            else:  # if INSCHEME element is not available
                scheme = Support.DEFAULT

        return cls(
            metadata=Metadata(timestamp, timestamp),
            scheme=scheme,
            userdata=owners,
            id=id,
        )
Example #6
0
    def __init__(
        self,
        metadata: t.Optional[Metadata] = None,
        userdata: t.Optional[Userdata] = None,
        id: t.Optional[str] = None,
    ):
        self._id = id or utils.uuid()
        self.metadata = metadata or Metadata()
        self.userdata = userdata or {}

        self.__post_init__()
Example #7
0
 def from_protobuf(cls, id: str, obj: graph_pb2.Participant) -> Participant:
     """Generate Participant object from PROTOBUF format Graph's Participant object."""
     return cls(
         obj.name,
         obj.username,
         obj.email,
         obj.url,
         obj.location,
         obj.description,
         Metadata.from_protobuf(obj.metadata),
         dict(obj.userdata.items()),
         id,
     )
Example #8
0
 def from_sadface(
     cls,
     obj: sadface.Node,
     nlp: t.Optional[t.Callable[[str], t.Any]] = None,
 ) -> AtomNode:
     """Generate AtomNode object from SADFace Node object."""
     timestamp = pendulum.now()
     return cls(
         id=obj["id"],
         text=utils.parse(obj["text"], nlp),
         userdata=obj["metadata"],
         metadata=Metadata(timestamp, timestamp),
     )
Example #9
0
    def from_ova(
        cls,
        obj: ova.Node,
        nlp: t.Optional[t.Callable[[str], t.Any]] = None,
    ) -> AtomNode:
        """Generate AtomNode object from OVA Node object."""
        timestamp = dt.from_format(obj.get("date"),
                                   ova.DATE_FORMAT) or pendulum.now()

        return cls(
            id=str(obj["id"]),
            metadata=Metadata(timestamp, timestamp),
            text=utils.parse(obj["text"], nlp),
        )
Example #10
0
    def from_aif(
        cls,
        obj: aif.Node,
        nlp: t.Optional[t.Callable[[str], t.Any]] = None,
    ) -> AtomNode:
        """Generate AtomNode object from AIF Node object."""
        timestamp = (dt.from_format(obj.get("timestamp"), aif.DATE_FORMAT)
                     or pendulum.now())

        return cls(
            id=obj["nodeID"],
            metadata=Metadata(timestamp, timestamp),
            text=utils.parse(obj["text"], nlp),
        )
Example #11
0
 def from_protobuf(
     cls,
     id: str,
     obj: graph_pb2.Edge,
     nodes: t.Mapping[str, Node],
 ) -> Edge:
     """Generate Edge object from PROTOBUF Edge format."""
     return cls(
         nodes[obj.source],
         nodes[obj.target],
         Metadata.from_protobuf(obj.metadata),
         dict(obj.userdata.items()),
         id=id,
     )
Example #12
0
 def from_protobuf(
     cls,
     id: str,
     obj: graph_pb2.Resource,
     nlp: t.Optional[t.Callable[[str], t.Any]] = None,
 ) -> Resource:
     """Generate Resource object from PROTOBUF format Graph's Resource object."""
     return cls(
         utils.parse(obj.text, nlp),
         obj.title,
         obj.source,
         dt.from_protobuf(obj.timestamp),
         Metadata.from_protobuf(obj.metadata),
         dict(obj.userdata.items()),
         id,
     )
Example #13
0
 def from_protobuf(
     cls,
     id: str,
     obj: graph_pb2.Node,
     resources: t.Mapping[str, Resource],
     participants: t.Mapping[str, Participant],
     reference_class: t.Type[Reference],
     nlp: t.Optional[t.Callable[[str], t.Any]] = None,
 ) -> AtomNode:
     """Generate AtomNode object from PROTOBUF Node object."""
     return cls(
         utils.parse(obj.atom.text, nlp),
         reference_class.from_protobuf(obj.atom.reference, resources, nlp),
         participants.get(obj.atom.participant),
         Metadata.from_protobuf(obj.metadata),
         dict(obj.userdata.items()),
         id=id,
     )
Example #14
0
    def __init__(
        self,
        source: Node,
        target: Node,
        metadata: t.Optional[Metadata] = None,
        userdata: t.Optional[Userdata] = None,
        id: t.Optional[str] = None,
    ):
        # if isinstance(source, AtomNode) and isinstance(target, AtomNode):
        #     raise ValueError("Cannot create an edge between two atom nodes.")

        self._id = id or utils.uuid()
        self._source = source
        self._target = target
        self.metadata = metadata or Metadata()
        self.userdata = userdata or {}

        self.__post_init__()
Example #15
0
    def from_ova(
        cls,
        obj: ova.Edge,
        nodes: t.Mapping[str, Node],
    ) -> t.Optional[Edge]:
        """Generate Edge object from OVA Edge format."""
        source_id = str(obj["from"]["id"])
        target_id = str(obj["to"]["id"])
        date = dt.from_format(obj.get("date"),
                              ova.DATE_FORMAT) or pendulum.now()

        if source_id in nodes and target_id in nodes:
            return cls(
                id=utils.uuid(),
                source=nodes[source_id],
                target=nodes[target_id],
                metadata=Metadata(date, date),
            )

        return None
Example #16
0
 def __init__(
     self,
     name: t.Optional[str] = None,
     username: t.Optional[str] = None,
     email: t.Optional[str] = None,
     url: t.Optional[str] = None,
     location: t.Optional[str] = None,
     description: t.Optional[str] = None,
     metadata: t.Optional[Metadata] = None,
     userdata: t.Optional[Userdata] = None,
     id: t.Optional[str] = None,
 ) -> None:
     self.name = name
     self.username = username
     self.email = email
     self.url = url
     self.location = location
     self.description = description
     self.metadata = metadata or Metadata()
     self.userdata = userdata or {}
     self._id = id or utils.uuid()
Example #17
0
    def from_aml(
        cls,
        obj: et.Element,
        nlp: t.Optional[t.Callable[[str], t.Any]] = None,
    ) -> AtomNode:
        """
        Generate Node object from AML Node format. obj is a AML "PROP" element.
        """
        # get id of PROP
        if "identifier" in obj.attrib:
            id = obj.get("identifier")
        else:
            id = None

        # read text of PROP
        text = obj.find("PROPTEXT").text

        # read owners of PROP
        owner_list = obj.findall("OWNER")
        owners_lst = []
        if not owner_list:
            # if not empty, do something
            for owner in owner_list:
                owners_lst.append(owner.get("name"))
            owners = {"owners": ", ".join(owners_lst)}
        else:
            owners = {}

        # create timestamp
        timestamp = pendulum.now()

        return cls(
            id=id,
            text=utils.parse(text, nlp),
            metadata=Metadata(timestamp, timestamp),
            userdata=owners,
        )