Calyx 0.4.1
dotnet add package Calyx --version 0.4.1
NuGet\Install-Package Calyx -Version 0.4.1
<PackageReference Include="Calyx" Version="0.4.1" />
<PackageVersion Include="Calyx" Version="0.4.1" />
<PackageReference Include="Calyx" />
paket add Calyx --version 0.4.1
#r "nuget: Calyx, 0.4.1"
#:package Calyx@0.4.1
#addin nuget:?package=Calyx&version=0.4.1
#tool nuget:?package=Calyx&version=0.4.1
Calyx
Generative text processing for C# and Unity applications.
Build
dotnet build
Tests
dotnet test
Install
Unity
Create a new Unity project or open an existing project.
In the Assets
directory of your project, create a new directory called Libraries
(or adapt to suit your preferred style of organising).
Download Calyx.dll
from the compiled resources under Build/Release on eng-git.
Move Calyx.dll
into the Assets/Libraries
path of your Unity project.
Create a new C# Script in your Unity project and assign it to a new empty game object (or add it to an existing game object in your scene).
Add the following grammar script to the Awake()
or Start()
hook of your script.
using UnityEngine;
using Calyx;
public class TextSample : MonoBehaviour
{
void Start()
{
Grammar hello = new Grammar(P => {
P.Start("Hello from Unity");
});
Debug.Log(hello.Generate().text);
}
}
Run the scene in the Unity editor and you should see the phrase Hello from Unity
appear in the console.
Usage
Configuring the PRNG
Requires an instance of System.Random
not the static class UnityEngine.Random
.
If you have a predefined integer to use as a seed value, pass it in using the Seed
option:
int aSeed = 72365472659210;
Grammar grammar = new Grammar(Seed: aSeed);
You can also pass in an instance of Random
with the Rng
option.
int bSeed = 82673452385460;
Grammar grammar = new Grammar(
Rng: new System.Random(bSeed)
);
Defining grammar rules
Use the object delegate constructor to return a preconfigured instance with all the rules defined:
Grammar plums = new Grammar(P => {
P.Start("I have {eaten} the {plums}");
P.Rule("eaten", new[] { "eaten", "taken", "devoured" });
P.Rule("plums", new[] { "plums", "apricots", "pears" });
});
Rule definitions support fluent interface conventions, so can also be constructed as a single statement:
Grammar plums = new Grammar(P => {
P.Start("I have {eaten} the {plums}")
.Rule("eaten", new[] { "eaten", "taken", "devoured" })
.Rule("plums", new[] { "plums", "apricots", "pears" });
});
The Start
and Rule
methods are overloaded, supporting both arrays of strings, and single strings:
Grammar plums = new Grammar(P => {
P.Rule("single", "I have eaten the plums")
.Rule("alternate", new[] {
"I have eaten the plums",
"That were in the icebox"
});
});
Generating Results
Call Generate
on the grammar instance to get a result object from a unique run of the grammar and use the Text
property of the result to access the generated text.
Result result = plums.Generate();
Console.WriteLine(result.Text);
By default, grammars will generate top-down from the start
rule. To start from an alternative rule in the grammar, pass the start symbol as a string to Generate
:
Result result = plums.Generate("alternate");
Console.WriteLine(result.Text);
To append template context rules at runtime, you can pass a transient block of rules to the grammar as part of the call to Generate
. This can be useful if you want to mix data from different life cycles of your program state but (presumably—we haven’t performance tested this yet!) will make grammar generation slower though in most cases shouldn’t be noticeable.
Result result = plums.Generate(new Dictionary<string, string[]>() {
["single"] = new[] { "I have eaten the plums" }
["alternate"] = {
"I have eaten the plums",
"That were in the icebox"
}
});
Console.WriteLine(result.Text);
Other Features
Not all the features are ported over yet and the template syntax is not yet documented but you can read about the general format and structure of the language design in the original SYNTAX document.
Roadmap
Rough plan for stabilising the API and features for a 1.0
release.
Version | Features planned |
---|---|
0.1 |
|
0.2 |
|
0.3 |
|
0.4 |
|
0.5 |
|
0.6 |
|
0.7 |
affix tree productions, definition parser and expression syntax |
0.8 |
error handling and stack trace diagnostics |
0.9 |
Unity JSON affordances and file format integration |
0.10 |
Parity check with JavaScript and Ruby engines |
0.11 |
API documentation and code comments |
0.12 |
NuGet and Unity package manager nonsense |
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.