<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Twee2Z.ObjectTree;
using Twee2Z.Utils;
using Twee2Z.ObjectTree.PassageContents;
using Twee2Z.ObjectTree.PassageContents.Macro;
using Twee2Z.ObjectTree.PassageContents.Macro.Branch;

namespace Twee2Z.Analyzer
{
    class TweeBuilder
    {
        private Passage _currentPassage;
        private PassageContent _lastPassageContent;
        private List<PassageContent> _passageContentMacroStack = new List<PassageContent>();
        private PassageContentFormat _currentFormat;

        private Tree _tree;

        public TweeBuilder()
        {
            _tree = new Tree();
        }

        public Tree Tree
        {
            get { return _tree; }
        }

        public PassageContentFormat CurrentFormat
        {
            get
            {
                if (_currentFormat == null)
                {
                    _currentFormat = new PassageContentFormat();
                }
                return _currentFormat;
            }
        }

        private int MacroStackCount
        {
            get { return _passageContentMacroStack.Count; }
        }

        public void AddTag(string tag)
        {
            _currentPassage.AddTag(tag);
        }

        public void AddPassage(Passage passage)
        {
            if (_passageContentMacroStack.Count != 0)
            {
                Logger.LogWarning("A macro isn´t closed before passage " + passage.Name + " begins");
            }
            _passageContentMacroStack.Clear();
            _lastPassageContent = null;
            _currentPassage = passage;
            _currentFormat = new PassageContentFormat();
            _tree.AddPassage(passage);
        }

        public void AddPassageContent(PassageContent passageContent)
        {
            // set format
            passageContent.ContentFormat = _currentFormat;
            _currentFormat = _currentFormat.Copy();

            PassageContent lastContent = _lastPassageContent;

            // merge text content together
            if (lastContent != null &&
                lastContent.Type == PassageContent.ContentType.TextContent &&
                passageContent.Type == PassageContent.ContentType.TextContent &&
                CurrentFormat.euquals(lastContent.ContentFormat))
            {
                lastContent.PassageText.MergePassageText(passageContent.PassageText);
            }
            else if (MacroStackCount == 0)
            {
                _currentPassage.AddPassageContent(passageContent);
            }
            else if (lastContent.Type == PassageContent.ContentType.MacroContent &&
                    lastContent.PassageMacro.MacroType == PassageMacro.PassageMarcroType.BranchMacro)
            {
                PassageMacro macro = _passageContentMacroStack.Last().PassageMacro;
                PassageMacroBranch branch = macro.PassageMacroBranch;

                branch.AddBranch(passageContent.PassageBranch);

                _passageContentMacroStack.Add(passageContent);
            }
            // stack: macro branch if - input: else ifelse
            else if (_passageContentMacroStack[_passageContentMacroStack.Count - 2].Type == PassageContent.ContentType.MacroContent &&
                    _passageContentMacroStack[_passageContentMacroStack.Count - 2].PassageMacro.MacroType == PassageMacro.PassageMarcroType.BranchMacro &&
                    _passageContentMacroStack.Last().Type == PassageContent.ContentType.BranchContent &&
                    _passageContentMacroStack.Last().PassageBranch.BranchType != PassageMacroBranchNode.MacroBranchType.Else &&
                    passageContent.Type == PassageContent.ContentType.BranchContent &&
                    passageContent.PassageBranch.BranchType != PassageMacroBranchNode.MacroBranchType.If)
            {
                FinishBranchStatement();
                PassageMacroBranch branch = _passageContentMacroStack.Last().PassageMacro.PassageMacroBranch;

                branch.AddBranch(passageContent.PassageBranch);

                _passageContentMacroStack.Add(passageContent);

            }
            else if (_passageContentMacroStack.Last().Type == PassageContent.ContentType.BranchContent)
            {
                PassageMacroBranchNode branchNode = _passageContentMacroStack.Last().PassageBranch;
                branchNode.AddPassageContent(passageContent);
            }
            else
            {
                throw new Exception("unknown case");
            }

            // put on stack if macro branch
            if (passageContent.Type == PassageContent.ContentType.MacroContent &&
                passageContent.PassageMacro.MacroType == PassageMacro.PassageMarcroType.BranchMacro)
            {
                _passageContentMacroStack.Add(passageContent);
            }

            _lastPassageContent = passageContent;
        }

        private void FinishBranchStatement()
        {
            _passageContentMacroStack.RemoveAt(_passageContentMacroStack.Count - 1);

            if (_passageContentMacroStack.Count == 0)
            {
                _lastPassageContent = _currentPassage.PassageContentList.Last();
                _currentFormat = _lastPassageContent.ContentFormat.Copy();
            }
            else
            {
                _lastPassageContent = _passageContentMacroStack.Last();
                _currentFormat = _lastPassageContent.ContentFormat.Copy();
            }
        }

        /// <summary>
        /// Call this method, if a endIf statement occurs
        /// </summary>
        public void FinishBranch()
        {
            // stack: branch (if/else) // pop both
            FinishBranchStatement();
            FinishBranchStatement();
        }
    }
}
<commit_msg>fix text bug: text was wrong merged<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Twee2Z.ObjectTree;
using Twee2Z.Utils;
using Twee2Z.ObjectTree.PassageContents;
using Twee2Z.ObjectTree.PassageContents.Macro;
using Twee2Z.ObjectTree.PassageContents.Macro.Branch;

namespace Twee2Z.Analyzer
{
    class TweeBuilder
    {
        private Passage _currentPassage;
        private PassageContent _lastPassageContent;
        private List<PassageContent> _passageContentMacroStack = new List<PassageContent>();
        private PassageContentFormat _currentFormat;

        private Tree _tree;

        public TweeBuilder()
        {
            _tree = new Tree();
        }

        public Tree Tree
        {
            get { return _tree; }
        }

        public PassageContentFormat CurrentFormat
        {
            get
            {
                if (_currentFormat == null)
                {
                    _currentFormat = new PassageContentFormat();
                }
                return _currentFormat;
            }
        }

        private int MacroStackCount
        {
            get { return _passageContentMacroStack.Count; }
        }

        public void AddTag(string tag)
        {
            _currentPassage.AddTag(tag);
        }

        public void AddPassage(Passage passage)
        {
            if (_passageContentMacroStack.Count != 0)
            {
                Logger.LogWarning("A macro isn´t closed before passage " + passage.Name + " begins");
            }
            _passageContentMacroStack.Clear();
            _lastPassageContent = null;
            _currentPassage = passage;
            _currentFormat = new PassageContentFormat();
            _tree.AddPassage(passage);
        }

        public void AddPassageContent(PassageContent passageContent)
        {
            // set format
            passageContent.ContentFormat = _currentFormat;
            _currentFormat = _currentFormat.Copy();

            PassageContent lastContent = _lastPassageContent;

            // merge text content together
            if (lastContent != null &&
                lastContent.Type == PassageContent.ContentType.TextContent &&
                passageContent.Type == PassageContent.ContentType.TextContent &&
                CurrentFormat.euquals(lastContent.ContentFormat))
            {
                lastContent.PassageText.MergePassageText(passageContent.PassageText);
                passageContent = lastContent;
            }
            else if (MacroStackCount == 0)
            {
                _currentPassage.AddPassageContent(passageContent);
            }
            else if (lastContent.Type == PassageContent.ContentType.MacroContent &&
                    lastContent.PassageMacro.MacroType == PassageMacro.PassageMarcroType.BranchMacro)
            {
                PassageMacro macro = _passageContentMacroStack.Last().PassageMacro;
                PassageMacroBranch branch = macro.PassageMacroBranch;

                branch.AddBranch(passageContent.PassageBranch);

                _passageContentMacroStack.Add(passageContent);
            }
            // stack: macro branch if - input: else ifelse
            else if (_passageContentMacroStack[_passageContentMacroStack.Count - 2].Type == PassageContent.ContentType.MacroContent &&
                    _passageContentMacroStack[_passageContentMacroStack.Count - 2].PassageMacro.MacroType == PassageMacro.PassageMarcroType.BranchMacro &&
                    _passageContentMacroStack.Last().Type == PassageContent.ContentType.BranchContent &&
                    _passageContentMacroStack.Last().PassageBranch.BranchType != PassageMacroBranchNode.MacroBranchType.Else &&
                    passageContent.Type == PassageContent.ContentType.BranchContent &&
                    passageContent.PassageBranch.BranchType != PassageMacroBranchNode.MacroBranchType.If)
            {
                FinishBranchStatement();
                PassageMacroBranch branch = _passageContentMacroStack.Last().PassageMacro.PassageMacroBranch;

                branch.AddBranch(passageContent.PassageBranch);

                _passageContentMacroStack.Add(passageContent);

            }
            else if (_passageContentMacroStack.Last().Type == PassageContent.ContentType.BranchContent)
            {
                PassageMacroBranchNode branchNode = _passageContentMacroStack.Last().PassageBranch;
                branchNode.AddPassageContent(passageContent);
            }
            else
            {
                throw new Exception("unknown case");
            }

            // put on stack if macro branch
            if (passageContent.Type == PassageContent.ContentType.MacroContent &&
                passageContent.PassageMacro.MacroType == PassageMacro.PassageMarcroType.BranchMacro)
            {
                _passageContentMacroStack.Add(passageContent);
            }

            _lastPassageContent = passageContent;
        }

        private void FinishBranchStatement()
        {
            _passageContentMacroStack.RemoveAt(_passageContentMacroStack.Count - 1);

            if (_passageContentMacroStack.Count == 0)
            {
                _lastPassageContent = _currentPassage.PassageContentList.Last();
                _currentFormat = _lastPassageContent.ContentFormat.Copy();
            }
            else
            {
                _lastPassageContent = _passageContentMacroStack.Last();
                _currentFormat = _lastPassageContent.ContentFormat.Copy();
            }
        }

        /// <summary>
        /// Call this method, if a endIf statement occurs
        /// </summary>
        public void FinishBranch()
        {
            // stack: branch (if/else) // pop both
            FinishBranchStatement();
            FinishBranchStatement();
        }
    }
}

{'fraction_non_alphanumeric': 0.0743529013703265, 'fraction_numerical': 0.002030113347995263, 'mean_word_length': 1.910635155096012, 'pattern_counts': {'":': 0, '<': 11, '<?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': '4866980', 'n_tokens_mistral': 3063, 'n_tokens_neox': 3002, 'n_words': 482}