Springen naar inhoud


- - - - -

Fun Project : Woorden Spellen Als Chemische Elementen

C# mendeljev parser Recursief json

  • Log in a.u.b. om te beantwoorden
Geen reacties in dit onderwerp

#1 doebi

doebi

    Master Developer

  • Leden
  • PipPipPipPipPip
  • 539 berichten
    Laatst bezocht 14 jul 2019 17:53

Geplaatst op 22 juni 2019 - 17:01

Hoi,

Effen een fun project gemaakt in C# . De bedoeling is om een lijst met woorden te schrijven als een combinatie van chemische elementen.
Ingredienten :  
   - recursiviteit
   - JSON bestanden
   - dictionary & sorted dictionary
   - gridview
  - exporteren

De lijst met woorden vond ik eenvoudig op google ( > 370.000 woorden) in JSON formaat.
De lijst van elementen van de tabel van mendeljev in JSON (118 elementen)

Doel : grappige combinaties maken

Vb woord :

sarcasm :

{"strWord":"sarcasm","strNr":"1","strFormula":"S+Ar+Ca+Sm"}

Hopelijk kan iemand er iets mee aanvangen

Suggesties welkom

Code:
// Fun project WordsToElements
//
// By Doubbel Danny
//
// Find me @ dannydoubbel@gmail.com

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WordsToElements
{
public partial class FindingElements : Form
{
	 public class clsData
	 {
		 public string strWord
		 {
			 get;set;
		 }
		 public string strNr
		 {
			 get;set;
		 }
		 public string strFormula
		 {
			 get;set;
		 }
	 }
	 public Dictionary<String,Object> AllWords;
	 public SortedDictionary<String, Object> AllElements;
	 public enum enStatus {Play,Pause,Stop,Error,Export};
	 public enStatus Status;
	 public long FormulasFound = 0;
	 public IBindingList myData = new BindingList<clsData>();
	
	 public FindingElements()
	 {
		 InitializeComponent();
	 }
	 private void FindingElements_Load(object sender, EventArgs e)
	 {
		 Status = enStatus.Stop;
		 DisplayStatus();
		 Dictionary<String, Object> AllElementsUnsorted;
		 try
		 {
			 var jsonWords = File.ReadAllText(@"C:\Words Woorden JSON\words_dictionary.json");
			 AllWords = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonWords);
			 txtNrOfWords.Text = AllWords.Count().ToString();
			 var jsonElements = File.ReadAllText(@"C:\Words Woorden JSON\elements_alpha.json");			
			 AllElementsUnsorted = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonElements);
			 AllElements = new SortedDictionary<string, object>(AllElementsUnsorted);
			 txtNrOfElements.Text = AllElements.Count().ToString();
			 dgFormulas.DataSource = myData;
		 }
		 catch (Exception ex)
		 {
			 MessageBox.Show("Something went wrong : " + ex.ToString(), "WARNING");
		 }
	 }
	 private void btnPlay_Click(object sender, EventArgs e)
	 {
		 switch (Status)
		 {
			 case enStatus.Stop:
				 {					
					 Status = enStatus.Play;
					 DisplayStatus();
					 ParseWords();
					 Status = enStatus.Stop;
					 DisplayStatus();
					 break;
				 }
			 case enStatus.Play:
				 {
					 // nothing to do
					 break;
				 }
			 case enStatus.Error:
				 {
					 // implement
					 break;
				 }
			 case enStatus.Pause:
				 {
					 Status = enStatus.Play;
					 break;
				 }
		 }
		 DisplayStatus();
	 }
	 private void btnPauze_Click(object sender, EventArgs e)
	 {
		 switch (Status)
		 {
			 case enStatus.Stop:
				 {
					 // nothing to do
					 break;
				 }
			 case enStatus.Play:
				 {
					 Status = enStatus.Pause;
					 break;
				 }
			 case enStatus.Error:
				 {
					 // implement
					 break;
				 }
			 case enStatus.Pause:
				 {
					 // nothing to do
					 break;
				 }
		 }
		 DisplayStatus();
	 }
	 private void btnStop_Click(object sender, EventArgs e)
	 {
		 switch (Status)
		 {
			 case enStatus.Stop:
				 {
					 // nothing to do
					 break;
				 }
			 case enStatus.Play:
				 {
					 Status = enStatus.Stop;
					 break;
				 }
			 case enStatus.Error:
				 {
					 // implement
					 break;
				 }
			 case enStatus.Pause:
				 {
					 // nothing to do
					 Status = enStatus.Stop;
					 break;
				 }
		 }
		 DisplayStatus();
	 }
	 private void DisplayStatus()
	 {
		 String S = "";
		 switch (Status)
		 {
			 case enStatus.Stop:
				 {
					 S = "Stop";
					 checkOrder.Enabled = true;
					 btnExport.Enabled = true;
					 break;
				 }
			 case enStatus.Play:
				 {
					 S = "Play";
					 checkOrder.Enabled = false;
					 btnExport.Enabled = false;
					 break;
				 }
			 case enStatus.Error:
				 {
					 // not yet implemented
					 S = "Error";
					 break;
				 }
			 case enStatus.Pause:
				 {
					 S = "Pause";
					 checkOrder.Enabled = false;
					 btnExport.Enabled = true;
					 break;
				 }
			 case enStatus.Export:
				 {
					 S = "Export";
					 checkOrder.Enabled = false;
					 btnExport.Enabled = false;
					 break;
				 }
		 }
		 txtStatus.Text = S;
	 }
	 public void ParseWords()
	 {
		 string CurrentWord = "";
		 long CurrentWordNumber = 0;
		 // foreach (var Word in AllWords.Reverse())
		 foreach (var Word in checkOrder.Checked ? AllWords.Reverse() : AllWords )
			 {
			 Application.DoEvents();
			 while (Status != enStatus.Play)
			 {
				 Application.DoEvents();
				 if (Status == enStatus.Stop)
				 {
					 return;
				 }
			 }
			 CurrentWord = Word.Key.ToUpper();
			
				 txtBusyWord.Text = CurrentWord;
				 txtBusyWord.Refresh();
				 txtWordNumber.Text = (CurrentWordNumber++).ToString();
				 txtWordNumber.Refresh();
				 long Teller = 0;
			 ParseWord(Word.Key, CurrentWord, "", "", ref Teller);
			
		 }
	 }
	 public bool ParseWord(String WordBeingParsed,String WordToParse,String FoundElements,String PreviousElement,ref long Counter )
	 {
		 // WordBeingParsed = Word that is unchanged that needs to be parsed
		 // WordToParse = Word that change to be parsed, each time there is an element match, cut off that element from front of WordToParse
		 // FoundElements = Hold the found formula
		 // PreviousElement = Holds the element from the parent recursieve call
		 string CurrentElement = "";
		 if (WordToParse.Length == 0)
		 {
			 // means formula has been found
			 if (FoundElements.Length > 0)
			 {
				 if (PreviousElement.Length > 0)
				 {
					 // FoundElements += "+";
				 }
			 }
			 FoundElements = AddElement(FoundElements, PreviousElement);
			 Counter++;
			 FormulasFound++;
			 txtFormulasFound.Text = FormulasFound.ToString();
			 txtFormulasFound.Refresh();
			
			 // make a new formula row
			 var Data = new clsData();
			 Data.strFormula = FoundElements;
			 Data.strNr = Counter.ToString();
			 Data.strWord = WordBeingParsed;
			 // add the row to the formula data grid
			 // by updating its datasource
			 myData.Add(Data);
		 }
		 // loop through all elements in AllElements
		 foreach (var Element in AllElements)
		 {
			 // catch any user events
			 Application.DoEvents();
			 while (Status != enStatus.Play)
			 {
				 Application.DoEvents();
				 if (Status == enStatus.Stop)
				 {
					 // quit from this function
					 return false;
				 }
			 }
			 CurrentElement = Element.Key;
			 txtBusyElement.Text = CurrentElement;
			 txtBusyElement.Refresh();
			 // if word to check start with the Element from the foreach loop
			 if (string.Compare( CurrentElement.ToUpper(), WordToParse ) == 1)
			 {
				 // meaning current element is greater than WordToParse
				 // so quit from this function
				 return false;
			 }
			 if (WordToParse.StartsWith(CurrentElement.ToUpper()))
			 {									
				 // FoundElements += PreviousElement;
				 if (ParseWord(
					 WordBeingParsed,
					 WordToParse.Substring(CurrentElement.Length),
					 AddElement(FoundElements, PreviousElement),
					 CurrentElement,
					 ref Counter ))
				 {
					 return true;
				 }
			 }

		 }

		 // being here means no Elements match WordToParse
		 return false;
	 }
	 public string AddElement(string Elements,string Element)
	 {
		 // means of this function is to avoid + sign on top of the spring and double + signs, and return Elements + plus sign + Element
		 if (Elements.Length > 0)
		 {
			 if (Elements[Elements.Length-1] != '+')
			 {
				 Elements += "+";
			 }
		 }
		 Elements += Element;
		 return Elements;
	 }
	 private void btnQuit_Click(object sender, EventArgs e)
	 {
		 Environment.Exit(1);
	 }
	 private void btnExport_Click(object sender, EventArgs e)
	 {
		 if (Status != enStatus.Play)
		 {
			 ExportFormulas();
		 }
	 }
	 public void ExportFormulas()
	 {
		 if (myData.Count <= 0)
		 {
			 // nothing to Export
			 return;
		 }
		 enStatus OldStatus = Status;
		 Status = enStatus.Export;
		 DisplayStatus();
		 try
		 {

			 string myFoundFormulas = JsonConvert.SerializeObject(myData);
			 System.IO.File.WriteAllText(@"C:\Words Woorden JSON\word2formulas.json", myFoundFormulas);
			 MessageBox.Show("Formulas are exported" , "Exporting formulas ");
		 }
		 catch (Exception ex)
		 {
			 MessageBox.Show("Something went wrong : " + ex.ToString(), "Exporting formulas ");
		 }
		 Status = OldStatus;
		 DisplayStatus();
		
	 }

}
}

Bijgevoegde Bestanden







Ook met taq C#, mendeljev parser Recursief, json voorzien

0 gebruiker(s) lezen dit onderwerp

0 lid(leden), 0 bezoeker(s), 0 anonieme gebruikers

Inloggen


Untitled 1

Met dank aan J├╝rgen voor de jarenlange inzet van visualbasic.be (anno dec 2000)
Met dank aan Mike en Ronneke voor de jarenlange inzet van vbib.be (anno dec 2010)
Met dank aan PascalBianca voor de jarenlange inzet van vbib.be (anno dec 2016)