实现此目的的方法是获取初始文本,并使用空格将其拆分为字符串数组string.split(' ');
接下来,您需要迭代数组中的每个字符串。
这对于单个单词来说很容易,但对于组来说则更复杂。
因此,您需要定义组大小。您必须控制每次迭代时指针在数组中前进的位数。
一旦能够迭代数组,您就需要获取该组单词(无论您选择的长度),并将其存储在某个地方。
示例中的字典是一个好方法。
如果字典包含单词组,则将其值加一。
如果不包含该组,只需添加它,默认值为 1。
if (wordList.ContainsKey(theKey)) {
wordList[theKey]++;
} else {
wordList.Add(theKey, 1);
}
您确实正确地提到您的研究表明正则表达式的性能并不高。对于此任务,正则表达式是完全错误的工具 - 您不是在寻找模式,而是在检查组。
为此,您必须从头到尾浏览文本,检查值。
任何涉及遍历文本并对其运行重复函数的任务都不应使用正则表达式。
编辑:我对正则表达式性能的最初假设是不正确的 - 在 C# 中,它似乎比 Java 快得多,但我仍然认为纯正则表达式方法不如使用正则表达式标记文本那么快然后使用循环或 linq 表达式来查找组。
Stating
@galakt 正如我上面提到的,让我们说 3。这重要吗?
词组的概念是完全抽象的。是的,这是一组单词,但整个文本块都是一组单词。
必须应用规则来控制您对这组单词的操作方式。
下面是一个示例方法,它将根据通过方法调用传递的大小返回所有单词组的字典。
它不会从文本中删除任何非字母数字字符,但即使组大小较大,它也很快。
要调用它,请使用Dictionary<String, int> SingleWordGroups = GetWordGroupInstances(1);
private Dictionary<String, int> GetWordGroupInstances(int GroupSize) {
Dictionary<String, int> WordGroupInstances = new Dictionary<string, int>();
//Grab the string to work from...
String[] sourceText = GetSourceText().Split(' ');
int pointer = 0;
StringBuilder groupBuilder = new StringBuilder();
while (pointer < sourceText.Length - GroupSize) {
groupBuilder.Clear();
int offset = pointer + GroupSize;
for (int i = pointer; i < offset; i++) {
//prepend a space to allow separation between words in groups.
//We can make a substring from this later starting from char 1
//to lose the initial whitespace from the string.
groupBuilder.Append(" ").Append(sourceText[i]);
}
String key = groupBuilder.ToString().Substring(1);
if (!WordGroupInstances.ContainsKey(key)) {
WordGroupInstances.Add(key, 1);
} else {
WordGroupInstances[key]++;
}
/**
* Setting the pointer to increase by group size grabs a group, moves on
* to the end of the group, and grabs the next.
*
*/
pointer += GroupSize;
/**
* Setting the point to increment by 1 grabs a group, advances by 1 word, then
* grabs the next, so from the phrase - "Hello world, I'm some text", the groups of size 2 would be
* "Hello world,", "world, I'm", "I'm some" etc...
*/
//pointer++;
}
return WordGroupInstances;
}
下面的方法被修改为依次产生所有组输出,所以
这
绿色的
绿藻
绿藻
ETC...
值得注意的是,整个文本必须转换为小写或大写,以便单词不区分大小写。
我对此进行了一些改进以提高性能(并消除了对中断指令的需要)。
private Dictionary<String, int> GetAllGroups() {
Dictionary<string, int> WordGroupInstances = new Dictionary<string, int>();
StringBuilder groupBuilder = new StringBuilder();
String[] sourceText = GetSourceText().Split(' ');
for (int i = 0; i < sourceText.Length; i++) {
groupBuilder.Clear();
for (int j = i; j < sourceText.Length; j++) {
groupBuilder.Append(" ").Append(sourceText[j]);
String key = groupBuilder.ToString().Substring(1);
if (!WordGroupInstances.ContainsKey(key)) {
WordGroupInstances.Add(key, 1);
} else {
WordGroupInstances[key]++;
}
}
}
return WordGroupInstances;
}
经过对文本语料库(288个词)的性能测试,它将在0.171886秒内创建41773个词组。