<commit_before>package parse

import (
	"fmt"
	"nli-go/lib/mentalese"
)

type SenseBuilder struct {
	varIndexCounter map[string]int
	constantCounter int
}

func NewSenseBuilder() SenseBuilder {
	return SenseBuilder{varIndexCounter: map[string]int{}, constantCounter: 1}
}

// Returns a new variable name
func (builder SenseBuilder) GetNewVariable(formalVariable string) string {

	initial := formalVariable[0:1]

	_, present := builder.varIndexCounter[initial]
	if !present {
		builder.varIndexCounter[initial] = 5
	} else {
		builder.varIndexCounter[initial]++
	}

	return fmt.Sprint(initial, builder.varIndexCounter[initial])
}

// Creates a map of formal variables to actual variables (new variables are created)
func (builder SenseBuilder) CreateVariableMap(actualAntecedents []string, antecedentVariables []string, allConsequentVariables [][]string) map[string]mentalese.Term {

	m := map[string]mentalese.Term{}

	if len(actualAntecedents) == 0 {
		return m
	}

	for i, antecedentVariable := range antecedentVariables {
		m[antecedentVariable] = mentalese.NewVariable(actualAntecedents[i])
	}

	for _, consequentVariables := range allConsequentVariables {
		for _, consequentVariable := range consequentVariables {

			_, present := m[consequentVariable]
			if !present {
				m[consequentVariable] = mentalese.NewVariable(builder.GetNewVariable(consequentVariable))
			}
		}
	}

	return m
}

func (builder SenseBuilder) ExtendVariableMap(sense mentalese.RelationSet, variableMap map[string]mentalese.Term) map[string]mentalese.Term {

	for _, relation := range sense {
		for _, argument := range relation.Arguments {
			if argument.IsVariable() {
				variable := argument.TermValue
				_, present := variableMap[variable]
				if !present {
					variableMap[variable] = mentalese.NewVariable(builder.GetNewVariable(variable))
				}
			}
		}
	}

	return variableMap
}

// Create actual relations given a set of templates and a variable map (formal to actual variables)
func (builder SenseBuilder) CreateGrammarRuleRelations(relationTemplates mentalese.RelationSet, variableMap map[string]mentalese.Term) mentalese.RelationSet {

	relations := mentalese.RelationSet{}

	for _, relation := range relationTemplates {

		newRelation := relation.Copy()

		for a, argument := range newRelation.Arguments {

			if argument.TermType == mentalese.TermVariable {

				newRelation.Arguments[a].TermType = variableMap[argument.TermValue].TermType
				newRelation.Arguments[a].TermValue = variableMap[argument.TermValue].TermValue

			} else if argument.TermType == mentalese.TermRelationSet {

				newRelation.Arguments[a].TermType = mentalese.TermRelationSet
				newRelation.Arguments[a].TermValueRelationSet = builder.CreateGrammarRuleRelations(argument.TermValueRelationSet, variableMap)
			}<commit_msg>sensebuilder: relation sets<commit_after>package parse

import (
	"fmt"
	"nli-go/lib/mentalese"
)

type SenseBuilder struct {
	varIndexCounter map[string]int
	constantCounter int
}

func NewSenseBuilder() SenseBuilder {
	return SenseBuilder{varIndexCounter: map[string]int{}, constantCounter: 1}
}

// Returns a new variable name
func (builder SenseBuilder) GetNewVariable(formalVariable string) string {

	initial := formalVariable[0:1]

	_, present := builder.varIndexCounter[initial]
	if !present {
		builder.varIndexCounter[initial] = 5
	} else {
		builder.varIndexCounter[initial]++
	}

	return fmt.Sprint(initial, builder.varIndexCounter[initial])
}

// Creates a map of formal variables to actual variables (new variables are created)
func (builder SenseBuilder) CreateVariableMap(actualAntecedents []string, antecedentVariables []string, allConsequentVariables [][]string) map[string]mentalese.Term {

	m := map[string]mentalese.Term{}

	if len(actualAntecedents) == 0 {
		return m
	}

	for i, antecedentVariable := range antecedentVariables {
		m[antecedentVariable] = mentalese.NewVariable(actualAntecedents[i])
	}

	for _, consequentVariables := range allConsequentVariables {
		for _, consequentVariable := range consequentVariables {

			_, present := m[consequentVariable]
			if !present {
				m[consequentVariable] = mentalese.NewVariable(builder.GetNewVariable(consequentVariable))
			}
		}
	}

	return m
}

func (builder SenseBuilder) ExtendVariableMap(sense mentalese.RelationSet, variableMap map[string]mentalese.Term) map[string]mentalese.Term {

	for _, relation := range sense {
		for _, argument := range relation.Arguments {
			if argument.IsVariable() {
				variable := argument.TermValue
				_, present := variableMap[variable]
				if !present {
					variableMap[variable] = mentalese.NewVariable(builder.GetNewVariable(variable))
				}
			} else if argument.IsRelationSet() {
				childMap := builder.ExtendVariableMap(argument.TermValueRelationSet, variableMap)
				for key, value := range childMap {
					variableMap[key] = value
				}
			}
		}
	}

	return variableMap
}

// Create actual relations given a set of templates and a variable map (formal to actual variables)
func (builder SenseBuilder) CreateGrammarRuleRelations(relationTemplates mentalese.RelationSet, variableMap map[string]mentalese.Term) mentalese.RelationSet {

	relations := mentalese.RelationSet{}

	for _, relation := range relationTemplates {

		newRelation := relation.Copy()

		for a, argument := range newRelation.Arguments {

			if argument.TermType == mentalese.TermVariable {

				newRelation.Arguments[a].TermType = variableMap[argument.TermValue].TermType
				newRelation.Arguments[a].TermValue = variableMap[argument.TermValue].TermValue

			} else if argument.TermType == mentalese.TermRelationSet {

				newRelation.Arguments[a].TermType = mentalese.TermRelationSet
				newRelation.Arguments[a].TermValueRelationSet = builder.CreateGrammarRuleRelations(argument.TermValueRelationSet, variableMap)
			}
{'fraction_non_alphanumeric': 0.09822810940994323, 'fraction_numerical': 0.001720282126268708, 'mean_word_length': 5.251612903225807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2455364', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1682, 'n_words': 371}