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