Good progress in nim
This commit is contained in:
parent
3219555785
commit
2a4a29c051
138
dictionary.nim
138
dictionary.nim
|
@ -1,74 +1,92 @@
|
||||||
import dsl
|
import dsl
|
||||||
import macros
|
|
||||||
|
|
||||||
dumpTree: # dictionary:
|
# Tips: in python run
|
||||||
|
#
|
||||||
|
# import gensim.downloader
|
||||||
|
# model = gensim.downloader.load("glove-wiki-gigaword-50")
|
||||||
|
#
|
||||||
|
# Then find words related to what you want in model[] and do math on them.
|
||||||
|
# Then search through model to find nearby vectors.
|
||||||
|
|
||||||
|
dictionary:
|
||||||
glyphs:
|
glyphs:
|
||||||
vowel "i" outer
|
vowel "i", outer
|
||||||
vowel "e" outer slashed
|
vowel "e", outer, slashed
|
||||||
vowel "a" slashed
|
vowel "a", slashed
|
||||||
vowel "o" inner slashed
|
vowel "o", inner, slashed
|
||||||
vowel "u" inner
|
vowel "u", inner
|
||||||
vowel "y" both
|
vowel "y", both
|
||||||
|
|
||||||
cluster θ left top
|
cluster "θ", left, top
|
||||||
cluster ∫ left middle
|
cluster "∫", left, middle
|
||||||
cluster x left bottom
|
cluster "x", left, bottom
|
||||||
cluster n center middle
|
cluster "n", center, middle
|
||||||
cluster p right top
|
cluster "p", right, top
|
||||||
cluster t right middle
|
cluster "t", right, middle
|
||||||
cluster k right bottom
|
cluster "k", right, bottom
|
||||||
cluster θl left top tall
|
cluster "θl", left, top, tall
|
||||||
cluster ∫l left middle tall
|
cluster "∫l", left, middle, tall
|
||||||
cluster xl left bottom tall
|
cluster "xl", left, bottom, tall
|
||||||
cluster nl center middle tall
|
cluster "nl", center, middle, tall
|
||||||
cluster pl right top tall
|
cluster "pl", right, top, tall
|
||||||
cluster tl right middle tall
|
cluster "tl", right, middle, tall
|
||||||
cluster kl right bottom tall
|
cluster "kl", right, bottom, tall
|
||||||
cluster θr left top wide
|
cluster "θr", left, top, wide
|
||||||
cluster ∫r left middle wide
|
cluster "∫r", left, middle, wide
|
||||||
cluster xr left bottom wide
|
cluster "xr", left, bottom, wide
|
||||||
cluster nr center middle wide
|
cluster "nr", center, middle, wide
|
||||||
cluster pr right top wide
|
cluster "pr", right, top, wide
|
||||||
cluster tr right middle wide
|
cluster "tr", right, middle, wide
|
||||||
cluster kr right bottom wide
|
cluster "kr", right, bottom, wide
|
||||||
cluster sθ left top both
|
cluster "sθ", left, top, both
|
||||||
cluster s∫ left middle both
|
cluster "s∫", left, middle, both
|
||||||
cluster sx left bottom both
|
cluster "sx", left, bottom, both
|
||||||
cluster sn center middle both
|
cluster "sn", center, middle, both
|
||||||
cluster sp right top both
|
cluster "sp", right, top, both
|
||||||
cluster st right middle both
|
cluster "st", right, middle, both
|
||||||
cluster sk right bottom both
|
cluster "sk", right, bottom, both
|
||||||
|
|
||||||
syllables
|
syllables()
|
||||||
|
|
||||||
punctuation « left
|
punctuation "«", left
|
||||||
punctuation `.`
|
punctuation "."
|
||||||
punctuation » right
|
punctuation "»", right
|
||||||
|
|
||||||
numeric 0 circle
|
numeric "0", circle
|
||||||
numeric 1 dash
|
numeric "1", dash
|
||||||
numeric 2 vee
|
numeric "2", vee
|
||||||
numeric 4 dash hump
|
numeric "3", hump
|
||||||
numeric 5 vee hump
|
numeric "4", dash, hump
|
||||||
numeric `.` dump
|
numeric "5", vee, hump
|
||||||
|
numeric ".", dot
|
||||||
|
|
||||||
dialect jukashenikan:
|
dialect "jukashenikan":
|
||||||
replace x ç
|
replace "x", "ç"
|
||||||
replace p j
|
replace "p", "j"
|
||||||
# ...
|
# ...
|
||||||
|
|
||||||
dialect gazhenigan:
|
dialect "gazhenigan":
|
||||||
replace k g
|
replace "k", "g"
|
||||||
replace ∫ ʒ
|
replace "∫", "ʒ"
|
||||||
replace s z
|
replace "s", "z"
|
||||||
replace θ ð
|
replace "θ", "ð"
|
||||||
replace t d
|
replace "t", "d"
|
||||||
# ...
|
# ...
|
||||||
|
|
||||||
romanization:
|
words:
|
||||||
discard # ...
|
word "t":
|
||||||
|
noun "thing", "See t - dmPenta for better meaning."
|
||||||
|
# verb "be"
|
||||||
|
|
||||||
dictionary:
|
penta "n x", "Pronouns":
|
||||||
discard # ...
|
extremes i="Fully proximal", u="Fully distal"
|
||||||
|
|
||||||
#when isMainModule:
|
i: pronoun "I/me"
|
||||||
|
e: pronoun "this"
|
||||||
|
a: pronoun "you"
|
||||||
|
o: pronoun "it"
|
||||||
|
u: pronoun "that"
|
||||||
|
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
echo dict.toJSON
|
||||||
|
|
213
dsl.nim
213
dsl.nim
|
@ -1,37 +1,220 @@
|
||||||
import std/macros
|
import std/macros
|
||||||
import std/genasts
|
import std/tables
|
||||||
|
import std/options
|
||||||
|
import std/strutils
|
||||||
|
|
||||||
type
|
type
|
||||||
VowelAttribute* = enum Outer, Slashed, Inner
|
GlyphAttribute* = enum
|
||||||
GlyphKind* = enum Vowel
|
# Vowel Attributes
|
||||||
|
outer, slashed, inner,
|
||||||
|
|
||||||
|
# Cluster/Punctuation Attributes
|
||||||
|
left, center, right,
|
||||||
|
top, middle, bottom,
|
||||||
|
tall, wide, both,
|
||||||
|
|
||||||
|
# Numeric Attributes
|
||||||
|
circle, dash, vee, hump, dot,
|
||||||
|
|
||||||
|
|
||||||
|
GlyphKind* = enum Vowel, Cluster, Punctuation, Syllable, Numeric
|
||||||
Glyph* = object
|
Glyph* = object
|
||||||
spelling*: string
|
spelling*: string
|
||||||
case kind*: GlyphKind
|
kind*: GlyphKind
|
||||||
of Vowel: vattrs*: set[VowelAttribute]
|
attrs*: set[GlyphAttribute]
|
||||||
|
|
||||||
|
Replacement* = object
|
||||||
|
original*: string
|
||||||
|
replaced*: string
|
||||||
|
|
||||||
|
PartOfSpeech* = enum Noun, Pronoun, Verb, Adjective, Adverb, Syntactic
|
||||||
|
|
||||||
|
Definition* = object
|
||||||
|
brief*: string
|
||||||
|
long*: Option[string]
|
||||||
|
|
||||||
|
Word* = object
|
||||||
|
spelling*: string
|
||||||
|
definitions*: Table[PartOfSpeech, Definition]
|
||||||
|
|
||||||
|
Extremes* = object
|
||||||
|
i*: Option[string]
|
||||||
|
u*: Option[string]
|
||||||
|
|
||||||
|
Penta*[T] = object
|
||||||
|
spelling*: string
|
||||||
|
name*: string
|
||||||
|
exts*: Extremes
|
||||||
|
i*: ptr T
|
||||||
|
e*: ptr T
|
||||||
|
a*: ptr T
|
||||||
|
o*: ptr T
|
||||||
|
u*: ptr T
|
||||||
|
|
||||||
Dictionary* = object
|
Dictionary* = object
|
||||||
glyphs*: seq[Glyph]
|
glyphs*: seq[Glyph]
|
||||||
|
dialects*: Table[string, seq[Replacement]]
|
||||||
|
words*: seq[Word]
|
||||||
|
pentas*: seq[Penta[Word]]
|
||||||
|
|
||||||
|
proc replaceFirst(haystack: string, needle: char, content: char): string =
|
||||||
|
let idx = haystack.find(needle)
|
||||||
|
result = haystack
|
||||||
|
result[idx] = content
|
||||||
|
|
||||||
|
macro makeposprocs(): untyped =
|
||||||
|
result = nnkStmtList.newNimNode
|
||||||
|
|
||||||
|
for pos in PartOfSpeech:
|
||||||
|
let w = ident("w")
|
||||||
|
let defns = ident("definitions")
|
||||||
|
let name = ident(($pos).toLowerAscii)
|
||||||
|
let vpos = pos.newLit
|
||||||
|
|
||||||
|
result.add quote do:
|
||||||
|
proc `name`(short: string) {.used.} =
|
||||||
|
`w`.`defns`[`vpos`] = Definition(brief: short, long: string.none)
|
||||||
|
|
||||||
|
proc `name`(short: string, long: string) {.used.} =
|
||||||
|
`w`.`defns`[`vpos`] = Definition(brief: short, long: long.some)
|
||||||
|
|
||||||
template dictionary*(body: untyped) =
|
template dictionary*(body: untyped) =
|
||||||
var dict {.inject.}: Dictionary
|
var dict {.inject.}: Dictionary
|
||||||
dict.glyphs = @[]
|
dict.glyphs = @[]
|
||||||
|
|
||||||
macro vowel(arg: untyped) =
|
template glyphs(gbody: untyped) =
|
||||||
var v: Glyph = Glyph(spelling: arg[0].strVal)
|
block:
|
||||||
echo arg.treeRepr
|
proc vowel(spelling: string, attrs: varargs[GlyphAttribute]) =
|
||||||
|
var v: Glyph = Glyph(spelling: spelling, kind: Vowel)
|
||||||
|
for attr in attrs:
|
||||||
|
v.attrs.incl attr
|
||||||
|
dict.glyphs.add v
|
||||||
|
|
||||||
# TODO: populate attributes of v, then implement similar for other glyphs
|
proc cluster(spelling: string, attrs: varargs[GlyphAttribute]) =
|
||||||
|
var c: Glyph = Glyph(spelling: spelling, kind: Cluster)
|
||||||
|
for attr in attrs:
|
||||||
|
c.attrs.incl attr
|
||||||
|
dict.glyphs.add c
|
||||||
|
|
||||||
let x = newLit(v)
|
proc punctuation(spelling: string, attrs: varargs[GlyphAttribute]) =
|
||||||
result = genAst(x):
|
var p: Glyph = Glyph(spelling: spelling, kind: Punctuation)
|
||||||
dict.glyphs.add `x`
|
for attr in attrs:
|
||||||
|
p.attrs.incl attr
|
||||||
|
dict.glyphs.add p
|
||||||
|
|
||||||
echo result.repr
|
proc numeric(spelling: string, attrs: varargs[GlyphAttribute]) =
|
||||||
|
var n: Glyph = Glyph(spelling: spelling, kind: Numeric)
|
||||||
|
for attr in attrs:
|
||||||
|
n.attrs.incl attr
|
||||||
|
dict.glyphs.add n
|
||||||
|
|
||||||
|
proc syllables() =
|
||||||
|
var newglyphs: seq[Glyph]
|
||||||
|
for v in dict.glyphs:
|
||||||
|
if v.kind != Vowel:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for c in dict.glyphs:
|
||||||
|
if c.kind != Cluster:
|
||||||
|
continue
|
||||||
|
|
||||||
|
newglyphs.add Glyph(
|
||||||
|
spelling: c.spelling & v.spelling,
|
||||||
|
kind: Syllable,
|
||||||
|
attrs: v.attrs + c.attrs
|
||||||
|
)
|
||||||
|
|
||||||
|
dict.glyphs.add newglyphs
|
||||||
|
|
||||||
|
gbody
|
||||||
|
|
||||||
|
template dialect(name: string, dbody: untyped) =
|
||||||
|
block:
|
||||||
|
var dial: seq[Replacement]
|
||||||
|
proc replace(orig: string, by: string) =
|
||||||
|
dial.add Replacement(original: orig, replaced: by)
|
||||||
|
|
||||||
|
dbody
|
||||||
|
|
||||||
|
dict.dialects[name] = dial
|
||||||
|
|
||||||
|
template words(wbody: untyped) =
|
||||||
|
block:
|
||||||
|
template word(ortho: string, defns: untyped) =
|
||||||
|
block:
|
||||||
|
var w {.inject.}: Word = Word(spelling: ortho)
|
||||||
|
|
||||||
|
makeposprocs()
|
||||||
|
|
||||||
|
defns
|
||||||
|
|
||||||
|
dict.words.add w
|
||||||
|
|
||||||
|
template penta(ortho: string, pname: string, defns: untyped) =
|
||||||
|
block:
|
||||||
|
var p {.inject.}: Penta[Word]
|
||||||
|
p.spelling = ortho
|
||||||
|
|
||||||
|
proc extremes(i {.inject.}: string, u {.inject.}: string) {.used.} =
|
||||||
|
p.exts.i = some(i)
|
||||||
|
p.exts.u = some(u)
|
||||||
|
|
||||||
|
template i(defns2: untyped) {.used.} =
|
||||||
|
word ortho.replaceFirst(' ', 'i'): defns2; p.i = w.addr
|
||||||
|
template e(defns2: untyped) {.used.} =
|
||||||
|
word ortho.replaceFirst(' ', 'e'): defns2; p.e = w.addr
|
||||||
|
template a(defns2: untyped) {.used.} =
|
||||||
|
word ortho.replaceFirst(' ', 'a'): defns2; p.a = w.addr
|
||||||
|
template o(defns2: untyped) {.used.} =
|
||||||
|
word ortho.replaceFirst(' ', 'o'): defns2; p.o = w.addr
|
||||||
|
template u(defns2: untyped) {.used.} =
|
||||||
|
word ortho.replaceFirst(' ', 'u'): defns2; p.u = w.addr
|
||||||
|
|
||||||
|
p.spelling = ortho
|
||||||
|
p.name = pname
|
||||||
|
|
||||||
|
defns
|
||||||
|
|
||||||
|
dict.pentas.add p
|
||||||
|
|
||||||
|
wbody
|
||||||
|
|
||||||
|
# TODO: penta[X]s
|
||||||
body
|
body
|
||||||
|
|
||||||
|
proc toJSON*(dict: Dictionary): string =
|
||||||
|
return $dict
|
||||||
|
# TODO: stringify to JSON
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
dictionary:
|
dictionary:
|
||||||
vowel "x" outer slashed inner
|
glyphs:
|
||||||
|
vowel "i", outer
|
||||||
|
vowel "y", both
|
||||||
|
|
||||||
echo dict
|
cluster "θ", left, top
|
||||||
|
cluster "∫", left, middle
|
||||||
|
|
||||||
|
syllables()
|
||||||
|
|
||||||
|
punctuation "«", left
|
||||||
|
|
||||||
|
numeric "0", circle
|
||||||
|
|
||||||
|
dialect "jukashenikan":
|
||||||
|
replace "x", "ç"
|
||||||
|
|
||||||
|
dialect "gazhenigan":
|
||||||
|
replace "k", "g"
|
||||||
|
|
||||||
|
words:
|
||||||
|
word "t":
|
||||||
|
noun "thing", "See t - dmPenta for better meaning."
|
||||||
|
# verb "be"
|
||||||
|
|
||||||
|
penta "n x", "Pronouns":
|
||||||
|
extremes(i="Fully proximal", u="Fully distal")
|
||||||
|
|
||||||
|
i: pronoun "I/me"
|
||||||
|
|
||||||
|
echo dict.toJSON
|
||||||
|
|
Loading…
Reference in a new issue