LLM for Unity  v2.4.2
Create characters in Unity with LLMs!
Loading...
Searching...
No Matches
WordSplitter.cs
Go to the documentation of this file.
1
3using System;
4using System.Collections.Generic;
5using System.Threading.Tasks;
6using UnityEngine;
7
8namespace LLMUnity
9{
14 [Serializable]
15 public class WordSplitter : Chunking
16 {
18 [Tooltip("number of words by which to split phrases into chunks")]
19 public int numWords = 10;
20
26 public override async Task<List<(int, int)>> Split(string input)
27 {
28 bool IsBoundary(char c)
29 {
30 return Char.IsPunctuation(c) || Char.IsWhiteSpace(c);
31 }
32
33 List<(int, int)> indices = new List<(int, int)>();
34 await Task.Run(() => {
35 List<(int, int)> wordIndices = new List<(int, int)>();
36 int startIndex = 0;
37 int endIndex;
38 for (int i = 0; i < input.Length; i++)
39 {
40 if (i == input.Length - 1 || IsBoundary(input[i]))
41 {
42 while (i < input.Length - 1 && IsBoundary(input[i + 1])) i++;
43 endIndex = i;
44 wordIndices.Add((startIndex, endIndex));
45 startIndex = i + 1;
46 }
47 }
48
49 for (int i = 0; i < wordIndices.Count; i += numWords)
50 {
51 int iTo = Math.Min(wordIndices.Count - 1, i + numWords - 1);
52 indices.Add((wordIndices[i].Item1, wordIndices[iTo].Item2));
53 }
54 });
55 return indices;
56 }
57 }
58}
Class implementing the chunking functionality.
Definition Chunking.cs:18
Class implementing a word-based splitter.
override async Task< List<(int, int)> > Split(string input)
Splits the provided phrase into chunks of a specific number of words (defined by the numWords variabl...
int numWords
number of words by which to split phrases into chunks