add python
This commit is contained in:
parent
2a234dbf7d
commit
b62f6c2fa4
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,6 +3,7 @@ out/
|
|||||||
temp/
|
temp/
|
||||||
NlpSrc/*
|
NlpSrc/*
|
||||||
!NlpSrc/*.nlp
|
!NlpSrc/*.nlp
|
||||||
|
!NlpSrc/README.md
|
||||||
NlpParser/*
|
NlpParser/*
|
||||||
!NlpParser/Nlp.g4
|
!NlpParser/Nlp.g4
|
||||||
!NlpParser/NlpRunner.java
|
!NlpParser/NlpRunner.java
|
||||||
|
2
NlpProc/NlpTrBaseCompiler.py
Normal file
2
NlpProc/NlpTrBaseCompiler.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
import NlpUtils
|
||||||
|
import sys, collections
|
26
NlpProc/NlpTrBaseCreator.py
Normal file
26
NlpProc/NlpTrBaseCreator.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import NlpUtils
|
||||||
|
import sys, collections
|
||||||
|
|
||||||
|
def CreateBaseJsonWrapper(baseJson: dict) -> dict[str, str]:
|
||||||
|
result: dict[str, str] = {}
|
||||||
|
stack: collections.deque = collections.deque()
|
||||||
|
CreateBaseJson(baseJson, stack, result)
|
||||||
|
return result
|
||||||
|
def CreateBaseJson(baseJson: dict, stack: collections.deque, result: dict[str, str]):
|
||||||
|
assert isinstance(baseJson, dict)
|
||||||
|
assert 'entries' in baseJson
|
||||||
|
|
||||||
|
counter = 0
|
||||||
|
for entry in baseJson['entries']:
|
||||||
|
if isinstance(entry, str):
|
||||||
|
result['.'.join(tuple(stack) + (str(counter), ))] = entry
|
||||||
|
counter += 1
|
||||||
|
else:
|
||||||
|
stack.append(entry['section'])
|
||||||
|
CreateBaseJson(entry, stack, result)
|
||||||
|
stack.pop()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
baseJson = NlpUtils.LoadJsonFromFile(sys.argv[1])
|
||||||
|
trJson = CreateBaseJsonWrapper(baseJson)
|
||||||
|
NlpUtils.WriteJsonToFile(sys.argv[2], trJson)
|
0
NlpProc/NlpTrPatchCompiler.py
Normal file
0
NlpProc/NlpTrPatchCompiler.py
Normal file
4
NlpProc/NlpTrPatchCreator.py
Normal file
4
NlpProc/NlpTrPatchCreator.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import NlpUtils
|
||||||
|
import sys, collections
|
||||||
|
|
||||||
|
|
46
NlpProc/NlpUtils.py
Normal file
46
NlpProc/NlpUtils.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import jsondiff
|
||||||
|
import collections
|
||||||
|
import io
|
||||||
|
import json
|
||||||
|
|
||||||
|
def WriteJsonToFile(filepath: str, jsonData: dict):
|
||||||
|
with open(filepath, 'w', encoding='utf-8') as f:
|
||||||
|
json.dump(jsonData, f, indent=4, sort_keys=False)
|
||||||
|
|
||||||
|
def LoadJsonFromFile(filepath: str) -> dict:
|
||||||
|
with open(filepath, 'r', encoding='utf-8') as f:
|
||||||
|
return json.load(f)
|
||||||
|
|
||||||
|
def SaveDiffToFileWrapper(jsonDiffData: dict, filepath: str) -> dict[str, str]:
|
||||||
|
result: dict[tuple[int], str] = {}
|
||||||
|
stack: collections.deque = collections.deque()
|
||||||
|
with open(filepath, 'w', encoding='utf-8') as fdiff:
|
||||||
|
SaveDiffToFile(jsonDiffData, fdiff, stack, result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def SaveDiffToFile(jsonDiffData: dict, fs: io.TextIOWrapper, stack: collections.deque, result: dict[tuple[int], str]):
|
||||||
|
assert isinstance(jsonDiffData, dict)
|
||||||
|
assert len(jsonDiffData) == 1
|
||||||
|
assert "entries" in jsonDiffData
|
||||||
|
assert isinstance(jsonDiffData["entries"], dict)
|
||||||
|
|
||||||
|
for key, item in jsonDiffData["entries"].items():
|
||||||
|
if isinstance(key, int):
|
||||||
|
stack.append(key)
|
||||||
|
SaveDiffToFile(item, fs, stack, result)
|
||||||
|
stack.pop()
|
||||||
|
elif key == jsondiff.symbols.insert:
|
||||||
|
for (modIdx, modEntry) in item:
|
||||||
|
stridx = ".".join(tuple(stack) + (modIdx, ))
|
||||||
|
result[stridx] = modEntry
|
||||||
|
fs.write(f'i {stridx}\n')
|
||||||
|
elif key == jsondiff.symbols.delete:
|
||||||
|
for delIdx in item:
|
||||||
|
stridx = ".".join(tuple(stack) + (delIdx, ))
|
||||||
|
fs.write(f'd {stridx}\n')
|
||||||
|
else:
|
||||||
|
raise Exception("invalid key type")
|
||||||
|
|
||||||
|
def ReadDiffFromFile(translations: tuple[str], filepath: str, result: dict):
|
||||||
|
pass
|
||||||
|
|
6
NlpSrc/README.md
Normal file
6
NlpSrc/README.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# Nlp Src
|
||||||
|
|
||||||
|
Useful comparing differences command:
|
||||||
|
|
||||||
|
`diff -u VT25.json VT50.json`
|
||||||
|
`diff -u VT25.txt VT50.txt`
|
Loading…
Reference in New Issue
Block a user