Text Spinner

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

Problem Description

Text 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 & Techniques

In 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
applied to the X phrase set will be applied the the referencing phase set. To allow for the single phrase set, I also now recognize a phrase consisting of a the lower case letter "b" as a place holder for "no phrase to be inserted" condition. Referencing is indicated by an &X symbol + character following the opening { bracket. So a revised spin might look like "{%1Not|b} A {&1but|and also} B." which could produce either sentence "Not A but B." or "A and also B."
 

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

???
   

 

Original:  March 4, 2010

Modified:  February 18, 2016

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.