<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LASI.Algorithm.Binding
{
    class PhraseWiseObjectBinder
    {
        public PhraseWiseObjectBinder(VerbPhrase bindingTarget, IEnumerable<Phrase> remainingPhrases) {
            _bindingTarget = bindingTarget;

            inputstream = new Stack<Phrase>(remainingPhrases);

        }

        protected IEntity directObject;
        protected IEntity indirectObject;
        protected Stack<IEntity> entities;

        protected Stack<Phrase> inputstream;
        protected VerbPhrase _bindingTarget;
        internal abstract class State
        {
            protected State(PhraseWiseObjectBinder machine) {
                _machine = machine;
            }
            private string stateName;

            protected string StateName {
                get {
                    return stateName;
                }
                set {
                    stateName = value;
                }
            }
            private int stateNumber;
            private PhraseWiseObjectBinder _machine;

            public PhraseWiseObjectBinder Machine {
                get {
                    return _machine;
                }
                set {
                    _machine = value;
                }
            }

            protected int StateNumber {
                get {
                    return stateNumber;
                }
                set {
                    stateNumber = value;
                }
            }


            public virtual State ProcessNext(Phrase error) {
                throw new InvalidStateTransitionException(stateNumber, error);
            }
        }
        internal class State0 : State
        {
            protected State0(PhraseWiseObjectBinder machine)
                : base(machine) {
            }
            public virtual State ProcessNext(NounPhrase nounPhrase) {
                Machine.entities.Push(nounPhrase);
                return new State1(Machine);

            }

        }


        internal class State1 : State
        {


            public State1(PhraseWiseObjectBinder Machine)
                : base(Machine) {
            }
        }
    }


    static class DynamicStackExtensions
    {
        internal static dynamic PopDynamic(this Stack<Phrase> stack) {
            return stack.Pop();
        }
    }

}<commit_msg>Did a lot of work on a state machine for binding direct and indirect objects<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LASI.Algorithm.Binding
{
    class PhraseWiseObjectBinder
    {
        public PhraseWiseObjectBinder(VerbPhrase bindingTarget, IEnumerable<Phrase> remainingPhrases) {
            _bindingTarget = bindingTarget;

            inputstream = new Stack<Phrase>(remainingPhrases);

            St0 = new State0(this);
            St1 = new State1(this);
            St2 = new State2(this);
            St4 = new State4(this);
            St5 = new State5(this);
            St6 = new State6(this);

            ConjunctNounPhrases = new List<NounPhrase>();

            St0.ProcessNext(inputstream.PopDynamic());

        }

        protected IEntity directObject;
        protected IEntity indirectObject;
        protected Stack<NounPhrase> entities = new Stack<NounPhrase>();
        protected bool directObjectFound, indirectObjectFound;
        protected Stack<Phrase> inputstream;
        protected VerbPhrase _bindingTarget;
        protected List<AdjectivePhrase> lastAdjectivals = new List<AdjectivePhrase>();
        protected List<NounPhrase> ConjunctNounPhrases;
        protected State0 St0;
        protected State1 St1;
        protected State2 St2;
        protected State4 St4;
        protected State5 St5;
        private ConjunctionPhrase lastConjunctive;
        protected State6 St6;

        public ConjunctionPhrase LastConjunctive {
            get {
                return lastConjunctive;
            }
            set {
                lastConjunctive = value;
            }
        }
        private void BindBuiltupAdjectivePhrases(NounPhrase phrase) {
            foreach (var adjp in this.lastAdjectivals) {
                phrase.BindDescriber(adjp);
            }
            this.lastAdjectivals.Clear();
        }


        internal abstract class State
        {
            protected State(PhraseWiseObjectBinder machine) {
                _machine = machine;
                stream = machine.inputstream;
            }
            private string stateName;

            protected string StateName {
                get {
                    return stateName;
                }
                set {
                    stateName = value;
                }
            }
            private int stateNumber;
            private PhraseWiseObjectBinder _machine;

            public PhraseWiseObjectBinder Machine {
                get {
                    return _machine;
                }
                set {
                    _machine = value;
                }
            }

            protected int StateNumber {
                get {
                    return stateNumber;
                }
                set {
                    stateNumber = value;
                }
            }


            public virtual void ProcessNext(Phrase phrase) {
                throw new InvalidStateTransitionException(stateNumber, phrase);
            }

            private Stack<Phrase> stream;

            protected Stack<Phrase> Stream {
                get {
                    return stream;
                }
                set {
                    stream = value;
                }
            }
        }
        internal class State0 : State
        {
            public State0(PhraseWiseObjectBinder machine)
                : base(machine) {
                St1 = machine.St1;
                St2 = machine.St2;
            }
            public virtual void ProcessNext(NounPhrase phrase) {
                Machine.entities.Push(phrase);
                St2.ProcessNext(Stream.PopDynamic());
            }
            public virtual void ProcessNext(AdjectivePhrase phrase) {
                Machine.lastAdjectivals.Add(phrase);
                St1.ProcessNext(Stream.PopDynamic());
            }
            private State1 St1;
            private State2 St2;
        }


        internal class State1 : State
        {


            public State1(PhraseWiseObjectBinder machine)
                : base(machine) {
                St2 = Machine.St2;

            }
            public void ProcessNext(NounPhrase phrase) {
                Machine.entities.Push(phrase);
                Machine.BindBuiltupAdjectivePhrases(phrase);
                St2.ProcessNext(Stream.PopDynamic());
            }
            private State2 St2;
        }
        internal class State2 : State
        {


            public State2(PhraseWiseObjectBinder machine)
                : base(machine) {
                St4 = Machine.St4;
            }
            public void ProcessNext(ConjunctionPhrase phrase) {
                phrase.OnLeft = Machine.entities.Peek();
                Machine.LastConjunctive = phrase;
                Machine.ConjunctNounPhrases.Add(Machine.entities.Pop());
                St4.ProcessNext(Stream.PopDynamic());
            }
            public void ProcessNext(AdjectivePhrase phrase) {
                IndirectObjectFound();
            }
            public void ProcessNext(NounPhrase phrase) {
                IndirectObjectFound();
            }
            public void ProcessNext(PrepositionalPhrase phrase) {
                DirectObjectFound();
            }

            private void DirectObjectFound() {
                foreach (var E in Machine.entities) {
                    E.DirectObjectOf = Machine._bindingTarget;
                    Machine._bindingTarget.DirectObject = E;
                }
                Machine.entities.Clear();
                Machine.ConjunctNounPhrases.Clear();
                Machine.directObjectFound = true;
            }

            private void IndirectObjectFound() {
                foreach (var E in Machine.entities) {
                    E.IndirectObjectOf = Machine._bindingTarget;
                    Machine._bindingTarget.IndirectObject = E;
                }
                Machine.entities.Clear();
                Machine.ConjunctNounPhrases.Clear();
                Machine.indirectObjectFound = true;
            }
            private State4 St4;
        }
        internal class State4 : State
        {


            public State4(PhraseWiseObjectBinder machine)
                : base(machine) {
                St2 = Machine.St2;
                St5 = Machine.St5;
            }
            public void ProcessNext(NounPhrase phrase) {
                Machine.entities.Push(phrase);
                Machine.ConjunctNounPhrases.Add(phrase);
                Machine.lastConjunctive.OnRight = phrase;
                St2.ProcessNext(Stream.PopDynamic());
            }
            public void ProcessNext(AdjectivePhrase phrase) {
                Machine.lastAdjectivals.Add(phrase);
                St5.ProcessNext(Stream.PopDynamic());
            }
            private State2 St2;
            private State5 St5;
        }
        internal class State5 : State
        {
            public State5(PhraseWiseObjectBinder machine)
                : base(machine) {
                St2 = Machine.St2;
                St5 = Machine.St5;
                St6 = machine.St6;
            }

            public void ProcessNext(NounPhrase phrase) {
                Machine.ConjunctNounPhrases.Add(phrase);
                Machine.entities.Push(phrase);
                Machine.BindBuiltupAdjectivePhrases(phrase);
                St2.ProcessNext(Stream.PopDynamic());
            }
            public void ProcessNext(ConjunctionPhrase phrase) {
                phrase.OnLeft = Machine.lastAdjectivals.Last();
                Machine.LastConjunctive = phrase;
                St6.ProcessNext(Stream.PopDynamic());
            }
            private State2 St2;
            private State5 St5;
            private State6 St6;


        }
        internal class State6 : State
        {


            public State6(PhraseWiseObjectBinder machine)
                : base(machine) {
                St5 = Machine.St5;
                St2 = machine.St2;
            }
            public void ProcessNext(AdjectivePhrase phrase) {
                Machine.lastAdjectivals.Add(phrase);
                Machine.lastConjunctive.OnRight = phrase;
                St5.ProcessNext(Stream.PopDynamic());
            }
            public void ProcessNext(NounPhrase phrase) {
                Machine.entities.Push(phrase);
                Machine.ConjunctNounPhrases.Add(phrase);
                Machine.lastConjunctive.OnRight = phrase;
                Machine.BindBuiltupAdjectivePhrases(phrase);
                St2.ProcessNext(Stream.PopDynamic());
            }

            private State2 St2;
            private State5 St5;

        }
    }


    static class DynamicStackExtensions
    {
        internal static dynamic PopDynamic(this Stack<Phrase> stack) {
            return stack.Pop();
        }
    }

}
{'fraction_non_alphanumeric': 0.06549758877685226, 'fraction_numerical': 0.008417360806663743, 'mean_word_length': 1.373283395755306, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '4426130', 'n_tokens_mistral': 2921, 'n_tokens_neox': 2684, 'n_words': 569}