如何解析文本文件以将所有不同的变形分组? [英] How do I parse a text file to group all the different conjugations?

查看:97
本文介绍了如何解析文本文件以将所有不同的变形分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我有一个文本文件包含:



FIRST_WORD

男孩

男人

女孩



SECOND_WORD

正在阅读

正在写作



THIRD_WORD

a book

a小说


我需要以下输出 - (3组单词的每个结合):



男孩正在阅读一本书

男孩正在读一本小说

这个男孩正在写一本书

男孩正在写一本小说

男子正在读书



任何帮助都将不胜感激。



谢谢



我尝试了什么:



我理解需要一个循环来解析每节课,但我不知道如何将单词组合在一起形成完整的句子。

Hi all

I have a text file containing:

FIRST_WORD
The boy
The man
The girl

SECOND_WORD
is reading
is writing

THIRD_WORD
a book
a novel

I require the following output - (every conjugation of the 3 groups of words):

The boy is reading a book
The boy is reading a novel
The boy is writing a book
The boy is writing a novel
The man is reading a book

Any help would be greatly appreciated.

Thanks

What I have tried:

I understand a loop is required to parse each lesson but I not sure how to group together the words to form complete sentences.

推荐答案

你需要做的第一件事就是阅读文件,然后找到这些组。

阅读它很简单:

The first thing you need to do is read the file, and then locate the groups.
Reading it is easy:
string[] lines = File.ReadAllLines(pathToFile);

这将为您提供一个字符串数组,每个字符串都包含文件中的一行。

查找组更多复杂,但我这样做的方式 - 假设组标题是固定的 - 将设置一个字符串数组来包含标题:

That will give you an array of strings, each of which contain a line from the file.
Locating the groups is more complicated, but the way I'd do it - assuming the "group headings" are fixed - would be to set up an array of strings to contain the headings:

string[] groupHeadings = new string[] { "FIRST_WORD", "SECOND_WORD", "THIRD_WORD" };

你需要在某个地方保留小组:

And you will need somewhere to keep the groups:

List<string>[] groups = new List<string>[groupHeadings.Length];
for (int i = 0; i < groupHeadings.Length; i++)
    {
    groups[i] = new List<string>();
    }

这会创建一个列表数组:您希望从文件中获得的每个组一个列表。

现在,您可以查看文件中的每一行读取,并将其分成组。

This creates an array of lists: one list for each group you expect from the file.
Now, you can look at each line in the file you read, and separate it into groups.

int currentGroup = -1;
foreach (string line in lines)
    {
    if (!string.IsNullOrWhiteSpace(line))
        {
        if (groupHeadings.Contains(line))
            {
            currentGroup = Array.IndexOf(groupHeadings, line);
            }
        else
            {
            if (currentGroup >= 0 && currentGroup < groups.Length)
                {
                groups[currentGroup].Add(line);
                }
            else
                {
                string err = string.Format("Data cannot be added to a group: Group number {0}, line = \"{1}\"", currentGroup, line);
                throw new ApplicationException(err);
                }
            }
        }
    }

之后,组合由你决定!


引用:

但我不知道如何将单词组合在一起形成完整的句子。

but I not sure how to group together the words to form complete sentences.

对于输出,你需要搜索嵌套循环。

查看你的输出,每个句子放在一起,每个类别1个并枚举每种可能性。句子按111,112,121,122,211,212,221的顺序发出......





我们不做你的家庭作业,所以没有代码。

HomeWork不会在乞求别人做你的工作时测试你的技能,它会让你思考并帮助你的老师检查你对你已经学过的课程以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,试一试,重读你的课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。

For the output, you need to search for nested loops.
Look at your output, each sentence put together 1 piece of each category and enumerate each possibility. Sentences are made in this order 111, 112, 121, 122, 211, 212, 221 ...


We do not do your HomeWork, so no code.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.


这篇关于如何解析文本文件以将所有不同的变形分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆