<commit_before>import * as _ from 'lodash';
import {Statement} from '../model/Statement.js';
import {Argument} from '../model/Argument.js';
import {Relation} from '../model/Relation.js';
import {Section} from '../model/Section.js';
import {EquivalenceClass} from '../model/EquivalenceClass.js';
import {tokenMatcher} from 'chevrotain';
import {ArgdownLexer} from './../ArgdownLexer.js';

const RelationObjectTypes = Object.freeze({STATEMENT: Symbol("STATEMENT"), RECONSTRUCTED_ARGUMENT: Symbol("RECONSTRUCTED ARGUMENT"), SKETCHED_ARGUMENT: Symbol("SKETCHED ARGUMENT")});

class ModelPlugin{
  set config(config){
    let previousSettings = this.settings;
    if(!previousSettings){
      previousSettings = {
        removeTagsFromText: false
      }
    }
    this.settings = _.defaultsDeep({}, config, previousSettings);
  }
  run(data){
    if(data.config && data.config.model){
      this.config = data.config.model;
    }
    for(let relation of this.relations){
      let fromType = this.getElementType(relation.from);
      let toType = this.getElementType(relation.to);
      if(fromType == RelationObjectTypes.SKETCHED_ARGUMENT 
        ||toType == RelationObjectTypes.RECONSTRUCTED_ARGUMENT 
        ||toType == RelationObjectTypes.SKETCHED_ARGUMENT){
        relation.status = "sketched";
      }else if(fromType == RelationObjectTypes.STATEMENT 
        ||fromType == RelationObjectTypes.RECONSTRUCTED_ARGUMENT){
        relation.status = "reconstructed";
        
        if(fromType == RelationObjectTypes.RECONSTRUCTED_ARGUMENT){
          //change relation.from to point to the argument's conclusion
          let argument = relation.from;
          
          //remove from argument
          let index = _.indexOf(argument.relations, relation);
          argument.relations.splice(index, 1);
                    
          let conclusionStatement = argument.pcs[relation.from.pcs.length - 1];
          let equivalenceClass = this.statements[conclusionStatement.title];
          
          relation.from = equivalenceClass;

          //check if this relation already exists
          let relationExists = false;
          for(let existingRelation of relation.from.relations){
            if(relation.to == existingRelation.to && relation.type == existingRelation.type){
              relationExists = true;
              break;
            }
          }
          if(!relationExists){
            equivalenceClass.relations.push(relation);            
          }else{
            //remove relation from target
            let index = _.indexOf(relation.to.relations, relation);
            relation.to.relations.splice(index, 1);
            //remove relation from relations
            index = _.indexOf(this.relations, relation);
            this.relations.splice(index, 1);
          }
        }
        
        //Change dialectical types of statement-to-statement relations to semantic types
        if(relation.type == "support"){
          relation.type = "entails";
        }else if(relation.type == "attack"){
          relation.type = "contrary"
        }
        
      }
    }


    data.relations = this.relations;
    data.statements = this.statements;
    data.arguments = this.arguments;
    data.sections = this.sections;
    data.tags = this.tags;
    return data;
  }
  getElementType(obj){
    if(obj instanceof Argument){
      if(obj.pcs && obj.pcs.length > 0){
        return RelationObjectTypes.RECONSTRUCTED_ARGUMENT;
      }else{
        return RelationObjectTypes.SKETCHED_ARGUMENT;
      }
    }else if(obj instanceof EquivalenceClass){
        return RelationObjectTypes.STATEMENT;
    }
    return null;
  }
  constructor(config){
    this.name = "ModelPlugin";
    this.config = config;
    
    let $ = this;

    const statementReferencePattern = /\[(.+)\]/;
    const statementDefinitionPattern = /\[(.+)\]\:/;
    const statementMentionPattern = /\@\[(.+)\](\s?)/;
    const argumentReferencePattern = /\<(.+)\>/;
    const argumentDefinitionPattern = /\<(.+)\>\:/;
    const argumentMentionPattern = /\@\<(.+)\>(\s?)/;
    const linkPattern = /\[(.+)\]\((.+)\)/;
    const tagPattern = /#(?:\(([^\)]+)\)|([a-zA-z0-9-\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+))/;

    let uniqueTitleCounter = 0;
    function getUniqueTitle(){
      uniqueTitleCounter++;
      return "Untitled "+uniqueTitleCounter;
    }
    function getEquivalenceClass(title){
      if(!title)
        return null;

      let ec = $.statements[title];
      if(!ec){
        ec = new EquivalenceClass();
        ec.title = title;
        $.statements[title] = ec;
      }
      return ec;
    }


    let currentStatement = null;
    let currentStatementOrArgument = null;
    let currentArgument = null;
    let currentArgumentReconstruction = null;
    let currentInference = null;
    let rangesStack = [];
    let parentsStack = [];
    let currentRelation = null;
    let inStatementTree = false;
    let currentSection = null;
    let sectionCounter = 0;

    function onArgdownEntry(node, parentNode, childIndex, data){
      $.statements = {};
      $.arguments = {};
      $.sections = [];
      $.relations = [];
      $.tags = [];
      uniqueTitleCounter = 0;
      currentSection = null;
      currentStatementOrArgument = null;
      currentArgumentReconstruction = null;
      currentInference = null;
      currentArgument = null;
      rangesStack = [];
      parentsStack = [];
      currentRelation = null;
      inStatementTree = false;
      sectionCounter = 0;<commit_msg>fixed: ModelPlugin config data was not updated in OnArgdownEntry<commit_after>import * as _ from 'lodash';
import {Statement} from '../model/Statement.js';
import {Argument} from '../model/Argument.js';
import {Relation} from '../model/Relation.js';
import {Section} from '../model/Section.js';
import {EquivalenceClass} from '../model/EquivalenceClass.js';
import {tokenMatcher} from 'chevrotain';
import {ArgdownLexer} from './../ArgdownLexer.js';

const RelationObjectTypes = Object.freeze({STATEMENT: Symbol("STATEMENT"), RECONSTRUCTED_ARGUMENT: Symbol("RECONSTRUCTED ARGUMENT"), SKETCHED_ARGUMENT: Symbol("SKETCHED ARGUMENT")});

class ModelPlugin{
  set config(config){
    let previousSettings = this.settings;
    if(!previousSettings){
      previousSettings = {
        removeTagsFromText: false
      }
    }
    this.settings = _.defaultsDeep({}, config, previousSettings);
  }
  run(data){
    if(data.config && data.config.model){
      this.config = data.config.model;
    }
    for(let relation of this.relations){
      let fromType = this.getElementType(relation.from);
      let toType = this.getElementType(relation.to);
      if(fromType == RelationObjectTypes.SKETCHED_ARGUMENT 
        ||toType == RelationObjectTypes.RECONSTRUCTED_ARGUMENT 
        ||toType == RelationObjectTypes.SKETCHED_ARGUMENT){
        relation.status = "sketched";
      }else if(fromType == RelationObjectTypes.STATEMENT 
        ||fromType == RelationObjectTypes.RECONSTRUCTED_ARGUMENT){
        relation.status = "reconstructed";
        
        if(fromType == RelationObjectTypes.RECONSTRUCTED_ARGUMENT){
          //change relation.from to point to the argument's conclusion
          let argument = relation.from;
          
          //remove from argument
          let index = _.indexOf(argument.relations, relation);
          argument.relations.splice(index, 1);
                    
          let conclusionStatement = argument.pcs[relation.from.pcs.length - 1];
          let equivalenceClass = this.statements[conclusionStatement.title];
          
          relation.from = equivalenceClass;

          //check if this relation already exists
          let relationExists = false;
          for(let existingRelation of relation.from.relations){
            if(relation.to == existingRelation.to && relation.type == existingRelation.type){
              relationExists = true;
              break;
            }
          }
          if(!relationExists){
            equivalenceClass.relations.push(relation);            
          }else{
            //remove relation from target
            let index = _.indexOf(relation.to.relations, relation);
            relation.to.relations.splice(index, 1);
            //remove relation from relations
            index = _.indexOf(this.relations, relation);
            this.relations.splice(index, 1);
          }
        }
        
        //Change dialectical types of statement-to-statement relations to semantic types
        if(relation.type == "support"){
          relation.type = "entails";
        }else if(relation.type == "attack"){
          relation.type = "contrary"
        }
        
      }
    }


    data.relations = this.relations;
    data.statements = this.statements;
    data.arguments = this.arguments;
    data.sections = this.sections;
    data.tags = this.tags;
    return data;
  }
  getElementType(obj){
    if(obj instanceof Argument){
      if(obj.pcs && obj.pcs.length > 0){
        return RelationObjectTypes.RECONSTRUCTED_ARGUMENT;
      }else{
        return RelationObjectTypes.SKETCHED_ARGUMENT;
      }
    }else if(obj instanceof EquivalenceClass){
        return RelationObjectTypes.STATEMENT;
    }
    return null;
  }
  constructor(config){
    this.name = "ModelPlugin";
    this.config = config;
    
    let $ = this;

    const statementReferencePattern = /\[(.+)\]/;
    const statementDefinitionPattern = /\[(.+)\]\:/;
    const statementMentionPattern = /\@\[(.+)\](\s?)/;
    const argumentReferencePattern = /\<(.+)\>/;
    const argumentDefinitionPattern = /\<(.+)\>\:/;
    const argumentMentionPattern = /\@\<(.+)\>(\s?)/;
    const linkPattern = /\[(.+)\]\((.+)\)/;
    const tagPattern = /#(?:\(([^\)]+)\)|([a-zA-z0-9-\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+))/;

    let uniqueTitleCounter = 0;
    function getUniqueTitle(){
      uniqueTitleCounter++;
      return "Untitled "+uniqueTitleCounter;
    }
    function getEquivalenceClass(title){
      if(!title)
        return null;

      let ec = $.statements[title];
      if(!ec){
        ec = new EquivalenceClass();
        ec.title = title;
        $.statements[title] = ec;
      }
      return ec;
    }


    let currentStatement = null;
    let currentStatementOrArgument = null;
    let currentArgument = null;
    let currentArgumentReconstruction = null;
    let currentInference = null;
    let rangesStack = [];
    let parentsStack = [];
    let currentRelation = null;
    let inStatementTree = false;
    let currentSection = null;
    let sectionCounter = 0;

    function onArgdownEntry(node, parentNode, childIndex, data){
      if(data.config && data.config.model){
        this.config = data.config.model;
      }
      $.statements = {};
      $.arguments = {};
      $.sections = [];
      $.relations = [];
      $.tags = [];
      uniqueTitleCounter = 0;
      currentSection = null;
      currentStatementOrArgument = null;
      currentArgumentReconstruction = null;
      currentInference = null;
      currentArgument = null;
      rangesStack = [];
      parentsStack = [];
      currentRelation = null;
      inStatementTree = false;
      sectionCounter = 0;
{'fraction_non_alphanumeric': 0.12562589413447783, 'fraction_numerical': 0.003397711015736767, 'mean_word_length': 2.4607054455445545, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '3798757', 'n_tokens_mistral': 3237, 'n_tokens_neox': 3091, 'n_words': 659}