-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentationClasses.cs
More file actions
197 lines (117 loc) · 6.75 KB
/
Copy pathSegmentationClasses.cs
File metadata and controls
197 lines (117 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;
namespace NarrativeArc
{
internal class TextSegmenter
{
//private static string[] stopList { get; } = new string[] { "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(",
// ")", "_", "+", "-", "–", "=", "[", "]", "\\", ";",
// "'", ",", ".", "/", "{", "}", "|", ":", "\"", "<",
// ">", "?", "..", "...", "«", "««", "»»", "“", "”",
// "‘", "‘‘", "’", "’’", "1", "2", "3", "4", "5", "6",
// "7", "8", "9", "0", "10", "11", "12", "13", "14",
// "15", "16", "17", "18", "19", "20", "25", "30", "33",
// "40", "50", "60", "66", "70", "75", "80", "90", "99",
// "100", "123", "1000", "10000", "12345", "100000", "1000000" };
private static string[] stopList { get; } = new string[] { "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(",
")", "_", "+", "-", "–", "=", "[", "]", "\\", ";",
"'", ",", ".", "/", "{", "}", "|", ":", "\"", "<",
">", "?", "..", "...", "«", "««", "»»", "“", "”",
"‘", "‘‘", "’", "’’"};
public string[][] SegmentIncomingText(int SegmentationParameter, string[] tokens)
{
string[][] Segments = new string[SegmentationParameter][];
List<string> tokensCleaned = tokens.Where(x => !stopList.Contains(x)).ToList();
List<List<string>>SegmentedTokens = Partition<string>(tokensCleaned, SegmentationParameter);
for (int i = 0; i < SegmentationParameter; i++) Segments[i] = SegmentedTokens[i].ToArray();
return (Segments);
}
//modified version of
//https://stackoverflow.com/a/3893011
/// <summary>
/// Partition a list of elements into a smaller group of elements
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="totalPartitions"></param>
/// <returns></returns>
public static List<List<string>> Partition<T>(List<string> list, int totalPartitions)
{
List<string>[] partitions = new List<string>[totalPartitions];
//if (list == null)
// throw new ArgumentNullException("list");
//if (totalPartitions < 1)
// throw new ArgumentOutOfRangeException("totalPartitions");
if (list == null || totalPartitions < 1)
{
partitions[0] = new List<string>() { "" };
return partitions.ToList();
}
int maxSize = (int)Math.Ceiling(list.Count / (double)totalPartitions);
int k = 0;
for (int i = 0; i < partitions.Length; i++)
{
partitions[i] = new List<string>();
for (int j = k; j < k + maxSize; j++)
{
if (j >= list.Count)
break;
partitions[i].Add(list[j]);
}
k += maxSize;
}
return partitions.ToList();
}
//old and busted. some of this is broken code from MEH that simply fails in some cases.
//tried to modify, got distracted, opted for a *far* cleaner approach instead (above).
//private List<string> Segment_Chopper(string[] array_to_split, int number_of_splits)
//{
// if (array_to_split.Length < number_of_splits) number_of_splits = array_to_split.Length;
// string[][] Segmented_Array = new string[number_of_splits][];
// int Segment_Size = (int)System.Math.Ceiling((double)array_to_split.Length / number_of_splits);
// for (int i = 0; i < number_of_splits; i++)
// {
// int SegmentStart = i * Segment_Size;
// int SegmentEnd = ((i + 1) * Segment_Size);
// int SegmentLength = SegmentEnd - SegmentStart;
// //if ((i != number_of_splits - 1))
// //{
// Segmented_Array[i] = new string[SegmentLength];
// if (SegmentEnd + 1 <= array_to_split.Length)
// {
// Array.Copy(array_to_split, SegmentStart, Segmented_Array[i], 0, SegmentLength);
// }
// else
// {
// SegmentLength = Segment_Size;
// string[] tailstring = new string[SegmentLength];
// for (int j = 0; j < SegmentLength; j++) tailstring[j] = "";
// for (int j = SegmentStart; j < array_to_split.Length; j++) tailstring[j - SegmentStart] = array_to_split[j];
// Array.Copy(tailstring, SegmentStart, Segmented_Array[i], 0, SegmentLength);
// }
// //}
// //else
// //{
// // SegmentLength = array_to_split.Length - SegmentStart;
// // Segmented_Array[i] = new string[SegmentLength];
// // if (SegmentEnd + 1 <= array_to_split.Length)
// // {
// // Array.Copy(array_to_split, SegmentStart, Segmented_Array[i], 0, SegmentLength);
// // }
// // else
// // {
// // string[] tailstring = new string[SegmentLength];
// // for (int j = 0; j < SegmentLength; j++) tailstring[j] = "";
// // for (int j = SegmentStart; j < array_to_split.Length; j++) tailstring[j - SegmentStart] = "";
// // Array.Copy(tailstring, 0, Segmented_Array[i], 0, SegmentLength);
// // }
// //}
// }
// List<string> TextSegments = new List<string>();
// for (int i = 0; i < number_of_splits; i++) TextSegments.Add(string.Join(" ", Segmented_Array[i]));
// return TextSegments;
//}
}
}