add python
This commit is contained in:
		
							
								
								
									
										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
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user