Skip to content

Commit e3519f0

Browse files
Create ChatScript-Coding-Standards.md
1 parent 619c7c9 commit e3519f0

File tree

1 file changed

+267
-0
lines changed

1 file changed

+267
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
2+
# ChatScript Coding Standards
3+
© Bruce Wilcox, gowilcox@gmail.com www.brilligunderstanding.com
4+
<br>Revision 9/24/2017 cs7.55
5+
6+
Contents:
7+
8+
* [Proper indentation](ChatScript-Coding-Standards.md#Proper-indentation)
9+
* [Rule Labels](ChatScript-Coding-Standards.md#Rule-Labels)
10+
* [Sample inputs](ChatScript-Coding-Standards.md#Sample-inputs)
11+
* [Easy to read patterns](ChatScript-Coding-Standards.md#Easy-to-read-patterns)
12+
* [Easy to read output](ChatScript-Coding-Standards.md#Easy-to-read-output)
13+
* [Related rules bundles](ChatScript-Coding-Standards.md#Related-rules-bundles)
14+
* [Localized Concepts and functions](ChatScript-Coding-Standards.md#Localized-Concepts-and-functions)
15+
* [Keyword casing and Misspellings](ChatScript-Coding-Standards.md#Keyword-casing-and-Misspellings)
16+
17+
18+
*Rationale*:<br>
19+
Coding Standards allow you and others to understand your code.
20+
They can also allow others who are not programmers to view an abstract of
21+
your code, generated by CS’s ` :abstract ` ability.
22+
23+
To use `:abstract `, merely login locally with a new user name (or erase contents
24+
of USERS), then type `:abstract ~mytopic ` for a single topic. The abstract will
25+
whiz by on screen but is captured in the user log. Just rename that log file and
26+
move it somewhere else.
27+
28+
## Indentation of rules
29+
30+
* Do not indent top level rules.
31+
* Indent rejoinders per level.
32+
* Leave blank line before top level rule.
33+
34+
Examples:
35+
```
36+
t: How are you
37+
a: (~goodness) glad to hear it.
38+
b: (cool) not so cool.
39+
a: (~badness) sorry to hear it
40+
b: (cool) not so bad. # bad - wrong indentation level
41+
42+
u: (how are you) I am good.
43+
u: (and then) xxx # this is bad - jammed immediately after top level rule
44+
45+
u: (why not) because. # this is bad indentation of top level rule
46+
```
47+
48+
*Rationale*:<br>
49+
On one hand you want to minimize useless space on a page.
50+
So top-level rules should not be indented (nor should their sample inputs).
51+
52+
On the other hand you want to easily see the structure of the code, so properly
53+
indenting rejoinders makes structure clear. And you want to easily spot where
54+
one rule chunk ends and another begins. So separating top level rules and keep
55+
rejoinders together is what I do.
56+
57+
58+
## Rule Labels
59+
60+
* Make meaningful labels using capital letters.
61+
* Label all rules that have their own output text.
62+
63+
Examples:
64+
65+
```
66+
t: ASK_FOR_EMAIL () What is your email address?
67+
a: EMAIL_GIVEN (~email_url) Thank you.
68+
a: () ^reuse(EMAIL_GIVEN)
69+
70+
t: B55() What is your email address? # bad- meaningless label
71+
```
72+
73+
*Rationale*:<br>
74+
Using capital letters for rule labels (both at the rule and in places that
75+
`^reuse` the rule) make it easy to see them in a mass of text like a `:trace`.
76+
77+
If yours is not your own private bot project then every rule that generates output should
78+
have such a label, which will appear in an abstract.
79+
This allows others to refer to your rule and maybe find it in log files of customers.
80+
81+
82+
## Easy-to-read patterns
83+
84+
* Avoid superfluous parentheses.
85+
* For multiple patterns, put each on its own line.
86+
87+
Examples:
88+
89+
```
90+
?: WHAT_BRILLIG_DOES([
91+
(what * “Brillig Understanding” )
92+
([tell talk know] *~2 about “Brillig Understanding”)
93+
])
94+
Brillig Understanding makes natural language systems.
95+
96+
u: BAD_CHOICES ([
97+
(<< Brillig company >> ) # superfluous parens
98+
(~mywords) # superfluous parens
99+
([ word1 word2]) # superfluous parens
100+
])
101+
```
102+
103+
*Rationale*:<br>
104+
Superfluous parens make patterns harder to read, so harder to
105+
visually confirm they are correct. They also slow down the CS engine, but not
106+
enough to matter.
107+
108+
When you are using multiple patterns in a rule, putting each
109+
on its own line allows you (or code reviewers) to comprehend each one separately.
110+
111+
112+
## Give sample inputs
113+
114+
* Give sample inputs for responders and rejoinders.
115+
* Give multiple samples for wildly different sentence constructions when you have multiple patterns in a rule.
116+
117+
Examples:
118+
```
119+
t: What year is it?
120+
#! 1993
121+
a: (~year) Great.
122+
123+
#! What is your name
124+
#! Who are you?
125+
?: ([
126+
(‘you [name called])
127+
(who be ‘you)
128+
])
129+
My name is Rose.
130+
```
131+
132+
*Rationale*:<br>
133+
Sample inputs explain your patterns. Instead of having to interpret the
134+
pattern, you know immediately what the rule is intending to do.
135+
136+
Sample inputs allow you to use `:abstract` to give non-programmers an overview of your code.
137+
Sample inputs also allow CS to unit-test your code for you using `:verify`.
138+
139+
140+
## Easy to read rule output
141+
142+
Indent each sentence and each CS code statement on separate lines.
143+
144+
Examples:
145+
146+
```
147+
#! Test
148+
u: (test)
149+
$status += 1
150+
$_x = ^compute(1 + 2)
151+
The answer is $_x.
152+
Did you miss it?
153+
154+
#! Where were you born
155+
u: (where * you * born) # bad- harder to read
156+
I was born in San Francisco near the old church on the hill. I was born an
157+
only child but my parents always wanted more.
158+
159+
#! Where were you born
160+
u: (where * you * born) # good
161+
I was born in San Francisco near the old church on the hill.
162+
I was born an only child but my parents always wanted more.
163+
```
164+
165+
*Rationale*:<br>
166+
Multiple sentence user output is harder to read if on the same line
167+
since they will tend to run into very long lines.
168+
169+
Code is certainly harder to understand when multiple actions are on the same line.
170+
On the other hand if the output is tiny, you might put it on a single line like this:
171+
172+
u: (test) OK. $status += 1
173+
174+
175+
176+
## Bundle related rules
177+
178+
* Put related rules in topics
179+
* Put more closely related rules together when you can.
180+
181+
Examples:
182+
183+
```
184+
topic: ~aliens()
185+
...
186+
187+
topic: ~family()
188+
189+
#!x*** Parents
190+
191+
#! Who is your mother
192+
?: ( who * mother) Mom.
193+
194+
#! What does your mother do?
195+
?: ( what * mother * do) She works.
196+
197+
#! Who is your father # note we had all mother stuff together before father
198+
?: ( who * father) Dad.
199+
200+
#! What does your father do?
201+
?: ( what * father * do) He works.
202+
#!x*** Siblings
203+
...
204+
```
205+
206+
*Rationale*:<br>
207+
With appropriate bundling you should immediately know where to
208+
add a new rule or discover that you have multiple rules doing the same job.
209+
210+
I even annotate clusters of rules for `:abstract` using `#!x` comments which will
211+
reflect into the abstract to label the cluster and make it easy for people to follow
212+
along. The only problem may be when some earlier rule blocks a later one and
213+
you need to move something around. Consider using `!` keywords as instead.
214+
215+
216+
## Concept and Function localization
217+
218+
* If a concept or function is only used in 1 file, put it into that file.
219+
* If a concept or function is used in multiple places, use a global functions.top or concepts.top.
220+
* For tables used only used in 1 file, depends on size of table.
221+
222+
*Rationale*:<br>
223+
When you can localize data to the one file that uses it,
224+
it makes it easier to find the definition when you want to inspect it.
225+
226+
227+
## Keyword Casing and Misspellings
228+
229+
Use standard dictionary casing (or product owner ‘s casing in the case of new
230+
non-words in the language) for keywords in concepts or patterns.
231+
232+
Avoid putting misspellings in concepts or keywords. Use replace: in your
233+
scripts file to effectively add additions to the predefined substitutions files that
234+
come with ChatScript.
235+
236+
But use `:replace` only when the misspell has only one possible obvious value.
237+
`replace:` changes the dictionary for ALL BOTS.!
238+
For misspellings which are legal words, add them to concepts or keywords.
239+
240+
Examples:
241+
```
242+
concept: ~protocols (Blu-ray WiFi)
243+
replace: blue_ray Blu-ray # true always. Do this as the correct spelling
244+
replace: MS Microsoft # bad - computers true, medical not
245+
246+
#! I used my brakes
247+
u: ([brake break]) Don’t hit the brakes too hard. # misspell is a real word, list it
248+
249+
#! I like mars
250+
u: (_~planets) ‘_0 # outputs Mars even though user typed mars
251+
252+
#! Do you like sheep?
253+
u: ([sheep shep sheap]) I like sheep # generally bad to do this
254+
```
255+
256+
*Rationale*:<br>
257+
If a word exists with only one casing in the dictionary, CS can
258+
automatically adjust the input to make it correct.
259+
Even if a word exists in multiple cases, if a concept set has it in only one casing
260+
(e.g. `~planets` has the noun Mars and not the verb mars), if you memorize it,
261+
CS can memorize the correct casing.
262+
263+
Sometimes you expect some user typos. Putting the illegal
264+
forms in your patterns (like shep), means they enter the dictionary as acceptable
265+
words. This means spelling correction can change “misspellings” of
266+
misspellings, causing word drift for an input like “hep”.
267+
Prefer to use `replace:` if a misspelling can only go to one obvious place.

0 commit comments

Comments
 (0)