【发布时间】:2011-06-12 01:29:11
【问题描述】:
我有一些要处理的标记文件。
文件中的每一行都具有以下格式(为清晰起见进行了格式化):
Name1 Tag1 Origin1
Name2 Tag2 Origin2
我需要一个执行以下操作的 C# 解决方案:
- 获取名称出现的标记、来源和行号。
- 查看两个或多个连续名称是否具有相同的标签。如果是这样,请将它们组合起来。
为了做到这一点,我尝试了以下代码:
var line_token = new List<object_tag>();
line_token.Add(new object_tag
{ file_name = filename,
line_num = line_number,
string_name = name,
string_tag = tag,
string_origin = origin
});
List 从ArrayList 获取其输入值。
示例输入:
item[0]:
file_name:"test1.txt"
line_num:1
string_name:Asia
string_tag:NP
string_origin:<unknown>
有没有办法根据string_tag 搜索此列表,并查找一行中的两个或多个项目是否具有相同的 string_tag,如果是,则将它们组合成一个新项目?
更新:让我发布一些代码以使问题更清楚..
我用这个创建文件列表..
private static List <object_tag> tagged_line_list()
{
string input = "C:Desktop\\_tagged\\";
string line;
string[] files;
int j = 0;
if (System.IO.Directory.Exists(input) == false)
{
Console.WriteLine("The file doesn't exist");
}
//take the folder's files
files = System.IO.Directory.GetFiles(input);
//create new list with type object_tag
var line_token = new List<object_tag>();
//delete the contents of the list
line_token.Clear();
//create an array list
ArrayList tokens = new ArrayList();
tokens.Clear();
foreach (string file in files)
{
string filename = System.IO.Path.GetFileNameWithoutExtension(file);
int line_number = 1;
//read the files
StreamReader sr = new StreamReader(file);
while ((line = sr.ReadLine()) != null)
{
string input_line = line;
char[] delimiters = { '\t' };
//split the line in words
string[] words = input_line.Split(delimiters);
//add each word to the token array_list
foreach (string word in words)
{
tokens.Add(word);
}
string name = tokens[j+ 0] as string;
string tag = tokens[j + 1] as string;
string origin = tokens[j + 2] as string;
//add to the line-token list instances
line_token.Add(new object_tag{file_name=filename,line_num=line_number,string_name=name,string_tag=tag,string_origin=origin});
j = j + 3;
line_number++;
}
sr.Close();
}
//returns the line_token list
return line_token;
}
接下来我要搜索的代码列表是
private static List<object_tag> search_list()
{
//calls the tagged_line_list method for retrieving the line-token list
var line_token = tagged_line_list();
object_tag last = null;
List<object_tag> du_np = new List<object_tag>();
du_np.Clear();
List<object_tag> list_np_query = new List<object_tag>();
list_np_query.Clear();
var np_query =
from i in line_token
where ((i.string_tag == "NP" | i.string_tag == "NPS"))
select i;
//create new list which contains instances with string_tag NP or NPS
list_np_query = np_query.ToList<object_tag>();
for (int i = 0; i < list_np_query.Count; i++)
{
if (last == null)
{
last = list_np_query[i];
}
else if (
//the objects are in the same file
(last.file_name == list_np_query[i].file_name)
&
//the objects are consecutive
(list_np_query[i].line_num - last.line_num == 1)
)
{
last.file_name = list_np_query[i - 1].file_name;
last.line_num = list_np_query[i - 1].line_num;
last.string_name = last.string_name + " " + list_np_query[i].string_name;
last.string_tag = list_np_query[i - 1].string_tag;
last.string_origin = "<unknown>";
du_np.Add(last);
}
else
{
last = list_np_query[i];
}
}
return (du_np);
}
现在我有一个名为 list_np_query 的列表,其中仅包含带有 string_tag NP 或 NPS 的对象。如果对象在连续的行中并且具有相同的文件名,我会将它们放在一个名为du_np 的新列表中。解决方案就在我面前,但我看不到它......
无论如何,感谢大家的帮助和时间!!!!!
【问题讨论】:
-
请定义合并。因为即使标签相同,名称和来源也不同。另外我假设您必须继续组合,直到没有要组合的记录,例如连续5条相同标签的记录应合并为1条。
-
你能把你喜欢的答案之一标记为正确的吗?