{
  "Name": "TextFragment",
  "_id": "3fc25e84-6ef9-4619-98b6-47a9e58fce54",
  "Namespace": "lib.text",
  "Description": "Returns a fraction of a string. This can be ever characters, words, lines or sentences. This is useful for implementing things like typewrites and such. ",
  "Inputs": [
    {
      "Name": "InputText",
      "MetaInstanceID": "2b28730e-5abb-4317-a6c7-3ed909c5135b",
      "DefaultValue": {
        "Type": "Text",
        "Value": ""
      },
      "MetaID": "c522a66e-3260-4692-b3e3-79fd0361fa3d",
      "IsMultiInput": "False",
      "Relevance": "Relevant",
      "Description": "",
      "Min": "-100000",
      "Max": "100000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "FragmentStart",
      "MetaInstanceID": "fc483530-079e-416f-8034-16998e32f198",
      "DefaultValue": {
        "Type": "Float",
        "Value": "0"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-10000",
      "Max": "10000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "FragmentCount",
      "MetaInstanceID": "7c76426b-bf51-4984-be9d-45fa90f69272",
      "DefaultValue": {
        "Type": "Float",
        "Value": "1"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-10000",
      "Max": "10000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": []
    },
    {
      "Name": "SplitInto",
      "MetaInstanceID": "576cf884-c891-416e-bbd8-c5008eac0caf",
      "DefaultValue": {
        "Type": "Float",
        "Value": "0"
      },
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506",
      "IsMultiInput": "False",
      "Relevance": "Optional",
      "Description": "",
      "Min": "-10000",
      "Max": "10000",
      "Scale": "0.1",
      "ScaleType": "Linear",
      "EnumValues": [
        {
          "Name": "Characters",
          "Value": "0"
        },
        {
          "Name": "Words",
          "Value": "1"
        },
        {
          "Name": "Lines",
          "Value": "2"
        },
        {
          "Name": "Sentences",
          "Value": "3"
        }
      ]
    }
  ],
  "Outputs": [
    {
      "Name": "Fragment",
      "MetaInstanceID": "5af12a49-cedd-4ebb-aef8-2f8c2cedba89",
      "MetaID": "c522a66e-3260-4692-b3e3-79fd0361fa3d"
    },
    {
      "Name": "Count",
      "MetaInstanceID": "0e52b69d-82a9-43f1-a8e0-751f0a2cc6b1",
      "MetaID": "3f76dee3-3897-44ac-82d6-25ce9f53a506"
    }
  ],
  "OperatorParts": [
    {
      "MetaInstanceID": "a4304a82-86d9-4c4a-b622-84d4b02b6f17",
      "MetaID": "987cf347-5d85-4b83-b971-041b7337c895",
      "Name": "TextFragmentFunc",
      "Version": "44b1b1ef-176e-49f9-9327-8266d823770d",
      "Type": "Dynamic",
      "IsMultiInput": "True",
      "Script": [
        "//>>> _using",
        "using System;",
        "using System.Collections.Generic;",
        "using System.Linq;",
        "using System.Text;",
        "using SharpDX;",
        "using SharpDX.Direct3D11;",
        "using SharpDX.Windows;",
        "//<<< _using",
        "using System.Net;",
        "using System.IO;",
        "",
        "using System.Threading;",
        "using System.Threading.Tasks;",
        "using System.Collections.Specialized;",
        "using System.Text.RegularExpressions;",
        "",
        "namespace Framefield.Core.ID987cf347_5d85_4b83_b971_041b7337c895",
        "{",
        "    public class Class_TextFragment : OperatorPart.Function",
        "    {",
        "        //>>> _inputids",
        "        private enum InputId",
        "        {",
        "            InputText = 0,",
        "            FragmentStart = 1,",
        "            FragmentCount = 2,",
        "            SplitInto = 3",
        "        }",
        "        //<<< _inputids",
        "        ",
        "        //>>> _outputids",
        "        private enum OutputId",
        "        {",
        "            Count = 0,",
        "            Fragment = 1",
        "        }",
        "        //<<< _outputids",
        "        ",
        "        private enum EntityTypes {",
        "            Characters = 0,",
        "            Words,",
        "            Lines,            ",
        "            Sentences,",
        "        }",
        "",
        "        List<List<string>> _tmpList = new List<List<string>>();",
        "        List<String> _resultList = new List<String>();",
        "        String _oldString = \"\";",
        "",
        "        ",
        "        public override OperatorPartContext Eval(OperatorPartContext context, List<OperatorPart> inputs, int outputIdx) {",
        "            //>>> _params",
        "            var InputText = inputs[(int)InputId.InputText].Eval(context).Text;",
        "            var FragmentStart = inputs[(int)InputId.FragmentStart].Eval(context).Value;",
        "            var FragmentCount = inputs[(int)InputId.FragmentCount].Eval(context).Value;",
        "            var SplitInto = (int) inputs[(int)InputId.SplitInto].Eval(context).Value;",
        "            //<<< _params",
        "            ",
        "            var needsUpdate = false;",
        "            ",
        "            if(InputText != _text || (int)SplitInto != (int) _splitInto) {",
        "                ",
        "                _splitInto = (EntityTypes)(int)SplitInto;",
        "                ",
        "                switch(_splitInto) {",
        "                    case EntityTypes.Characters:",
        "                        _chunks = new Regex(\"(.)\").Split(InputText); ",
        "                        _delimiter = \"\";",
        "                        break;",
        "                ",
        "                    case EntityTypes.Words:",
        "                        _chunks = new Regex(\"[\\\\s\\\\.\\\\;\\\\,()`:]+\").Split(InputText); ",
        "                        _delimiter = \" \";",
        "                        break;",
        "                        ",
        "                    case EntityTypes.Lines:",
        "                        _chunks = new Regex(\"\\\\r+\\\\s*\").Split(InputText); ",
        "                        _delimiter = \"\\n\";",
        "                        break;",
        "                        ",
        "                    case EntityTypes.Sentences:",
        "                        _chunks = new Regex(\"\\\\.[\\\\s\\\\.]*\").Split(InputText); ",
        "                        _delimiter = \". \";",
        "                        break;",
        "",
        "                }",
        "                ",
        "                _numberOfChunks =  _chunks.Length > 0 && String.IsNullOrEmpty(_chunks[ _chunks.Length -1]) ",
        "                                ? _chunks.Length -1",
        "                                : _chunks.Length;",
        "                ",
        "                ",
        "                _lastFragment = \"\";",
        "                needsUpdate= true;",
        "            }",
        "            ",
        "            if(needsUpdate ||  (int)FragmentStart != _fragmentStart || (int)FragmentCount != _fragmentCount) {",
        "                _fragmentStart = (int)FragmentStart;",
        "                _fragmentCount = (_splitInto == EntityTypes.Characters) ",
        "                                ? (int)FragmentCount *2",
        "                                : (int)FragmentCount ;",
        "                _lastFragment = GetFragment(_fragmentStart, _fragmentCount);            ",
        "                context.Text = _lastFragment;",
        "            }",
        "            ",
        "            switch((int)outputIdx) {",
        "                case (int)OutputId.Fragment:",
        "                    context.Text = _lastFragment;",
        "                    Logger.Info(\"Text: \" + _lastFragment);",
        "                    ",
        "                    break;",
        "",
        "                case (int)OutputId.Count:",
        "                    context.Value= _chunks.Length;",
        "                    break;",
        "                    ",
        "                default:",
        "                    break;",
        "            }",
        "            ",
        "            return context;",
        "        }",
        "        ",
        "        private string GetFragment(int startFragment, int fragmentCount)",
        "        {",
        "            var d = \"\";",
        "            if(fragmentCount <= 0)",
        "                return \"\";",
        "",
        "            var sb = new StringBuilder();",
        "            for(var index = 0; index < fragmentCount; index++) ",
        "            {",
        "                var moduloIndex = (startFragment + index) % _numberOfChunks;",
        "                if(moduloIndex < 0)",
        "                    moduloIndex += _numberOfChunks;",
        "                    ",
        "                sb.Append(d);",
        "                sb.Append(_chunks[moduloIndex]);",
        "                d = _delimiter;",
        "                ",
        "            }",
        "            return sb.ToString();",
        "            ",
        "",
        "        }",
        "",
        "        private int _fragmentStart = 0;",
        "        private int _fragmentCount = 0;",
        "        private string _lastFragment;",
        "        private string _delimiter;",
        "        private string[] _chunks;",
        "        private int _numberOfChunks = 1;    // without empty tail",
        "        ",
        "        private EntityTypes  _splitInto; ",
        "        private String _text;",
        "    }    ",
        "}",
        "",
        ""
      ],
      "AdditionalAssemblies": []
    }
  ],
  "Operators": [],
  "Connections": [
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "2b28730e-5abb-4317-a6c7-3ed909c5135b",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "a4304a82-86d9-4c4a-b622-84d4b02b6f17"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "fc483530-079e-416f-8034-16998e32f198",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "a4304a82-86d9-4c4a-b622-84d4b02b6f17"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "7c76426b-bf51-4984-be9d-45fa90f69272",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "a4304a82-86d9-4c4a-b622-84d4b02b6f17"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "576cf884-c891-416e-bbd8-c5008eac0caf",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "a4304a82-86d9-4c4a-b622-84d4b02b6f17"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "a4304a82-86d9-4c4a-b622-84d4b02b6f17",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "0e52b69d-82a9-43f1-a8e0-751f0a2cc6b1"
    },
    {
      "SourceOp": "00000000-0000-0000-0000-000000000000",
      "SourceOpPart": "a4304a82-86d9-4c4a-b622-84d4b02b6f17",
      "TargetOp": "00000000-0000-0000-0000-000000000000",
      "TargetOpPart": "5af12a49-cedd-4ebb-aef8-2f8c2cedba89"
    }
  ]
}