Hi Everyone,

I have a project from school which is a console application that takes hard-coded items and process those inside of one of my custom class which intherit from a list of strings. Then, it processes those items and apply the Apriori Algorithm (I cannot use any existent package or libraries but to implement it on my own and it's working using hard-coded data).

I am having problems changing the code to read it the data from a file and proceed with the rest of the implementation. I tried everything but am still not able to make it work.

I am trying to pass/have the array passed into my class the same way I did with hard-coded; if works then I don't need to make changes to my classes and even the algorithm implementation.

What I am trying to do is to read the data from a file line by line and take each word and put into my class (custom list class) the same way as I passed the data as hard-coded but I am not able to have exactly the same structure which won't work well further in the process.

I am under the impression that is not possible to have the data structured the same way while using hard-coded data or from an input file.

input file:

ink pen cheese bag
milk pen juice cheese
milk juice
juice milk cheese

My main/program.cs (please noticed that the hard-coded commented out works well):

string[] lines = File.ReadLines("../../InputApriori.txt").ToArray();
            foreach (var line in lines)
            {
                Console.WriteLine(line);                
                itemsDataList.Add(new DataSets() { line });
            }  
            Console.WriteLine("\n");                       
/*          // OPTION A  
            string Aitem1 = "ink";
            string Aitem2 = "pen";
            string Aitem3 = "cheese";
            string Aitem4 = "bag";
            string Aitem5 = "milk";
            string Aitem6 = "juice";
            itemsDataList.Add(new DataSets() { Aitem1, Aitem2, Aitem3, Aitem4 }); 
            itemsDataList.Add(new DataSets() { Aitem5, Aitem2, Aitem6, Aitem3 });
            itemsDataList.Add(new DataSets() { Aitem5, Aitem6 });
            itemsDataList.Add(new DataSets() { Aitem6, Aitem5, Aitem3 });   */

According to Visual Studio Community 2015 debugger:

In the debugger (watch window) the hard- coded displayed under my class (itemsDataList) the following:

=>itemsDataList ==> Count = 4 
=>[0] count = 4  "if  I click in the arrow of it, it has the following under it"
[0] = "ink"
[1] = "pen"
[2] = "cheese"
[3] ="bag"

=>[1] count = 4 ... 
[0] = "milk"
[1] = "pen"
[2] = "juice"
[3] ="cheese"

=>[2] count = 2 ...
[0] = "milk"
[1] = "juice"

=>[3] count = 3  ...
[0] = "juice"
[1] = "milk"
[2] = "cheese"

While reading the data from a file as in the code provided, the itemsDataList class in the watch window/debubber displays as follows:

=>itemsDataList ==> Count = 4
=>[0] count = 1 ...
     [0] = "ink pen cheese bag"
=>[1] count = 1 ...
     [0] = "milk pen juice cheese"
=>[2] count = 1 ...
    [0] = "milk juice"
=>[3] count = 1  ...
    [0] = "juice milk cheese"  

I really appreciate your help. Thanks in advance.

Recommended Answers

All 8 Replies

Hi, to achieve the desire result you must split the "line" (using this: string[] arr = line.Split();) a then pass "arr" as parameter of the Datasets contructor or by setting the internal property that holds those values.

Regards

Thanks rproffit.

I tried before ReadLines and ReadAllLines but it did not work for my purpose. Both methods make my class itemsDataList by having Count = 4 which is fine. However, this class has under [0] the whole line (first one) such as "ink pen cheese bag".

Under the debugger, what I am trying to do is to have under the itemsDataList Count = 4 the following:
[0] = "ink"
[1] = "pen"
[2] = "cheese"
[3] = "bag"
instead of [0] = "ink pen cheese bag" (the whole line)
And so on.

The code I provided (commented out) after passing to the constructor class have one item for each element position of the array.

The reason I am trying this is because everything works fine by using the hard-coded. My idea was not to touch my algorithm implementation and etc but to only change the way I was reading it in my main.

Perhaps it's not possible to read the data from the file and have my object itemsDataList with the same structured layout as when I passed the data hard-coded. If I am able to do this, then I don't need to touch anything else.

I really appreciate. Thanks.

Thanks Raul Perez. I will try your solution.

Hi Everyone,

I jus tried to pass the arr to the DataSets constructor but got compiler error. I changed it as you will see below but the arr.ToString( ) does not return any data.

foreach (string line in File.ReadAllLines("../../InputApriori.txt"))
{
        string[] arr = line.Split();
        Console.WriteLine(line);
        itemsDataList.Add(new DataSets() { arr.ToString() });
}

DataSets class excerpts:

namespace LanzaApriori   
{
    public class DataSets : List<string> 
    {
       public DataSets()
        { }
        ...
     }
}

I am not sure yet what to do. I am trying not to touch other parts of the code. Perhaps I will have too but I don't know yet how. For example, if I have to touch the DataSets constructor (right now it's a default constructor). In my case, the this pointer holds the data (I am a newbie in C# and I might calling the this pointer wrongly :)

Thanks and Happy Easter everyone.

Thanks Raul Perez and Rproffitt for your help.

I did not find the solution yet as described previously.
Right now, I am learning/working on the other parts of the project. I am learning about Amazon AWS, EMR and its related technologies.

I am not sure yet but probably my data can be as rows like in a database and the way it's right now can be okay.

Thanks again.

Marco

Hey marcolanza24

If I am reading this correctly, you want each individual word as its own element in the collection (in relation to a line). You could try something like

foreach (string line in File.ReadAllLines(@"..\..\InputApriori.txt"))
{
    itemsDataList.Add(new DataSets() { line.Split(new char [] {' '}, StringSplitOptions.RemoveEmptyEntries) });
}

What that does is the foreach loop has a collection of string, each index is one line from the file (the split there being the line break, .NET handles that part). Then what I have done is split the "line" itself on a space. This split returns a collection of strings, or in your case each word on the line. That should produce the results you want.

I don't know why you used "arr.ToString()" if I typed "arr".
But you figured it out.

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.