DISCLAIMER: This video is boring to the average YouTube viewer.
Is translating Latin texts in school fun for everyone? Which words are the most words in the text? We're building an app that shows that.
We'll build the user view first. It is built on the form with a "few" components (ToolBar, Button, SpinBox, CheckBox, Layout, Splitter, Label, Memo, OpenDialog).
Then we create the method for opening files. We use OpenDialog for this. When the user has selected a file, we check whether it really exists. If so, we will load this file into the memo on the left. Because the text in the Latinwords.txt file is encoded with UTF8, we use TEncoding.UTF8 for the LoadFromFile method.
Another button should help to insert data from the clipboard into the left memo. To do this, we use the PasteFromClipboard method from the Memo component.
We have another button to create the word list. Working hours are to be bridged with an indicator. We will then put the text from the memo in a string (S). Now we can read any character C (Char) from S (String). Here we're just checking, is the character in the range from A to Z or a to z? So there would be a k, but not a question mark, for example. If it is a sign, we assemble a word, piece by piece. If it is not a sign, then we have already finished with the first word. So the word is there because D is not empty. Check if the word is already in the word list. If no, IndexOf returns the index of the word from the list, then the result is -1. Okay, then we can add the word in string D to the list. If it is there we have to do the counting. StringList has an AddObject method. This can be used to put a string and an object together. And you can call the object directly. Let's go briefly to this object.
When counting values, we can use an object that we write to ourselves as a class. It should have a memory for the number, it should start with the value 1. Then it should simply increase this number by one using the Add method, i.e. +1. And it should have a function to output the number formatted as a string (with leading zeros). We call this class TCheckNr.
Back to the list. When we add a word to the list, we use AddObject. To do this, we create the object in which we create this. Then the word and the object are added to the list together. If we already have the word in the list, we only have to call the associated object. And then use the Add method from TCheckNr.
With our own ChangeList method, we will evaluate the data from the WordList. We can now filter and format.
Tutorial: Latein Wörter zählen mit FireMonkey (Delphi DX 10.4 Sydney) auf Windows und OSX
Latein Texte zu übersetzen in der Schule macht jedem Spass? Welche Wörter sind die meisten Wörter im Text? Wir bauen eine App, die das zeigt.
Wir bauen zuerst die Benutzer Ansicht. Das ist mit ein "paar" Komponenten (ToolBar, Button, SpinBox, CheckBox, Layout, Splitter, Label, Memo, OpenDialog) auf der Form zusammen gebaut.
Dann erstellen wir die Methode zum öffnen von Dateien. Dafür nutzen wir OpenDialog. Wenn der Nutzer eine Datei ausgewählt hat, prüfen wir ob diese wirklich vorhanden ist. Wenn Ja, laden wir diese Datei in das Memo auf der linken Seite. Weil der Text in der Datei Latinwords.txt mit UTF8 kodiert ist, nutzen wir bei der LoadFromFile Methode hier TEncoding.UTF8.
Ein weiter Button soll helfen, Daten aus der Zwischenablage in das linke Memo einzufügen. Dazu nutzen wir die Methode PasteFromClipboard von der Memo Komponente.
Zum erstellen der Wort Liste haben wir einen weiteren Button. Mit einem AniIndicator soll die Arbeitszeit überbrückt werden. Wir werden den Text aus dem Memo dann in einen String (S) geben. Nun können wir jedes Zeichen C (Char) aus S (String) lesen. Hier prüfen wir nur, ist das Zeichen im Bereich von A bis Z oder a bis z? Ein k wäre also drin, aber ein Fragezeichen zum Beispiel nicht. Ist es ein Zeichen, so bauen wir ein Wort zusammen, Stück für Stück. Ist es kein Zeichen, dann sind wir schon fertig mit dem ersten Wort. Also ist das Wort vorhanden, weil D nicht Leer ist. Prüfe ob das Wort schon in der Wortliste enthalten ist. Wenn nein, IndexOf gibt den Index des Wortes aus der Liste zurück, dann ist das Ergebnis -1. Okay, dann können wir das Wort in String D in der Liste aufnehmen. Wenn es vorhanden ist, müssen wir das Zählen. StringList hat eine Methode AddObject. Damit kann man einen String und ein Objekt zusammen legen. Und man kann das Objekt direkt aufrufen. Gehen wir kurz zu diesem Objekt.
Beim Zählen von Werten können wir ein Object nutzen, das wir uns selbst als Klasse schreiben. Es soll einen Speicher für die Zahl haben, starten soll es mit dem Wert 1. Dann soll es mit der Methode Add einfach diese Zahl um eins erhöhen, also +1. Und es soll eine Funktion haben, um die Zahl formatiert auszugeben als String (mit vorstehenden Nullen). Diese Klasse nennen wir hier TCheckNr.
#FireMonkey #Delphi