Overview
TheNL2FOL class is the primary interface for translating natural language sentences into first-order logic (FOL) expressions. It orchestrates the entire translation pipeline, from claim extraction to logical formula generation.
Class Definition
Parameters
str
required
The natural language sentence to convert to first-order logic
str
required
Type of language model to use. Supported values:
'llama' or 'gpt'transformers.Pipeline
HuggingFace text generation pipeline (required for LLaMA models,
None for GPT)transformers.Tokenizer
Tokenizer for the generation model (required for LLaMA models,
None for GPT)transformers.PreTrainedModel
required
Natural Language Inference model for entity relationship detection
transformers.Tokenizer
required
Tokenizer for the NLI model
bool
default:"False"
Enable debug output to print intermediate results
bool
default:"False"
Use direct FOL conversion (experimental)
Instance Attributes
After initialization and conversion, the following attributes are populated:str
Extracted claim from the input sentence
str
Extracted implication from the input sentence
str
Referring expressions identified in the claim
str
Referring expressions identified in the implication
list
List of tuples containing entities that are semantically equal
list
List of tuples where first entity is a subset of the second
dict
Mapping from entity names to logical variables (a, b, c, …)
str
Properties extracted from the claim
str
Properties extracted from the implication
list
List of tuples representing property-level implications
str
Logical form of the claim
str
Logical form of the implication
str
Final first-order logic formula (claim → implication)
str
Alternative final formula with different quantifier handling
Methods
convert_to_first_order_logic
tuple[str, str] - A tuple containing (final_lf, final_lf2)
Pipeline Steps:
- Extract claim and implication
- Get referring expressions
- Determine entity relations
- Create entity mappings
- Extract properties
- Generate logical forms
- Apply heuristics
- Generate final formulas
extract_claim_and_implication
self.claim and self.implication attributes
get_referring_expressions
self.claim_ref_exp and self.implication_ref_exp
get_entity_relations
self.equal_entities and self.subset_entities
get_entity_mapping
self.entity_mappings dictionary
get_properties
self.claim_properties and self.implication_properties
get_properties_relations
self.property_implications
check_entailment
clause1(str): First clauseclause2(str): Second clause
bool - True if entailment exists, False otherwise
get_fol
self.claim_lf and self.implication_lf
get_direct_fol
apply_heuristics
- Replaces
->with& - Normalizes
&toand - Normalizes
|toor
get_final_lf
self.final_lf
get_final_lf2
self.final_lf2
get_llm_result
prompt(str): Input prompt for the LLMmodel_type(str, optional): Override default model type
str - LLM response text
get_nli_prob
premise(str): Premise texthypothesis(str): Hypothesis text
float - Probability of entailment (0-100)
get_nli_prob_list
premise(str): Premise texthypothesis_list(list[str]): List of hypothesis texts
list[float] - List of entailment probabilities
Utility Functions
setup_dataset
fallacy_set(str): Dataset type -'logic','logicclimate','nli', or'folio'length(int): Number of samples to load
pandas.DataFrame - Dataset with articles and label columns
The
setup_dataset function is typically used in evaluation scripts to prepare test data for batch processing.Example Usage
See Also
- CVCGenerator - Generate SMT solver input from FOL formulas
- SMTResults - Interpret solver results
- Helper Functions - Utility functions for string processing
