| 1 |
# Soros Tutorial – Part Five 🚀 ... Go! |
| 2 |
# |
| 3 |
# We will complete the following number to English |
| 4 |
# number name conversion program, learning special |
| 5 |
# features of Soros programming language. |
| 6 |
|
| 7 |
0 zero |
| 8 |
1 one |
| 9 |
2 two |
| 10 |
3 three |
| 11 |
4 four |
| 12 |
5 five |
| 13 |
6 six |
| 14 |
7 seven |
| 15 |
8 eight |
| 16 |
9 nine |
| 17 |
10 ten |
| 18 |
11 eleven |
| 19 |
12 twelve |
| 20 |
13 thirteen |
| 21 |
|
| 22 |
# TASK 1 – Call me again! |
| 23 |
# |
| 24 |
# A Soros program is a single function, named $(). It’s |
| 25 |
# possible to call this function in the regex replacement |
| 26 |
# part with arbitrary text as an argument, and its return value |
| 27 |
# will be inserted in the regex replacement at the place of |
| 28 |
# the function call. |
| 29 |
# |
| 30 |
# In the following program line, the argument is “4”, |
| 31 |
# and function call “$(4)” is replaced with “four”, the |
| 32 |
# result of the call of function $ with the argument “4”: |
| 33 |
|
| 34 |
14 $(4)teen |
| 35 |
|
| 36 |
# Extend the line 16 this way! |
| 37 |
|
| 38 |
15 fifteen |
| 39 |
16 |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
# TASK 2 – Reference and call... |
| 44 |
# |
| 45 |
# We can use regex references in function arguments. |
| 46 |
# For example, we can describe 18 -> eighteen conversion |
| 47 |
# this way, too: |
| 48 |
|
| 49 |
1(8) $(\1)een |
| 50 |
|
| 51 |
# Using the regex pattern \d (matching arbitrary digits), |
| 52 |
# give a *single* program line for 17 -> seventeen and |
| 53 |
# 19 -> nineteen conversion! (With this, line 14 and 16 |
| 54 |
# will be unnecessary, too.) |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
# TASK 3 – Abbreviation of $(\n) is $n |
| 59 |
# |
| 60 |
# According to the title of this task, $1 means $(\1), |
| 61 |
# $2 means $(\2) etc. Rewrite the following program part to |
| 62 |
# use this abbreviated form! |
| 63 |
|
| 64 |
20 twenty |
| 65 |
2(\d) twenty-$(\1) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
# TASK 4 – Boundaries |
| 70 |
# |
| 71 |
# Soros programs have default automatic zero deletion in |
| 72 |
# its arguments, ie. $(1) or $(01) are equivalents. But |
| 73 |
# the following program line have a different problem for |
| 74 |
# the input number 100: after left zero deletion it still |
| 75 |
# remains a zero, resulting “one hundred zero” instead of |
| 76 |
# “one hundred”: |
| 77 |
|
| 78 |
1(\d\d) one hundred $1 |
| 79 |
|
| 80 |
# The solution is using boundary dependent patterns. |
| 81 |
# These patterns can always match at the first run of the |
| 82 |
# Soros program, but at the next recursive calls, |
| 83 |
# matching depends from the position of the function call: |
| 84 |
# |
| 85 |
# ^pattern only matches, if the function call is there at |
| 86 |
# the beginning of the replacement. Example: |
| 87 |
# 1(\d) $1teen |
| 88 |
# |
| 89 |
# pattern$ only matches, if the function call is there at |
| 90 |
# the end of the replacement. Example: |
| 91 |
# 2(\d) twenty-$(\1) |
| 92 |
# |
| 93 |
# ^pattern$ only matches, if the function call is there at |
| 94 |
# the beginning and the end of the replacement. |
| 95 |
# Example: |
| 96 |
# 1(\d) $(ordinal |$1) |
| 97 |
# |
| 98 |
# NOTE: “|” before or after function calls sets explicite |
| 99 |
# boundary position. |
| 100 |
# |
| 101 |
# Correct the bug “one hundred zero” by using the |
| 102 |
# correct boundary sign on the program line “0 zero”, also |
| 103 |
# create a rule for negative numbers, to handle |
| 104 |
# “-0” -> “minus zero” conversion correctly! |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
# TASK 5 – Deeper in recursion |
| 109 |
# |
| 110 |
# A recursive call can result further recursive calls, |
| 111 |
# allowing to break a complex problem into smaller parts, |
| 112 |
# or iterate a repeating task, as in the following |
| 113 |
# example. |
| 114 |
# |
| 115 |
# The first line of the following program uses the |
| 116 |
# boundary indicator $ to handle the originally 4-digit |
| 117 |
# or less numbers differently. What is the full program |
| 118 |
# doing with the other numbers? Try to answer, and |
| 119 |
# check it in a different IDE window! |
| 120 |
# |
| 121 |
# ^(\d{1,4})$ \1 |
| 122 |
# (\d+)(\d{3}) $1,\2 |
| 123 |
# .* \0 |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
# TASK 6 – Optional text, depending from the return value |
| 128 |
# |
| 129 |
# Previously, we had a different program line for 20 and one |
| 130 |
# for 21–29 , because English uses a hyphen between the |
| 131 |
# number names. Using brackets around the recursive calls, |
| 132 |
# we can skip the first program line. The hyphen is only |
| 133 |
# added to “thirty”, if the return value of the recursive |
| 134 |
# function calls is not empty. |
| 135 |
|
| 136 |
3(\d) thirty[-$(\1)] |
| 137 |
|
| 138 |
# Write a *single* program line to handle “sixty”, |
| 139 |
# “seventy” (60–79), and “ninety” (90–99)! |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
# TASK 7 – Language-dependent program lines |
| 144 |
# |
| 145 |
# Using [:lang-code:] in a comment sets a |
| 146 |
# language-dependent program line. For example, |
| 147 |
# German number to number name rules |
| 148 |
# |
| 149 |
# 3(\d) [$1und]dreissig # [:de-CH:] |
| 150 |
# 3(\d) [$1und]dreißig |
| 151 |
# |
| 152 |
# result “dreissig” for the input “30”, if the language |
| 153 |
# of the conversion is de-CH (Swiss Standard German), |
| 154 |
# otherwise “dreißig”. |
| 155 |
# |
| 156 |
# It’s possible to use arbitrary number language codes |
| 157 |
# in the same program line. |
| 158 |
# |
| 159 |
# In Australia, Great Britain, Ireland and New Zealand, |
| 160 |
# 101 is “one hundred and one”, in other English speaking |
| 161 |
# countries, like USA and Canada, 101 is “one hundred one”. |
| 162 |
# Using the language codes en-AU, en-GB, en-IE and en-NZ |
| 163 |
# and the previous conditional text, describe number 100–999 |
| 164 |
# in both variants! |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
# TASK 8 – More Functions |
| 169 |
# |
| 170 |
# In Soros language, there is only one real function, the $(), |
| 171 |
# but it’s possible to create function-like structures by using |
| 172 |
# prefixes. |
| 173 |
# |
| 174 |
# Numbertext.org language modules contain functions for |
| 175 |
# number to money conversion (“USD 2” -> “two U.S. dollars”), |
| 176 |
# number to ordinal name conversion (“2” -> “second”) and |
| 177 |
# ordinal-number conversion (“2” -> “2nd”). For example, |
| 178 |
# we can define ordinal numbers as follows: |
| 179 |
# |
| 180 |
# "ordinal 1" first |
| 181 |
# "ordinal 2" second |
| 182 |
# "ordinal 3" third |
| 183 |
# |
| 184 |
# According to the quotation marks, the regex patterns can |
| 185 |
# contain spaces, too. |
| 186 |
# |
| 187 |
# For the space separated prefixes, there is a default left |
| 188 |
# zero deletion, i.e. $(ordinal 1) is equivalent of |
| 189 |
# $(ordinal 001), so program line |
| 190 |
# |
| 191 |
# "ordinal 1(\d\d)" one hundred $(ordinal \1) |
| 192 |
# |
| 193 |
# works also for the input 101, 102 etc. correctly using |
| 194 |
# only the previous program lines. |
| 195 |
# |
| 196 |
# Other method of ordinal number conversion is to convert the |
| 197 |
# input to the default cardinal number names, and modifying |
| 198 |
# their text: |
| 199 |
# |
| 200 |
# "ordinal (\d+)" $(ordinal |$1) |
| 201 |
# "ordinal (.*)one" \1first |
| 202 |
# "ordinal (.*)two" \1second |
| 203 |
# |
| 204 |
# NOTE: As can be seen, it’s possible to embed function |
| 205 |
# calls within function calls, too. |
| 206 |
# |
| 207 |
# What can be the last program line of this function? |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
# TASK 9 – Prefix “macros” |
| 212 |
# |
| 213 |
# Prefix macros simplify prefix handling. We can define |
| 214 |
# the previous ordinal function as follows, too |
| 215 |
# (see “ordinal” between double equal signs): |
| 216 |
# |
| 217 |
# == ordinal == |
| 218 |
# |
| 219 |
# ^0 zeroth |
| 220 |
# 1 first |
| 221 |
# 2 second |
| 222 |
# 3 third |
| 223 |
# |
| 224 |
# because this will be expanded in the following form: |
| 225 |
# |
| 226 |
# "^ordinal 0" zeroth |
| 227 |
# "ordinal 1" first |
| 228 |
# "ordinal 2" second |
| 229 |
# "ordinal 3" third |
| 230 |
# |
| 231 |
# With the other method, converting cardinal number names, |
| 232 |
# and later to ordinal ones: |
| 233 |
# |
| 234 |
# == ordinal == |
| 235 |
# |
| 236 |
# (\d+) $(ordinal |$1) |
| 237 |
# (.*)one \1first |
| 238 |
# (.*)two \1second |
| 239 |
# ... |
| 240 |
# (.*) \1th |
| 241 |
# |
| 242 |
# How is this expanded? |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
# TASK 10 – Join Numbertext development! |
| 247 |
# |
| 248 |
# Your contribution will help LibreOffice and other free |
| 249 |
# softwares to support chapter numbering with number names, |
| 250 |
# converting money amounts to text (MONEYTEXT), creating |
| 251 |
# plural forms of money amounts and support better |
| 252 |
# title casing. |
| 253 |
# |
| 254 |
# Check and fix Soros code of your language on |
| 255 |
# http://NUMBERTEXT.org, or if there is no such module yet, |
| 256 |
# create it, based on the currency code of the |
| 257 |
# English or the more flexible Swedish (sv.sor) |
| 258 |
# modules! |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
SCROLL DOWN FOR SOLUTIONS |
| 263 |
⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩ |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
# SOLUTION – TASK 1–7 (for TASK 5, see next SOLUTION) |
| 273 |
# |
| 274 |
# Solutions marked as TASK 1–7 in comments: |
| 275 |
# |
| 276 |
# ^0 zero # TASK 4 |
| 277 |
# 1 one |
| 278 |
# 2 two |
| 279 |
# 3 three |
| 280 |
# 4 four |
| 281 |
# 5 five |
| 282 |
# 6 six |
| 283 |
# 7 seven |
| 284 |
# 8 eight |
| 285 |
# 9 nine |
| 286 |
# 10 ten |
| 287 |
# 11 eleven |
| 288 |
# 12 twelve |
| 289 |
# 13 thirteen |
| 290 |
# # 14 $(4)teen |
| 291 |
# 15 fifteen |
| 292 |
# # 16 $(6)teen # TASK 1 |
| 293 |
# 18 eighteen |
| 294 |
# 1(\d) $1teen # TASK 2 |
| 295 |
# 20 twenty |
| 296 |
# 2(\d) twenty-$1 # TASK 3 |
| 297 |
# 3(\d) thirty[-$1] |
| 298 |
# 4(\d) forty[-$1] |
| 299 |
# 5(\d) fifty[-$1] |
| 300 |
# 8(\d) eighty[-$1] |
| 301 |
# (\d)(\d) $1ty[-$2] # TASK 5 |
| 302 |
# |
| 303 |
# # separator function |
| 304 |
# :0+ # one million |
| 305 |
# :0*\d?\d " and " # one million and twenty-two |
| 306 |
# :\d+ ", " # one million, one thousand |
| 307 |
# |
| 308 |
# (\d)(\d\d) $1 hundred[ and $2] # one hundred and one [:en-AU:] [:en-GB:] [:en-IE:] [:en-NZ:] # TASK 6 |
| 309 |
# (\d)(\d\d) $1 hundred[ $2] # default: one hundred one # TASK 6 |
| 310 |
# (\d{1,2})([1-9]\d\d) $1 thousand[ $2] # ten thousand two hundred |
| 311 |
# (\d{1,3})(\d{3}) $1 thousand$(:\2)$2 # one hundred thousand, two hundred |
| 312 |
# (\d{1,3})(\d{6}) $1 million$(:\2)$2 |
| 313 |
# (\d{1,3})(\d{9}) $1 billion$(:\2)$2 |
| 314 |
# |
| 315 |
# # negative number |
| 316 |
# |
| 317 |
# [-−](\d+) negative |$1 # TASK 4 |
| 318 |
# |
| 319 |
# # decimals |
| 320 |
# |
| 321 |
# 0[.,] point |
| 322 |
# ([-−]?\d+)[.,] $1| point |
| 323 |
# ([-−]?\d+[.,]\d*)(\d) $1| |$2 |
| 324 |
# |
| 325 |
# # currency |
| 326 |
# |
| 327 |
# # unit/subunit singular/plural |
| 328 |
# |
| 329 |
# us:([^,]*),([^,]*),([^,]*),([^,]*) \1 |
| 330 |
# up:([^,]*),([^,]*),([^,]*),([^,]*) \2 |
| 331 |
# ss:([^,]*),([^,]*),([^,]*),([^,]*) \3 |
| 332 |
# sp:([^,]*),([^,]*),([^,]*),([^,]*) \4 |
| 333 |
# |
| 334 |
# AUD:(\D+) $(\1: Australian dollar, Australian dollars, cent, cents) |
| 335 |
# CAD:(\D+) $(\1: Canadian dollar, Canadian dollars, cent, cents) |
| 336 |
# CHF:(\D+) $(\1: Swiss franc, Swiss francs, centime, centimes) |
| 337 |
# CNY:(\D+) $(\1: Chinese yuan, Chinese yuan, fen, fen) |
| 338 |
# EUR:(\D+) $(\1: euro, euro, cent, cents) |
| 339 |
# GBP:(\D+) $(\1: pound sterling, pounds sterling, penny, pence) |
| 340 |
# JPY:(\D+) $(\1: Japanese yen, Japanese yen, sen, sen) |
| 341 |
# USD:(\D+) $(\1: U.S. dollar, U.S. dollars, cent, cents) |
| 342 |
# |
| 343 |
# "(JPY [-−]?\d+)[.,](\d\d)0" $1 |
| 344 |
# "(JPY [-−]?\d+[.,]\d\d)(\d)" $1 $2 rin |
| 345 |
# |
| 346 |
# "([A-Z]{3}) ([-−]?1)([.,]00?)?" $2$(\1:us) |
| 347 |
# "([A-Z]{3}) ([-−]?\d+)([.,]00?)?" $2$(\1:up) |
| 348 |
# |
| 349 |
# "(CNY [-−]?\d+)[.,](\d)0?" $1 $2 jiao |
| 350 |
# "(CNY [-−]?\d+[.,]\d)(\d)" $1 $2 fen |
| 351 |
# |
| 352 |
# "(([A-Z]{3}) [-−]?\d+)[.,](01)" $1 and |$(1)$(\2:ss) |
| 353 |
# "(([A-Z]{3}) [-−]?\d+)[.,](\d)" $1 and |$(\030)$(\2:sp) |
| 354 |
# "(([A-Z]{3}) [-−]?\d+)[.,](\d\d)" $1 and |$3$(\2:sp) |
| 355 |
# |
| 356 |
# == ordinal == |
| 357 |
# |
| 358 |
# # convert to text, and recall to convert |
| 359 |
# # cardinal names to ordinal ones |
| 360 |
# |
| 361 |
# ([-−]?\d+) $(ordinal |$1) |
| 362 |
# |
| 363 |
# (.*)one \1first |
| 364 |
# (.*)two \1second |
| 365 |
# (.*)three \1third |
| 366 |
# (.*)five \1fifth |
| 367 |
# (.*)eight \1eighth |
| 368 |
# (.*)nine \1ninth |
| 369 |
# (.*)twelve \1twelfth |
| 370 |
# (.*)y \1ieth |
| 371 |
# (.*) \1th # TASK 7 |
| 372 |
# |
| 373 |
# == ordinal-number == |
| 374 |
# |
| 375 |
# (.*1\d) \1th |
| 376 |
# (.*1) \1st |
| 377 |
# (.*2) \1nd |
| 378 |
# (.*3) \1rd |
| 379 |
# (.*) \1th |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
# SOLUTION – TASK 5 |
| 384 |
# |
| 385 |
# This is a thousand separator |
| 386 |
# program extended for all numbers. |
| 387 |
# The middle line put a comma before |
| 388 |
# the last three digits, and do the |
| 389 |
# same for the remaining digits using |
| 390 |
# a recursive call, and in that a |
| 391 |
# next recursive call etc. |
| 392 |
# |
| 393 |
# ^(\d{1,4})$ \1 |
| 394 |
# (\d+)(\d{3}) $1,\2 |
| 395 |
# .* \0 |
| 396 |
# |
| 397 |
# For example, for the input “10000000” |
| 398 |
# the inner states of the calls: |
| 399 |
# |
| 400 |
# First call: $(10000),000 |
| 401 |
# Second call: $(10),000 |
| 402 |
# Third call: 10 |
| 403 |
# |
| 404 |
# resulting “10,000,000” in the end of |
| 405 |
# all returns. |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
# SOLUTION – TASK 9 |
| 410 |
# |
| 411 |
# As before in TASK 8: |
| 412 |
# |
| 413 |
# "ordinal (\d+)" $(ordinal |$1) |
| 414 |
# "ordinal (.*)one" \1first |
| 415 |
# "ordinal (.*)two" \1second |
| 416 |
# ... |
| 417 |
# "ordinal (.*)" \1th |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
####################################################### |
| 422 |
# CONGRATULATIONS – You have finished Tutorial Five! 🙂 |
| 423 |
####################################################### |