ontometer_corpus.pipeline.parse¶
Phase 1: Parse.
About
Processes all corpus_registry rows with parse_status IS NULL. For each row, attempts to parse the raw ontology file and extract:
- Ontology IRI (declared by the file, else recovered from the registry's source_id — see _registry_declared_iri), version IRI, version info
- Triple count, class count, property count, individual count (with its declared and SKOS-concept subsets — see _individual_counts)
- owl:imports list
- Content hash (SHA-256 of sorted class+property IRIs, blank nodes excluded)
- Namespace footprint (referenced_namespaces / own_namespaces), which feeds the dependency network's namespace-usage edges
- A per-file structural sidecar (subclass edges + entities), written
to ONTOMETER_STORE/cache/structure/v
/ / .parquet
Parse strategy (in order): 0. files whose estimated decompressed size exceeds _STREAM_THRESHOLD_BYTES go to a bounded-memory streaming path (pyoxigraph, read as N-Triples) 1. rdflib — tries 'xml', 'turtle', 'n3', 'json-ld', 'nt', 'trig' 2. custom parsers — offline, lightweight parsers for OBO and OWL/XML 3. owlready2 — fallback helper (strips imports to prevent network requests)
Results are written back to corpus_registry via db.py helpers. Failures are recorded as parse_status='fail' with parse_error populated.
Workers run in a bounded ProcessPoolExecutor (capped max_workers,
max_tasks_per_child=1) over tasks sorted by estimated decompressed size.
Structural sidecars are written inside each worker, so large hierarchies
never cross the process-pool boundary — only scalar metadata does. See
ontometer_corpus/network/README.md for how the sidecars are consumed.
No graph-isomorphism digest is computed: rdflib's canonicalisation can run unboundedly long on blank-node-dense graphs. Dedup relies on content_hash plus the raw-bytes file_hash instead.
Attributes¶
LICENCE_PREDICATES = frozenset({'http://purl.org/dc/terms/license', 'http://purl.org/dc/terms/rights', 'http://purl.org/dc/elements/1.1/rights', 'http://creativecommons.org/ns#license', 'http://schema.org/license', 'https://schema.org/license', 'http://www.w3.org/1999/xhtml/vocab#license'})
module-attribute
¶
MAX_LICENCE_VALUES = 5
module-attribute
¶
MAX_LICENCE_CHARS = 500
module-attribute
¶
STREAM_FORMATS = {'xml': (RdfFormat.RDF_XML,), 'turtle_like': (RdfFormat.TRIG, RdfFormat.N_QUADS)}
module-attribute
¶
Classes¶
Functions:¶
try_rdflib(raw)
¶
Try rdflib formats against the file content, cheapest path first.
Returns¶
(graph, format_used, error) graph — populated Graph on success, None on failure format_used — format string that worked, None on failure error — last exception message on failure, None on success
Source code in ontometer_corpus/pipeline/parse.py
try_owlready2(content, orig_suffix)
¶
Fall back to owlready2 (handles OWL/XML and OBO). Converts the ontology to N-Triples, then loads into rdflib.
content is the already-read file bytes (reused from the caller to
avoid a redundant full-file read) and orig_suffix is the source file's
extension, used to pick the temp-file suffix owlready2 infers format from.
Returns (graph, 'owlready2', None) on success, (None, None, error) on failure.
Source code in ontometer_corpus/pipeline/parse.py
compute_content_hash(graph)
¶
Compute a stable content fingerprint for deduplication.
Algorithm
- Collect all non-blank-node subjects declared as one of _CLASS_TYPES + _PROPERTY_TYPES.
- Sort the IRI strings lexicographically.
- Join with newlines, encode UTF-8, SHA-256.
Blank nodes are excluded because they vary across serialisations of the same logical ontology.
The type list is shared with class_count/property_count via _CLASS_TYPES / _PROPERTY_TYPES — see the note there for why that coupling is mandatory.
Source code in ontometer_corpus/pipeline/parse.py
extract_metadata(graph, source_id=None)
¶
Extract all parse (Phase 1) metadata fields from a parsed rdflib Graph.
Returns a dict with keys matching corpus_registry columns, plus the transient structural keys (subclass_edges, entities) that parse_worker diverts to the sidecar rather than the DB.
source_id is optional and used only to recover an ontology IRI the file itself never declares — see _registry_declared_iri.
Source code in ontometer_corpus/pipeline/parse.py
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 | |
configure_paths(paths)
¶
Point this module's path helpers at paths for the current process.
Source code in ontometer_corpus/pipeline/parse.py
scratch_dir()
¶
Return a disk-backed directory for decompression temp files, or None to fall back to tempfile's platform default (only relevant when no object store is configured, e.g. when parse.py is used outside the normal pipeline).
Source code in ontometer_corpus/pipeline/parse.py
estimate_real_size(filepath)
¶
Cheaply estimate a file's decompressed size without fully decompressing it, so oversized archives route to the streaming parser and run_parse's scheduling sort reflects actual parse cost rather than on-disk size. Returns (estimated_bytes, kind, zip_member_name_or_None).
Source code in ontometer_corpus/pipeline/parse.py
open_content_stream(filepath, kind, zip_member)
¶
Open a fresh, from-byte-zero binary stream over the actual (decompressed) content. Returns (stream, extra_handle_to_close_or_None) — the zip case needs the ZipFile itself kept open and closed alongside its member stream.
Source code in ontometer_corpus/pipeline/parse.py
ntriples_lines(byte_stream, rdf_format)
¶
Stream byte_stream as canonical N-Triples lines, one triple at a time,
so memory stays bounded at any file size. Raises SyntaxError where the
input stops being valid rdf_format. Parsing is lenient about malformed
IRIs and language tags, which published files carry and the in-memory
rdflib path also accepts.
Source code in ontometer_corpus/pipeline/parse.py
parse_worker(task_info)
¶
Multiprocessing worker function to parse a single ontology file. Does not use/share database connections directly.
The task is (row_id, retrieved_file, store_dir, source_id, file_hash, corpus_dir).
It carries both roots alongside the already-resolved file path, because a
worker is a separate process: under spawn it re-imports this module with
no inherited state, so it configures its own paths from the task rather
than assuming the parent's or the environment's. source_id is only consulted to recover an
ontology IRI the file itself never declares (see _registry_declared_iri).
Source code in ontometer_corpus/pipeline/parse.py
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 | |
run_parse(db_path, batch_size=500, max_workers=None, parallel=True, paths=None)
¶
Parse all unparsed rows in corpus_registry.
parallel=True (default) uses a ProcessPoolExecutor with the concurrency and memory-safety measures below. parallel=False processes rows one at a time in the current process instead — useful when a file is suspected of causing runaway CPU/memory (e.g. a ZIP-wrapped ontology that decompresses to hundreds of MB), since it isolates that cost to a single, easily Ctrl+C-able process instead of spreading it across worker processes whose shutdown blocks on in-flight tasks.
Concurrency and memory-safety measures (parallel=True only): - max_workers is capped (see _DEFAULT_MAX_WORKERS) instead of defaulting to os.cpu_count(), bounding peak concurrent RAM. - max_tasks_per_child=1 recycles each worker process after a single file, so memory that the interpreter/native extensions don't return to the OS (common with lxml/owlready2 on large files) can't accumulate across the whole run. - Tasks are sorted by estimated size ascending and submitted in batch_size chunks (each chunk fully drains before the next is submitted), so large ontologies are less likely to land on workers simultaneously and a crash only loses progress within the current chunk. The sort key is the estimated decompressed size (ZIP member size, gzip ISIZE); bzip2 has no cheap size trailer, so a bzip2 file sorts by its on-disk size and can still decompress to something far larger.
Source code in ontometer_corpus/pipeline/parse.py
2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 | |