Posted to tcl by hypnotoad at Mon Oct 22 18:16:29 GMT 2018view raw

  1. package provide pheme_random 0.1
  2. #package require pheme
  3. namespace eval ::pheme {}
  4. namespace eval ::randomword {}
  5.  
  6. # Adapted from https://www.dwheeler.com/totro.pl.txt
  7.  
  8. namespace eval ::randomword {}
  9.  
  10. proc ::randomword::lrandom list {
  11. set len [llength $list]
  12. set idx [expr int(rand()*$len)]
  13. return [lindex $list $idx]
  14. }
  15.  
  16. proc ::randomword::lshuffle list {
  17. set len [llength $list]
  18. set idx [expr int(rand()*$len)]
  19. return [lindex $list $idx]
  20. }
  21.  
  22. proc ::randomword::table {pattern type table} {
  23. foreach {letter frequency rule} $table {
  24. for {set x 0} {$x < $frequency} {incr x} {
  25. dict set cards [incr cardnum] [list $letter $rule]
  26. }
  27. }
  28. set body {}
  29. append body \n " " [list set cards $cards]
  30. append body {
  31. set srule [yield [info coroutine]]
  32. while 1 {
  33. set idxlist [dict keys $cards]
  34. set N [llength $idxlist]
  35. set order {}
  36. foreach id $idxlist {
  37. set idx [expr {int(floor(rand()*[llength $order]))}]
  38. set order [::linsert $order $idx $id]
  39. }
  40. foreach id $order {
  41. lassign [dict get $cards $id] letter lrule
  42. #puts [list $letter $lrule | $srule] ;
  43. if {$srule && ($srule & $lrule)==0} { continue }
  44. set srule [yield $letter]
  45. }
  46. if {$srule & 1} { yield {} }
  47.  
  48. }
  49. }
  50. coroutine ::randomword::${pattern}_$type apply [list {} $body]
  51. }
  52.  
  53. ###
  54. # Syllable Rules
  55. #
  56. # 1 - Can be at ending
  57. # 2 - Can be at beginning
  58. # 4 - Can be in the middle
  59. # 8 - "strong" syllable
  60. # 16 - "sonerant" syllable
  61. ###
  62.  
  63. ###
  64. # Build a table of vowels for generic words
  65. ###
  66. ::randomword::table generic vowel {
  67. a 12 7
  68. e 12 7
  69. i 12 7
  70. o 12 7
  71. u 12 7
  72. ae 1 7
  73. ai 1 7
  74. ao 1 7
  75. aa 1 7
  76. ay 2 7
  77. ea 1 7
  78. ei 3 7
  79. eo 1 7
  80. eu 1 7
  81. ee 1 7
  82. eau 1 7
  83. ia 1 7
  84. io 1 7
  85. iu 1 7
  86. ii 1 7
  87. oa 1 7
  88. oe 1 7
  89. oi 1 7
  90. ou 1 7
  91. oo 1 7
  92. ua 2 7
  93. ' 1 4
  94. y 1 7
  95. }
  96.  
  97. ###
  98. # Build a table of consonants for generic words
  99. ###
  100. ::randomword::table generic consonant {
  101. b 3 7
  102. br 2 6
  103. c 3 7
  104. ch 1 7
  105. ck 1 5
  106. cl 1 6
  107. cr 1 6
  108. d 3 7
  109. df 1 6
  110. dr 2 7
  111. f 3 7
  112. fl 1 6
  113. fr 2 6
  114. g 3 7
  115. gh 1 7
  116. gl 1 6
  117. gr 2 6
  118. h 3 7
  119. j 3 7
  120. k 3 7
  121. kl 1 6
  122. kr 2 6
  123. l 3 7
  124. ll 1 6
  125. m 3 7
  126. n 3 7
  127. nk 1 5
  128. p 3 7
  129. ph 1 7
  130. pl 1 6
  131. pr 1 6
  132. qu 1 6
  133. r 3 7
  134. rk 1 5
  135. s 3 7
  136. sc 1 7
  137. sh 1 7
  138. sk 1 7
  139. sl 1 6
  140. sr 1 6
  141. ss 1 5
  142. st 1 7
  143. str 1 6
  144. t 3 7
  145. th 1 7
  146. tr 1 6
  147. v 3 7
  148. w 3 7
  149. wh 1 6
  150. wk 1 0
  151. x 1 7
  152. y 1 7
  153. }
  154.  
  155. ###
  156. # Build a table of vowels for female words
  157. ###
  158. ###
  159. # Syllable Rules
  160. #
  161. # 1 - Can be at ending
  162. # 2 - Can be at beginning
  163. # 4 - Can be in the middle
  164. # 8 - "strong" syllable
  165. # 16 - "sonerant" syllable
  166. ###
  167.  
  168. ::randomword::table female vowel {
  169. a 12 7
  170. e 12 7
  171. i 12 7
  172. o 12 6
  173. u 12 6
  174. ae 1 6
  175. ai 1 7
  176. ao 1 6
  177. aa 1 7
  178. ah 6 7
  179. ay 2 7
  180. ea 1 6
  181. ei 3 7
  182. eo 1 6
  183. eu 1 6
  184. ee 1 7
  185. eau 1 6
  186. ia 1 7
  187. io 1 6
  188. iu 1 6
  189. ii 1 7
  190. oa 1 6
  191. oe 1 6
  192. oi 1 6
  193. ou 1 6
  194. oo 1 6
  195. ua 2 6
  196. ' 1 4
  197. y 1 7
  198. }
  199.  
  200. ::randomword::table female consonant {
  201. b 3 6
  202. br 2 6
  203. c 3 6
  204. ch 1 6
  205. ck 1 5
  206. cl 1 6
  207. cr 1 6
  208. d 3 6
  209. df 1 6
  210. dr 2 6
  211. f 3 6
  212. fl 1 6
  213. fr 2 6
  214. g 3 6
  215. gh 1 7
  216. gl 1 6
  217. gr 2 6
  218. h 3 6
  219. j 3 6
  220. k 3 6
  221. kl 1 6
  222. kr 2 6
  223. l 3 7
  224. ll 1 7
  225. m 3 7
  226. n 3 7
  227. nk 1 5
  228. ng 1 6
  229. p 3 6
  230. ph 1 7
  231. pl 1 6
  232. pr 1 6
  233. qu 1 6
  234. r 3 7
  235. rk 1 5
  236. s 3 6
  237. sc 1 6
  238. sh 1 6
  239. sk 1 6
  240. sl 1 6
  241. sr 1 6
  242. ss 1 5
  243. st 1 6
  244. str 1 6
  245. t 3 6
  246. th 1 6
  247. tr 1 6
  248. v 3 6
  249. w 3 6
  250. wh 1 6
  251. wk 1 0
  252. x 1 6
  253. y 1 7
  254. }
  255.  
  256. ###
  257. # Build a table of vowels for male words
  258. ###
  259. ::randomword::table male vowel {
  260. a 12 7
  261. e 12 7
  262. i 12 7
  263. o 12 7
  264. u 12 7
  265. ae 1 7
  266. ai 1 7
  267. ao 1 7
  268. aa 1 7
  269. ay 2 7
  270. ea 1 7
  271. ei 3 7
  272. eo 1 7
  273. eu 1 7
  274. ee 1 7
  275. eau 1 7
  276. ia 1 7
  277. io 1 7
  278. iu 1 7
  279. ii 1 7
  280. oa 1 7
  281. oe 1 7
  282. oi 1 7
  283. ou 1 7
  284. oo 1 7
  285. ua 2 7
  286. ' 1 4
  287. y 1 7
  288. }
  289.  
  290. ::randomword::table male consonant {
  291. b 3 7
  292. br 2 6
  293. c 3 7
  294. ch 1 7
  295. ck 1 5
  296. cl 1 6
  297. cr 1 6
  298. d 3 7
  299. df 1 6
  300. dr 2 7
  301. f 3 7
  302. fl 1 6
  303. fr 2 6
  304. g 3 7
  305. gh 1 7
  306. gl 1 6
  307. gr 2 6
  308. h 3 7
  309. j 3 7
  310. k 3 7
  311. kl 1 6
  312. kr 2 6
  313. l 3 7
  314. ll 1 6
  315. m 3 7
  316. n 3 7
  317. nk 1 5
  318. p 3 7
  319. ph 1 7
  320. pl 1 6
  321. pr 1 6
  322. qu 1 6
  323. r 3 7
  324. rk 1 5
  325. s 3 7
  326. sc 1 7
  327. sh 1 7
  328. sk 1 7
  329. sl 1 6
  330. sr 1 6
  331. ss 1 5
  332. st 1 7
  333. str 1 6
  334. t 3 7
  335. th 1 7
  336. tr 1 6
  337. v 3 7
  338. w 3 7
  339. wh 1 6
  340. wk 1 0
  341. x 1 7
  342. y 1 7
  343. }
  344.  
  345. proc ::randomword::word {{pattern generic} {length 0}} {
  346. if {$length <=0 } {
  347. set length [expr {3+int(rand()*10)}]
  348. }
  349. set vowel [expr {rand()>0.5}]
  350. set word {}
  351. for {set i 0} {$i < $length} {incr i} {
  352. if {$i==0} {
  353. # Pattern cannot be at the start of a word
  354. set srule 2
  355. } elseif {$i==($length-1)} {
  356. # Pattern cannot be at the end of a word
  357. set srule 1
  358. } else {
  359. # Pattern cannot be in the middle of a word
  360. set srule 4
  361. }
  362. if {$vowel} {
  363. set chars [${pattern}_vowel $srule]
  364. #[lrandom $::randomword::vowels($pattern)]
  365. } else {
  366. set chars [${pattern}_consonant $srule]
  367. #set letter [lrandom $::randomword::consonants($pattern)]
  368. }
  369. append word $chars
  370. set vowel [expr {$vowel ^ 1}]
  371. }
  372. return $word
  373. }
  374.