Exemplo n.º 1
0
        //////////////////////////////////////////////////////
        // Function:
        //	LoadTrainingData
        //
        // Purpose:
        //	Loads the training data used to classify queries.
        //////////////////////////////////////////////////////
        public bool LoadTrainingData(
            string szInFileName)
        {
            using (var pInputFile = new StreamReader(szInFileName))
            {
                int iNumClasses = 0;
                int i           = 0;
                int j           = 0;

                string szInputString;

                // Read in the classes
                string input = pInputFile.ReadLine();
                input       = pInputFile.ReadLine();
                iNumClasses = int.Parse(input);
                //fscanf(pInputFile, "%d", &iNumClasses);

                m_rwExamples.Clear();
                m_rwAttributes.Clear();
                m_rwClasses.Clear();

                // Print out the names of the classes as we read them in
                //cout << "Read " << iNumClasses << " classes";

                // Read in the classifications
                // that we can put the data into
                for (i = 0; i < iNumClasses; ++i)
                {
                    szInputString = pInputFile.ReadLine();
                    //fscanf(pInputFile, "%s", szInputString);

                    m_rwClasses.Add(szInputString.Trim());

                    //cout << " " << m_rwClasses[i];
                }
                //cout << endl;

                // Read in the attributes
                pInputFile.ReadLine();
                //fscanf(pInputFile, "%s", szInputString);

                int iNumAttributes = int.Parse(pInputFile.ReadLine());
                //fscanf(pInputFile, "%d", &iNumAttributes);
                //cout << "Read " << iNumAttributes << " attributes\n";

                // Read in each attribute and it's values
                for (i = 0; i < iNumAttributes; ++i)
                {
                    // Check to make sure we haven't hit
                    // a bad file and EOF
                    if (pInputFile.EndOfStream)
                    {
                        return(false);
                    }

                    szInputString = pInputFile.ReadLine().Trim();
                    //fscanf(pInputFile, "%s", szInputString);


                    m_rwAttributes.Add(new CAttribute());
                    m_rwAttributes[i].SetName(szInputString);

                    int iNumValues = int.Parse(pInputFile.ReadLine());
                    //fscanf(pInputFile, "%d", &iNumValues);

                    //cout << "Attribute " << m_rwAttributes[i].GetName() << " has " << iNumValues << " values:";

                    for (j = 0; j < iNumValues; ++j)
                    {
                        szInputString = pInputFile.ReadLine().Trim();
                        //fscanf(pInputFile, "%s", szInputString);
                        m_rwAttributes[i].AddValue(szInputString);

                        //cout << " " << m_rwAttributes[i].GetValue(j);
                    }

                    //cout << endl;
                }

                // We should get the keyword "examples"
                szInputString = pInputFile.ReadLine().Trim();
                //fscanf(pInputFile, "%s", szInputString);

                //cout << "EXAMPLES\n";

                // Read in the examples until we hit EOF
                // If we are running a debug build, print out
                // the example data as we read it in
                while (!pInputFile.EndOfStream)
                {
                    var newExample = new CExample();

                    szInputString = pInputFile.ReadLine().Trim();
                    //int iScanResult(fscanf(pInputFile, "%s", szInputString ) );

                    if (string.IsNullOrEmpty(szInputString))
                    {
                        break;
                    }
                    //cout << szInputString;

                    int iClassIdentifier = 0;

                    if (!GetClassIdentifier(szInputString, ref iClassIdentifier))
                    {
                        Debug.Fail("Unable to find class identifier!");
                        break;
                    }

                    newExample.SetClassIdentifier(iClassIdentifier);

                    for (i = 0; i < m_rwAttributes.Count; ++i)
                    {
                        szInputString = pInputFile.ReadLine().Trim();
                        //fscanf(pInputFile, "%s", szInputString);
                        //cout << " " << szInputString;

                        int iValueIdentifier = 0;

                        if (m_rwAttributes[i].GetValueIdentifier(szInputString, ref iValueIdentifier))
                        {
                            newExample.SetValueIdentifier(i, iValueIdentifier);
                        }
                    }

                    // cout << endl;

                    // The the newly read example into the example pool.
                    AddExample(newExample);

                    //		cout << "Read new example: " << newExample << endl;
                }

                return(true);
            }
        }
Exemplo n.º 2
0
 //////////////////////////////////////////////////////
 // Mutator:
 //	AddExample
 //
 // Purpose:
 //	Adds an example to the pool of training data.
 //////////////////////////////////////////////////////
 public void AddExample(
     CExample inNewExample)
 {
     m_rwExamples.Add(inNewExample);
 }