[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Problem DescriptionText spinning is a technique for randomly varying text based on user defined phrase choices embedded within the text. This demo program was suggested by a user and is presented here more as a Delphi coding exercise than as a program with any practical use. Background & TechniquesIn this implementation, "Phrase sets" of alternative phrases are defined within pairs of curly brackets, {}. Inside the brackets, alternative phrases to be randomly selected are separated by a vertical line character, |. If a phrase set contains only a single phrase, it will be randomly included or omitted on a 50-50 basis. An example is included. Version 2 adds handling of the awkward result of the "{Not} A but B"
structure of the Version 1 included sample which could produce "A but B".
The syntax now allows %X, immediately following the { start symbol
where X is any single letter or digit to uniquely identify this phrase
set. This identifying symbol may be referenced in a later phrase set and the
same index that Non-programmers are welcome to read on, but may want to jump to bottom of this page to download the executable program now. Programmer's Notes:There may be a a quasi-standard for defining how to spin text, but I couldn't locate any, so the first task was to define one. The viewer who submitted the program request used the curly brackets and vertical line conventions which seemed good because those symbols to not commonly appear in general text. I added the other Version 2 extensions with the same considerations. The sample spin text or other spin text provided by the user is enter into a TMemo control which uses a TStrings object to hold lines of text. Since output text length will be not match the input, it seemed best to convert the input to a single string, convert it into a single output string and let the Memo2 output TMemo break the text into lines as necessary. I use the Posex function to scan the input for the {|}.and |symbols. Posex has the advantage over the older Pos function because we can specify a point to start the search for the specified character. By updating the Start field from the previous N and scanning for the location, N, of the next desired character we can step through the text in a systematic manner. Using Posex requires adding StrUtils to the Uses clause of the unit. The logic is to copy the unchanged text from the beginning of the text or the previous } symbol to the next following { or the end of the text string. If { is found, scan to make sure that there is a closing } symbol and then build an array of phrases (Phrases) contained within these brackets, the end of each phrase being marked by the | symbol or the closing } symbol. We use the Random function to choose one to add to the output text line. The %X and &X feature is implementing by creating an entry in the string list SpinSets when the %X is found. The string added is the X character plus a string version of the random PhraseIndex used for this phrase set. When &X is found at the beginning of a following phrase set, we scan SpinSets looking for the X character and reconvert the phrase index embedded in the entry for use with this phrase set. Running/Exploring the Program
Suggestions for Further Explorations
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |