Skip to content

YAML Parser

Bases: Parser

A parser for YAML files.

Source code in src/typedlogic/parsers/yaml_parser.py
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
66
67
68
69
class YAMLParser(Parser):
    """
    A parser for YAML files.
    """

    def parse(self, source: Union[Path, str, TextIO], **kwargs) -> Theory:
        if isinstance(source, Path):
            source = source.open()
        if not isinstance(source, (str, TextIOWrapper)):
            raise ValueError(f"Invalid source type: {type(source)}")
        obj = yaml.safe_load(source)
        return from_object(obj)

    def parse_ground_terms(self, source: Union[Path, str, TextIO], **kwargs) -> List[Term]:
        """
        Parse a source and return a list of sentences.

        :param source:
        :param kwargs:
        :return:
        """
        file_name = None
        if isinstance(source, Path):
            source = source.open()
            file_name = str(source)
        if isinstance(source, str):
            file_name = source
        if not isinstance(source, (str, TextIOWrapper)):
            raise ValueError(f"Invalid source type: {type(source)}")
        obj = yaml.safe_load(source)
        if file_name:
            default_predicate = Path(file_name).stem.split(".")[0]
        else:
            default_predicate = None
        if isinstance(obj, list):
            return [self._ground_term(o, default_predicate) for o in obj]
        elif isinstance(obj, dict):
            terms = []
            for k, objs in obj.items():
                terms.extend([self._ground_term(o, k) for o in objs])
            return terms
        else:
            raise ValueError(f"Invalid object type: {type(obj)}")

    def _ground_term(self, obj: Union[Dict[str, Any], List[Any]], default_predicate: Optional[str] = None) -> Term:
        if isinstance(obj, dict):
            if "@type" in obj:
                default_predicate = obj["@type"]
                del obj["@type"]
            if not default_predicate:
                raise ValueError("No predicate found")
            return Term(default_predicate, obj)
        elif isinstance(obj, list):
            if not default_predicate:
                raise ValueError("No predicate found")
            return Term(default_predicate, *obj)
        else:
            raise ValueError(f"Invalid object type: {type(obj)}")

parse_ground_terms(source, **kwargs)

Parse a source and return a list of sentences.

Parameters:

Name Type Description Default
source Union[Path, str, TextIO]
required
kwargs
{}

Returns:

Type Description
List[Term]
Source code in src/typedlogic/parsers/yaml_parser.py
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
def parse_ground_terms(self, source: Union[Path, str, TextIO], **kwargs) -> List[Term]:
    """
    Parse a source and return a list of sentences.

    :param source:
    :param kwargs:
    :return:
    """
    file_name = None
    if isinstance(source, Path):
        source = source.open()
        file_name = str(source)
    if isinstance(source, str):
        file_name = source
    if not isinstance(source, (str, TextIOWrapper)):
        raise ValueError(f"Invalid source type: {type(source)}")
    obj = yaml.safe_load(source)
    if file_name:
        default_predicate = Path(file_name).stem.split(".")[0]
    else:
        default_predicate = None
    if isinstance(obj, list):
        return [self._ground_term(o, default_predicate) for o in obj]
    elif isinstance(obj, dict):
        terms = []
        for k, objs in obj.items():
            terms.extend([self._ground_term(o, k) for o in objs])
        return terms
    else:
        raise ValueError(f"Invalid object type: {type(obj)}")