【发布时间】:2015-11-28 12:13:06
【问题描述】:
目前我正在尝试创建一个应用程序来进行一些文本处理以读取文本文件,然后我使用字典来创建单词索引,从技术上讲它会像这样......程序将运行并读取文本文件然后检查它,看看这个词是否已经在那个文件中,以及它的 id 词是什么作为唯一词。如果是这样,它将打印出他们遇到的每个单词的索引号和出现总数,并继续检查整个文件。并产生这样的东西:http://pastebin.com/CjtcYchF
这是我正在输入的文本文件的示例:http://pastebin.com/ZRVbhWhV 快速 ctrl-F 显示“not”出现 2 次,“that”出现 4 次。我需要做的是索引每个单词并像这样调用它:
sample input : "that I have not that place sunrise beach like not good dirty beach trash beach"
dictionary : output.txt / output.dat:
index word
1 I 4:2 1:1 2:1 3:2 5:1 6:1 7:3 8:1 9:1 10:1 11:1
2 have
3 not
4 that
5 place
6 sunrise
7 beach
8 like
9 good
10 dirty
11 trash
我尝试实现一些代码来创建字典。这是我目前所拥有的:
private void bagofword_Click(object sender, EventArgs e)
{
//creating dictionary in background
//Dictionary<string, int> dict = new Dictionary<string, int>();
string rawinputbow = File.ReadAllText(textBox31.Text);
//string[] inputbow = rawinputbow.Split(' ');
var inputbow = rawinputbow.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
.ToList();
var dict = new OrderedDictionary();
var output = new List<int>();
foreach (var element in inputbow.Select((word, index) => new { word, index }))
{
if (dict.Contains(element.word))
{
var count = (int)dict[element.word];
dict[element.word] = ++count;
output.Add(GetIndex(dict, element.word));
//textBoxfile.Text = output.ToString();
// textBoxfile.Text = inputbow.ToString();
string result = string.Join(",", output);
textBoxfile.Text = result.ToString();
}
else
{
dict[element.word] = 1;
output.Add(GetIndex(dict, element.word));
//textBoxfile.Text = dict.ToString();
string result = string.Join(",", output);
textBoxfile.Text = result.ToString();
}
}
}
public int GetIndex(OrderedDictionary dictionary, string key)
{
for (int index = 0; index < dictionary.Count; index++)
{
if (dictionary[index] == dictionary[key])
return index; // We found the item
//textBoxfile.Text = index.ToString();
}
return -1;
}
有谁知道如何完成该代码?非常感谢任何帮助!
【问题讨论】:
-
几年前我做了这个完全一样的作业问题......
-
Does anyone know how to complete that code?您遇到的实际问题是什么?代码在做什么或不做什么?您遇到了哪些错误或意外输出? -
你不应该在标点符号上也分开,所以句尾的东西不会和句末的句号一起出现。
-
为什么在 ASP.NET 文本框控件上使用
File.ReadAllText? -
@DangerZone 你介意与我分享吗? :)
标签: c# visual-studio text-processing