Compare commits

...

2 Commits

Author SHA1 Message Date
9fbab51257 texdict: add logo idea to todo.md 2025-07-20 20:10:26 +02:00
9066701bdd textdict: add basic mulit-entry capability 2025-07-20 20:06:05 +02:00
6 changed files with 135 additions and 38 deletions

View File

@ -1 +1 @@
{"entry": "asɛm", "key": "asem", "meanings": [ { "type": "noun", "description": "word", "examples": ["Wonim saa asɛm yi?", " Wo nim sa ara asɛm yi? " ]}, { "type": "noun", "description": "issue", "examples": ["Dɛn ne asɛm no? " ]}, { "type": "noun", "description": "news ", "examples": [ ]} ] } { "entries" : [ {"entry": "asɛm", "key": "asem", "meanings": [ { "type": "noun", "description": "word", "examples": ["Wonim saa asɛm yi?", " Wo nim sa ara asɛm yi? " ]}, { "type": "noun", "description": "issue", "examples": ["Dɛn ne asɛm no? " ]}, { "type": "noun", "description": "news ", "examples": [ ]} ] } ] }

4
texdict/example2.json Normal file
View File

@ -0,0 +1,4 @@
{ "entries" : [
{"entry": "asɛm", "key": "asem", "meanings": [ { "type": "noun", "description": "word", "examples": ["Wonim saa asɛm yi?", " Wo nim sa ara asɛm yi? " ]}, { "type": "noun", "description": "issue", "examples": ["Dɛn ne asɛm no? " ]}, { "type": "noun", "description": "news ", "examples": [ ]} ] },
{"entry": "asɛm", "key": "asem", "meanings": [ { "type": "noun", "description": "word", "examples": ["Wonim saa asɛm yi?", " Wo nim sa ara asɛm yi? " ]}, { "type": "noun", "description": "issue", "examples": ["Dɛn ne asɛm no? " ]}, { "type": "noun", "description": "news ", "examples": [ ]} ] }
] }

5
texdict/example2.tex Normal file
View File

@ -0,0 +1,5 @@
\dictentrysorted{asɛm}{asem}{\m (n) word ; Wonim saa asɛm yi? ; Wo nim sa ara asɛm yi? \m (n) issue ; Dɛn ne asɛm no? \m (n) news }
\dictentrysorted{asɛm}{asem}{\m (n) word ; Wonim saa asɛm yi? ; Wo nim sa ara asɛm yi? \m (n) issue ; Dɛn ne asɛm no? \m (n) news }
%\dictentrysorted{asɛm}{asem}{\m (n) word ; Wonim saa asɛm yi? ; Wo nim sa ara asɛm yi? \m (n) issue ; Dɛn ne asɛm no? \m (n) news }
%testasdfasd

8
texdict/example5.tex Normal file
View File

@ -0,0 +1,8 @@
\dictentrysorted{a}{a}{\w{n} which, who}
%\dictentrysorted{aba}{aba}{ \m \w{v} \g{ba} \m \w{n} child, offspring}
\dictentrysorted{ababawa}{ababawa}{\w{n}young woman}
\dictentrysorted{abarima}{abarima}{\w{n}young man}
\dictentrysorted{abe}{abe}{\w{n} palm}
\dictentrysorted{abebisa}{abebisa}{\s{bisa}}
%\dictentrysorted{abede}{abede}{\g{de} to be ripe \td{???}}

View File

@ -9,48 +9,57 @@ parser = argparse.ArgumentParser(
) )
parser.add_argument('file', help='input file') parser.add_argument('file', help='input file')
parser.add_argument('-i', '--input', choices=('json', 'tex'), help='input format: json, tex, sqlite (default: file extension') parser.add_argument('-i', '--input', choices=('json', 'tex'), help='input format: json, tex (default: json; sqlite might be added)')
parser.add_argument('-f', '--format', choices=('json', 'tex'), help='choose output format: json, tex, sqlite (default: json)') parser.add_argument('-o', '--output', choices=('json', 'tex'), help='output format: json, tex (default: json; sqlite might be added)')
parser.add_argument('-v', '--verbose', action='store_true', help='boolean verbose switch (default: false)') parser.add_argument('-v', '--verbose', action='store_true', help='boolean verbose switch (default: false)')
#parser.add_argument('-e', '--entry', help='add a entry to a file') #parser.add_argument('-e', '--entry', help='add a entry to a file')
#parser.add_argument('-o', '--output', help='write dictionary file to file') #parser.add_argument('-o', '--output', help='write dictionary file to file')
#parser.add_argument('-w', '--write', action='store_true', required=False, help='overwrite input file') #parser.add_argument('-w', '--write', action='store_true', required=False, help='overwrite input file')
args = parser.parse_args() args = parser.parse_args()
# verboseprint https://stackoverflow.com/a/5980173 # def verboseprint() https://stackoverflow.com/a/5980173
verboseprint = print if args.verbose else lambda *a, **k: None verboseprint = print if args.verbose else lambda *a, **k: None
inputfile=args.file inputfile=args.file
informat=inputfile.split('.')[-1] informat=inputfile.split('.')[-1]
#verboseprint(args.input) #verboseprint(args.input)
if args.input: if args.input:
informat=args.input informat=args.input
verboseprint("input file format:", informat) verboseprint("INFO: input file format:", informat)
outformat='json' outformat='json'
if args.format: if args.output:
outformat=args.format outformat=args.output
verboseprint("output file format:", outformat) verboseprint("INFO: output file format:", outformat)
# KNOWN WORD TYPES # KNOWN WORD TYPES
shorttypes={ shorttypes={
'noun':'n' 'noun':'n'
} }
shorttypes_inversed= {v: k for k, v in shorttypes.items()} shorttypes_inversed= {v: k for k, v in shorttypes.items()}
verboseprint("Word types:",shorttypes) verboseprint("INFO: Word types:",shorttypes)
# #
# READ JSON FILE # READ JSON FILE
def get_data_from_jsonfile(filename): def get_data_from_jsonfile(filename):
verboseprint("opening file:", filename) verboseprint("INFO: opening file:", filename)
with open(filename) as infile: with open(filename) as infile:
data = json.load(infile) data = json.load(infile)
verboseprint(json.dumps(data, ensure_ascii=False)) verboseprint("INFO:", json.dumps(data, ensure_ascii=False))
return json.dumps(data, ensure_ascii=False) return json.dumps(data, ensure_ascii=False)
# READ TEX FILE # READ TEX FILE
def get_data_from_texfile(filename): def get_data_from_texfile(filename):
verboseprint("opening file:", filename) verboseprint("INFO: opening file:", filename)
with open(filename) as infile: with open(filename) as infile:
jsonstring='{ "entries" : [ '
j=0
for line in infile: for line in infile:
jsonstring='{"entry": "' if line == '\n':
continue
if line.startswith('%'):
continue
if j!=0:
jsonstring+=', '
j+=1
jsonstring+='{"entry": "'
#ENTRY #ENTRY
jsonstring+=line.split('{')[1].split('}')[0]+'", ' jsonstring+=line.split('{')[1].split('}')[0]+'", '
#KEY #KEY
@ -58,15 +67,15 @@ def get_data_from_texfile(filename):
jsonstring+=line.split('{')[2].split('}')[0]+'", ' jsonstring+=line.split('{')[2].split('}')[0]+'", '
#MEANINGS #MEANINGS
number_of_meanings=line.count('\m') number_of_meanings=line.count('\m')
verboseprint("Meanings found:", number_of_meanings) verboseprint("INFO: Meanings found:", number_of_meanings)
jsonstring+='"meanings": [ ' jsonstring+='"meanings": [ '
for i in range(number_of_meanings): for i in range(number_of_meanings):
#MEANING #MEANING
verboseprint(i) verboseprint("INFO: number of meanins", i)
meaning=line.split("\m")[i+1][1:] meaning=line.split("\m")[i+1][1:]
if i == (number_of_meanings-1): if i == (number_of_meanings-1):
meaning=meaning[:-2] meaning=meaning[:-2]
verboseprint("Meaning:",meaning) verboseprint("INFO: Meaning:",meaning)
#TYPE #TYPE
#todo what if no type #todo what if no type
if i == 0: if i == 0:
@ -83,7 +92,7 @@ def get_data_from_texfile(filename):
jsonstring+=', "examples": [' jsonstring+=', "examples": ['
if ';' in meaning: if ';' in meaning:
examples=meaning.split('; ')[1] examples=meaning.split('; ')[1]
verboseprint("examples:", examples) verboseprint("INFO: examples:", examples)
number_of_examples=len(examples.split(', ')) number_of_examples=len(examples.split(', '))
j=0 j=0
for example in examples.split(','): for example in examples.split(','):
@ -96,28 +105,34 @@ def get_data_from_texfile(filename):
#CLOSE MEANING #CLOSE MEANING
jsonstring+='}' jsonstring+='}'
jsonstring+=' ] }' jsonstring+=' ] }'
jsonstring+='] }'
return jsonstring return jsonstring
def json2tex(entry): def json2tex(entries):
entry_json=json.loads(entry) entries_json=json.loads(entries)
entry_tex='\dictentrysorted{'+entry_json['entry']+'}{'+entry_json['key']+'}{' entries_tex=""
for meaning in entry_json['meanings']: for entry_json in entries_json["entries"]:
#TYPE # entry_json=json.loads(entry)
wordtype=meaning['type'] entry_tex='\dictentrysorted{'+entry_json['entry']+'}{'+entry_json['key']+'}{'
if wordtype in shorttypes: for meaning in entry_json['meanings']:
wordtype=shorttypes[wordtype] #TYPE
else: wordtype=meaning['type']
print("VERBOSE: stderr: "+wordtype+" is not in the the known word type list!") if wordtype in shorttypes:
entry_tex+='\m ('+wordtype+') ' wordtype=shorttypes[wordtype]
#DESCRIPTION else:
entry_tex+=meaning['description']+' ' print("VERBOSE: stderr: "+wordtype+" is not in the the known word type list!")
#EXAMPLES entry_tex+='\m ('+wordtype+') '
verboseprint(meaning) #DESCRIPTION
if len(meaning['examples']) != 0: entry_tex+=meaning['description']+' '
for example in meaning['examples']: #EXAMPLES
entry_tex+='; '+example+' ' verboseprint("INFO:", meaning)
entry_tex+='}' if len(meaning['examples']) != 0:
return entry_tex for example in meaning['examples']:
entry_tex+='; '+example+' '
entry_tex+='}'
entries_tex+=entry_tex
entries_tex+="\n"
return entries_tex
def tex2json(entry): def tex2json(entry):
entry_json=json.dumps(json.loads(entry), ensure_ascii=False) entry_json=json.dumps(json.loads(entry), ensure_ascii=False)

View File

@ -1,2 +1,67 @@
# TODO: # TODO:
- change such that two entries can be converted
- [ ] test with example5.tex
- [ ] make tex parser cleaner and think more about structure of entries and levels
- [ ] how should the output of texdict.py -o tex be used?
```
\documenttype{standalone}
%\usepackage{texdict}
\begin{document}
\input{output-from-textdict.tex}
\end{document}
```
- [ ] if dict package, how can I keep texdict.py and texdict.sty in sync?
- [ ] textdict.py needs to be part of textdict.sty package
- [ ] refactor code acording to 12factor app
- [ ] https://12factor.net/de/
- [ ] what about that every entry should only appear once?
- [ ] if input and output are the same, should it be converted to json trotzdem?
- [ ] maybe not and add a --test flag for testing the syntax correctness
- [ ] logo could be
```
\documentclass{standalone}
\begin{document}
$\tau$e$\chi$\textit{dict}\texttt{.py}
\end{document}
```
---
---
- [x] test:
- [x] $(./texdict.py example2.json -o tex`)
- [x] dann auch testen:
- [x] $(./texdict.py example2.tex )
- [x] change such that two entries can be converted
- [x] json:
```
{[entry1,e2,e3]}
{entries : [entry1, entry2, entry3]}
```
- [x] tex
```
\entry{1}
\entry{2}
\entry{3}
```
\documentclass{standalone}
\begin{document}
$\tau$e$\Xi$\textit{dict}\texttt{.py}
\end{document}
a poems with vim
a poems with him
a poems with vim vim
a poems with vim vim