LLM for Unity  v2.3.0
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;
6
7namespace LLMUnity
8{
13 [Serializable]
14 public class WordSplitter : Chunking
15 {
17 public int numWords = 10;
18
24 public override async Task<List<(int, int)>> Split(string input)
25 {
26 bool IsBoundary(char c)
27 {
28 return Char.IsPunctuation(c) || Char.IsWhiteSpace(c);
29 }
30
31 List<(int, int)> indices = new List<(int, int)>();
32 await Task.Run(() => {
33 List<(int, int)> wordIndices = new List<(int, int)>();
34 int startIndex = 0;
35 int endIndex;
36 for (int i = 0; i < input.Length; i++)
37 {
38 if (i == input.Length - 1 || IsBoundary(input[i]))
39 {
40 while (i < input.Length - 1 && IsBoundary(input[i + 1])) i++;
41 endIndex = i;
42 wordIndices.Add((startIndex, endIndex));
43 startIndex = i + 1;
44 }
45 }
46
47 for (int i = 0; i < wordIndices.Count; i += numWords)
48 {
49 int iTo = Math.Min(wordIndices.Count - 1, i + numWords - 1);
50 indices.Add((wordIndices[i].Item1, wordIndices[iTo].Item2));
51 }
52 });
53 return indices;
54 }
55 }
56}
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
the number of words to split phrases into chunks