Résolu Remplacer des mots clés à partir d'une BDD

Statut
N'est pas ouverte pour d'autres réponses.

Lagger

Responsable des lags
Premium
Inscription
28 Novembre 2011
Messages
1 903
Réactions
581
Points
12 558
Bonjour, bonsoir

J'ai 2 form:
- Une avec deux TextBox, un bouton, un bouton pour ouvrir la form2
- Une avec une TextBox, un bouton pour envoyer dans ma BDD, une listBox lié à un .txt (qui est ma BDD)


Je veux pouvoir remplacer des mots clés du TextBox1 qui sont renseignés dans la BDD, par du texte brut (des balises) dans le TextBox2 en ayant cliquer sur le bouton.


Admettons, j'ai la phrase "Realitygaming est le meilleur forum du monde". Realitygaming et forum sont des mots clés renseignés dans la BDD, et si je clique sur le bouton, il vérifie si il y a des mots clés, si oui, il me remplace les mots clés par "<a href=" '">lemotclé</a>"

Ce qui donnerai

Realitygaming est le meilleur forum du monde

On clique sur le bouton

<a href=" ">Realitygaming</a> est le meilleur <a href=" ">forum</a> du monde


Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ReferencementAuto
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public List<string> readDatabase()
        {
            string line;
            List<string> lines = new List<string>();
            StreamReader file = new StreamReader(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\reference.txt");
            while ((line = file.ReadLine()) != null)
            {
                lines.Add(line);
            }

            file.Close();
            return lines;
        }
        public string replaceword(string texteDeBase)
        {
            List<string> keyword = readDatabase();
            foreach (string tag in keyword)
            {
                if (texteDeBase.Contains(tag))
                    texteDeBase = texteDeBase.Replace(tag, string.Format("<a href='www.site.com/tag/{0}'>{0}</a>", tag));
            }
            return texteDeBase;
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = replaceword(textBox1.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox3.Text.Length > 0)
            {
                listBox1.Items.Add(textBox3.Text);
                StreamWriter sw = new StreamWriter(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\reference.txt");
                foreach (string item in listBox1.Items)
                {
                    sw.WriteLine(item);
                }
                sw.Close();
                MessageBox.Show("Programs saved!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Remove(listBox1.SelectedItem);
            StreamWriter sw = new StreamWriter(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\reference.txt");
            foreach (string items in listBox1.Items)
            {
                sw.WriteLine(items);
            }
            sw.Close();
            MessageBox.Show("Programs saved!");
        }
    }
}


Merci d'avance :love:
 
Dernière édition:

Swfq

Membre
Inscription
12 Juin 2013
Messages
146
Réactions
34
Points
2 406
Je considère que ton fichier txt est composé de cette manière :
Code:
mot clé 1
mot clé 2
mot clé 3
...

Code:
public List<string> readDatabase()
{
      string line;
      List<string> lines= new List<string>();

      System.IO.StreamReader file = new System.IO.StreamReader("c:\\chemin\\vers\\ta\\bdd.txt");

      while((line = file.ReadLine()) != null)
      {
         lines.Add(line);
      }

      file.Close();
      return lines;
}

public string jsptropquoimettre(string texteDeBase)
{
      //on recupere la "BDD"
      List<string> mots_cles = readDatabase();

     //on va chercher si le texteDeBase contient un de nos mots de la BDD
     foreach(string mot in mots_cles)
     {
        //on vérifie si le texteDeBase contient notre mot
        if(texteDeBase.Contains(mot))
           texteDeBase = texteDeBase.Replace(mot,String.Format( <ahref=\"www.site.fr/index.php?'{0}'\">{0}</a>", mot)); 
//si c'est le cas on replace

      }

      return texteDeBase;
}

Comment l'utiliser ?
Simplement dans la fonction de ton button_click tu mets :
Code:
TextBox2.Text = jsptropquoimettre(TextBox1.Text);
 
Dernière édition:

Lagger

Responsable des lags
Premium
Inscription
28 Novembre 2011
Messages
1 903
Réactions
581
Points
12 558
Je considère que ton fichier txt est composé de cette manière :
Code:
mot clé 1
mot clé 2
mot clé 3
...

Code:
public List<string> readDatabase()
{
      string line;
      List<string> lines= new List<string>();

      System.IO.StreamReader file = new System.IO.StreamReader("c:\\chemin\\vers\\ta\\bdd.txt");

      while((line = file.ReadLine()) != null)
      {
         lines.Add(line);
      }

      file.Close();
      return lines;
}

public string jsptropquoimettre(string texteDeBase)
{
      //on recupere la "BDD"
      List<string> mots_cles = readDatabase();

     //on va chercher si le texteDeBase contient un de nos mots de la BDD
     foreach(string mot in mots_cles)
     {
        //on vérifie si le texteDeBase contient notre mot
        if(texteDeBase.Contains(mot))
           texteDeBase = texteDeBase.Replace(mot,String.Format("<ahref=\"www.site.fr/index.php?'{0}'\">{0}</a>", mot)); //si c'est le cas on replace

      }

      return texteDeBase;
}

Comment l'utiliser ?
Simplement dans la fonction de ton button_click tu mets :
Code:
TextBox2.Text = jsptropquoimettre(TextBox1.Text);
Merci beaucoup, je vais essayer d’intégrer ça a mon code ! :love:
 

Lagger

Responsable des lags
Premium
Inscription
28 Novembre 2011
Messages
1 903
Réactions
581
Points
12 558
Je considère que ton fichier txt est composé de cette manière :
Code:
mot clé 1
mot clé 2
mot clé 3
...

Code:
public List<string> readDatabase()
{
      string line;
      List<string> lines= new List<string>();

      System.IO.StreamReader file = new System.IO.StreamReader("c:\\chemin\\vers\\ta\\bdd.txt");

      while((line = file.ReadLine()) != null)
      {
         lines.Add(line);
      }

      file.Close();
      return lines;
}

public string jsptropquoimettre(string texteDeBase)
{
      //on recupere la "BDD"
      List<string> mots_cles = readDatabase();

     //on va chercher si le texteDeBase contient un de nos mots de la BDD
     foreach(string mot in mots_cles)
     {
        //on vérifie si le texteDeBase contient notre mot
        if(texteDeBase.Contains(mot))
           texteDeBase = texteDeBase.Replace(mot,String.Format( <ahref=\"www.site.fr/index.php?'{0}'\">{0}</a>", mot));
//si c'est le cas on replace

      }

      return texteDeBase;
}

Comment l'utiliser ?
Simplement dans la fonction de ton button_click tu mets :
Code:
TextBox2.Text = jsptropquoimettre(TextBox1.Text);
Comment je peux charger le .txt au démarrage du logiciel pour qu'il apparaisse dans la listbox ?

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ReferencementAuto
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public List<string> readDatabase()
        {
            string line;
            List<string> lines = new List<string>();
            StreamReader file = new StreamReader(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\reference.txt");
            while ((line = file.ReadLine()) != null)
            {
                lines.Add(line);
            }

            file.Close();
            return lines;
        }
        public string replaceword(string texteDeBase)
        {
            List<string> keyword = readDatabase();
            foreach (string tag in keyword)
            {
                if (texteDeBase.Contains(tag))
                    texteDeBase = texteDeBase.Replace(tag, string.Format("<a href='www.site.com/tag/{0}'>{0}</a>", tag));
            }
            return texteDeBase;
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = replaceword(textBox1.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox3.Text.Length > 0)
            {
                listBox1.Items.Add(textBox3.Text);
                StreamWriter sw = new StreamWriter(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\reference.txt");
                foreach (string item in listBox1.Items)
                {
                    sw.WriteLine(item);
                }
                sw.Close();
                MessageBox.Show("Programs saved!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Remove(listBox1.SelectedItem);
            StreamWriter sw = new StreamWriter(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\reference.txt");
            foreach (string items in listBox1.Items)
            {
                sw.WriteLine(items);
            }
            sw.Close();
            MessageBox.Show("Programs saved!");
        }
    }
}
 
Statut
N'est pas ouverte pour d'autres réponses.
Haut