Code Flow Diagrams for parse() Function
Main Function Flow
flowchart TD
A[parse: ontology, ontology_path, input_text] --> B[Setup Logging]
B --> C[getParser: Get/Create Parser]
C --> D{Parser in Cache?}
D -->|No| E[Create FindOntologyData]
E --> F[Create MutatoAPI]
F --> G[Cache Parser]
G --> H[Get swap_input_text method]
D -->|Yes| H
H --> I[Initialize entities = empty list]
I --> J[segment_text: Split into paragraphs]
J --> K[For Each Paragraph]
K --> L[For Each Sentence]
L --> M[swap_synonyms: Parse sentence]
M --> N[Extract tokens with 'swaps' key]
N --> O[Add to entities list]
O --> P{More sentences?}
P -->|Yes| L
P -->|No| Q{More paragraphs?}
Q -->|Yes| K
Q -->|No| R[Deduplicate: list set entities]
R --> S[Return unique entities]
Parser Initialization Flow
flowchart TD
A[getParser: ontology, ontology_path] --> B[Create Cache Key]
B --> C{Key exists in parsers dict?}
C -->|Yes| D[Return Cached Parser]
C -->|No| E[Create FindOntologyData]
E --> F[Set ontologies = ontology]
F --> G[Set absolute_path = ontology_path]
G --> H[Set namespace = 'http://bast.ai']
H --> I[Initialize FindOntologyData]
I --> J[Create MutatoAPI with finder]
J --> K[Cache parser in parsers dict]
K --> L[Return Parser]
Text Segmentation Flow
flowchart TD
A[segment_text: input_text] --> B[Segmenter.input_text]
B --> C[PerformParagraphSegmentation]
C --> D[Split by double newlines]
D --> E[For Each Paragraph]
E --> F[PerformSentenceSegmentation]
F --> G[Use spaCy NLP]
G --> H[Post-process sentences]
H --> I[Return Nested List]
I --> J["Nested list: paragraph1 with sentences, paragraph2 with sentences"]
flowchart TD
A[swap_input_text: sentence] --> B[Tokenizer.input_text]
B --> C[Tokenize into words]
C --> D[ParseInputTokens.process]
D --> E[Parse tokens with NLP]
E --> F{Has tokens?}
F -->|Yes| G[swap_input_tokens]
F -->|No| H[Return empty list]
G --> I[Return swapped tokens]
flowchart TD
A[swap_input_tokens: tokens, ctr] --> B[Convert SpacyResult to dict if needed]
B --> C[Perform Exact Matching]
C --> D[Match against ontology labels]
D --> E{rdfs:label<br/>rdfs:seeAlso<br/>skos:altLabel<br/>:inflection}
E --> F{Spans exist?}
F -->|Yes| G[Perform Span Matching]
F -->|No| H[Perform Hierarchical Matching]
G --> H
H --> I[Match parent/child relationships]
I --> J{ctr < 2?}
J -->|Yes| K[Recursive Call: swap_input_tokens]
K --> L[ctr = ctr + 1]
L --> C
J -->|No| M[Return swapped tokens]
Exact Matching Detail
flowchart TD
A[PerformExactMatching] --> B[For gram_size 5 to 1]
B --> C[SlidingWindowExtract]
C --> D[Extract n-grams from tokens]
D --> E[SlidingWindowLookup]
E --> F[Query Ontology]
F --> G{Match found?}
G -->|Yes| H[Add to swaps]
G -->|No| I[Continue]
H --> J{More gram sizes?}
I --> J
J -->|Yes| B
J -->|No| K[Return tokens with swaps]
Complete Data Flow
flowchart LR
A[Input Text] --> B[Segmenter]
B --> C[Paragraphs]
C --> D[Sentences]
D --> E[Tokenizer]
E --> F[Tokens]
F --> G[ParseInputTokens]
G --> H[Parsed Tokens]
H --> I[Exact Matching]
I --> J[Span Matching]
J --> K[Hierarchical Matching]
K --> L[Swapped Tokens]
L --> M{Has 'swaps'?}
M -->|Yes| N[Extract 'normal' value]
M -->|No| O[Skip token]
N --> P[Add to entities]
O --> Q[Next token]
P --> Q
Q --> R[Deduplicate]
R --> S[Output: Unique Entities]
Component Interaction Diagram
graph TB
subgraph "parse() Function"
P[parse]
end
subgraph "Parser Management"
GP[getParser]
CACHE[Parser Cache]
FOD[FindOntologyData]
MA[MutatoAPI]
end
subgraph "Text Processing"
ST[segment_text]
SEG[Segmenter]
PARA[Paragraph Segmentation]
SENT[Sentence Segmentation]
end
subgraph "Entity Extraction"
SWAP[swap_input_text]
TOK[Tokenizer]
PARSE[ParseInputTokens]
SWAP_TOK[swap_input_tokens]
end
subgraph "Matching Pipeline"
EXACT[Exact Matching]
SPAN[Span Matching]
HIER[Hierarchical Matching]
end
subgraph "Ontology"
OWL[OWL File]
SPARQL[SPARQL Queries]
LOOKUP[Lookup Structures]
end
P --> GP
GP --> CACHE
GP --> FOD
FOD --> MA
MA --> OWL
OWL --> SPARQL
SPARQL --> LOOKUP
P --> ST
ST --> SEG
SEG --> PARA
SEG --> SENT
P --> SWAP
SWAP --> TOK
SWAP --> PARSE
SWAP --> SWAP_TOK
SWAP_TOK --> EXACT
SWAP_TOK --> SPAN
SWAP_TOK --> HIER
EXACT --> LOOKUP
SPAN --> LOOKUP
HIER --> LOOKUP
Sequence Diagram
sequenceDiagram
participant User
participant parse
participant getParser
participant FindOntologyData
participant MutatoAPI
participant Segmenter
participant swap_input_text
participant swap_input_tokens
participant Matching
User->>parse: parse(ontology, path, text)
parse->>getParser: getParser(ontology, path)
getParser->>FindOntologyData: Create finder
FindOntologyData->>FindOntologyData: Load OWL file
FindOntologyData->>FindOntologyData: Build lookup structures
getParser->>MutatoAPI: Create parser
getParser-->>parse: Return parser
parse->>Segmenter: segment_text(text)
Segmenter->>Segmenter: Split paragraphs
Segmenter->>Segmenter: Split sentences
Segmenter-->>parse: Return nested list of sentences
loop For each sentence
parse->>swap_input_text: swap_synonyms(sentence)
swap_input_text->>swap_input_text: Tokenize
swap_input_text->>swap_input_text: Parse tokens
swap_input_text->>swap_input_tokens: swap_input_tokens(tokens)
swap_input_tokens->>Matching: Exact matching
Matching-->>swap_input_tokens: Matched tokens
swap_input_tokens->>Matching: Span matching
Matching-->>swap_input_tokens: Matched spans
swap_input_tokens->>Matching: Hierarchical matching
Matching-->>swap_input_tokens: Matched hierarchy
swap_input_tokens-->>swap_input_text: Swapped tokens
swap_input_text-->>parse: Parser result
parse->>parse: Extract entities with 'swaps'
parse->>parse: Add to entities list
end
parse->>parse: Deduplicate entities
parse-->>User: Return unique entities
Matching Strategy Comparison
graph TD
A[Input Token: 'seattle'] --> B{Exact Match?}
B -->|Yes| C[Match found]
B -->|No| D{Span Match?}
D -->|Yes| E[Multi-word match]
D -->|No| F{Hierarchical Match?}
F -->|Yes| G[Parent/child match]
F -->|No| H[No match]
C --> I[Add to swaps]
E --> I
G --> I
H --> J[Skip token]
I --> K[Return in result]
J --> L[Continue processing]
Recursive Matching Flow
flowchart TD
A[swap_input_tokens: tokens, ctr=0] --> B[Exact + Span + Hierarchical]
B --> C[Get swapped tokens]
C --> D{ctr < 2?}
D -->|Yes| E[Recursive Call: ctr+1]
E --> B
D -->|No| F[Return final tokens]
style E fill:#ffcccc
style D fill:#ffffcc