105 lines
3.6 KiB
C#
105 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Presentation3Tiers.DAL
|
|
{
|
|
/// <summary>
|
|
/// Auteur : Hugo St-Louis
|
|
/// Description : Classe concrète qui gère un fichier texte.
|
|
/// Date : 2023-03-29
|
|
/// </summary>
|
|
public class GestionFichierTexte : ILectureFichier
|
|
{
|
|
public BDApprentissageAuto BD { get; set; }
|
|
/// <summary>
|
|
/// permet de remplir la structure de données pour l'apprentissage automatique à partir dun fichier texte
|
|
/// </summary>
|
|
/// <param name="sNomFichier"></param>
|
|
public BDApprentissageAuto RemplirDonneesBD(string sNomFichier)
|
|
{
|
|
StreamReader lecteur = new StreamReader(sNomFichier);
|
|
string sLigne = "";
|
|
BD = new BDApprentissageAuto();
|
|
string[] sTabElements = null;
|
|
|
|
if (!lecteur.EndOfStream)
|
|
{
|
|
sLigne = lecteur.ReadLine();
|
|
BD.NbElements = Convert.ToInt32(sLigne);
|
|
|
|
sLigne = lecteur.ReadLine();
|
|
BD.NbAttributs = Convert.ToInt32(sLigne);
|
|
|
|
BD.Elements = new double[BD.NbElements, BD.NbAttributs - 1];
|
|
BD.Resultats = new int[BD.NbElements];
|
|
for (int i = 0; i < BD.NbElements; i++)
|
|
{
|
|
sLigne = lecteur.ReadLine();
|
|
sTabElements = sLigne.Split('\t');
|
|
for (int j = 0; j < sTabElements.Length - 1; j++)
|
|
{
|
|
BD.Elements[i, j] = Convert.ToDouble(sTabElements[j]);
|
|
}
|
|
BD.Resultats[i] = Convert.ToInt32(sTabElements[sTabElements.Length - 1]);
|
|
}
|
|
}
|
|
return BD;
|
|
}
|
|
|
|
/// <summary>
|
|
/// rempli et affiche les resultats dun fichier dapprentissage auto
|
|
/// </summary>
|
|
/// <param name="sNomFichier"></param>
|
|
/// <returns></returns>
|
|
public string RemplirEtAfficherDonneesBD(string sNomFichier)
|
|
{
|
|
RemplirDonneesBD(sNomFichier);
|
|
string sResultat = "";
|
|
sResultat = "nb elements : " + BD.NbElements + "\r\n";
|
|
sResultat += "nb attribut : " + BD.NbAttributs + "\r\n";
|
|
sResultat += "Voici les attribut et valeur de verité" + "\r\n";
|
|
for (int i = 0; i < BD.NbElements; i++)
|
|
{
|
|
for (int j = 0; j < BD.NbAttributs - 1; j++)
|
|
{
|
|
sResultat += BD.Elements[i, j] + "\t";
|
|
}
|
|
sResultat += "\r\nValeur de verite : " + BD.Resultats[i] + "\r\n";
|
|
}
|
|
return sResultat;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Lit et retourne le contenu d'un fichier texte
|
|
/// pour lequel le nom est passé en paramètre.
|
|
/// </summary>
|
|
/// <param name="sNomFichier"></param>
|
|
/// <returns></returns>
|
|
public string LireFichierTexte(string sNomFichier)
|
|
{
|
|
bool bFichierExiste = File.Exists(sNomFichier);
|
|
string sContenuFichier = "";
|
|
StreamReader sr = null;
|
|
if (!bFichierExiste)
|
|
return "Le fichier n'existe pas ....";
|
|
try
|
|
{
|
|
sr = new StreamReader(sNomFichier);
|
|
sContenuFichier = sr.ReadToEnd();
|
|
return sContenuFichier;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return "Erreur lors de la lecture du fichier : " +
|
|
ex.Message;
|
|
}
|
|
}
|
|
}
|
|
}
|