Overview
Comment: | [graphspell] dawr builder: binary dict as string -> binary dict as list |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | graphspell | dict2 |
Files: | files | file ages | folders |
SHA3-256: |
85560b6ebaf4a870b5a71c06b099e334 |
User & Date: | olr on 2020-11-04 17:55:57 |
Other Links: | branch diff | manifest | tags |
Context
2020-11-05
| ||
13:27 | [graphspell][js] fix syntax error check-in: 6d54aadbb1 user: olr tags: graphspell, dict2 | |
2020-11-04
| ||
17:55 | [graphspell] dawr builder: binary dict as string -> binary dict as list check-in: 85560b6eba user: olr tags: graphspell, dict2 | |
12:37 | [graphspell][py] remove duplicate method check-in: a3980c3ca4 user: olr tags: graphspell, dict2 | |
Changes
Modified graphspell-js/dawg.js from [bb21108d9e] to [b461581aec].
︙ | ︙ | |||
340 341 342 343 344 345 346 | } } } } } // BINARY CONVERSION | | > > > > < | > > > | | > > > > > > > > > > > > > | > > > > > > > > > > > > > | 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | } } } } } // BINARY CONVERSION _calculateBinary (nCompressionMethod=1) { console.log("Write DAWG as an indexable binary dictionary"); this.nBytesArc = Math.floor( (this.nArcVal.toString(2).length + 2) / 8 ) + 1; // We add 2 bits. See DawgNode.convToBytes() this.nBytesOffset = 0; this._calcNumBytesNodeAddress(); this._calcNodesAddress(); let this.sByDic = this.oRoot.convToBytes(this.nBytesArc, this.nBytesNodeAddress); for (let oNode of this.dMinimizedNodes.values()) { this.sByDic += oNode.convToBytes(this.nBytesArc, this.nBytesNodeAddress); } console.log("Arc values (chars, affixes and tags): " + this.nArcVal); console.log("Arc size: "+this.nBytesArc+" bytes, Address size: "+this.nBytesNodeAddress+" bytes"); console.log("-> " + this.nBytesArc+this.nBytesNodeAddress + " * " + this.nArc + " = " + (this.nBytesArc+this.nBytesNodeAddress)*this.nArc + " bytes"); } _calcNumBytesNodeAddress () { // how many bytes needed to store all nodes/arcs in the binary dictionary this.nBytesNodeAddress = 1; while (((this.nBytesArc + this.nBytesNodeAddress) * this.nArc) > (2 ** (this.nBytesNodeAddress * 8))) { this.nBytesNodeAddress += 1; } } _calcNodesAddress () { let nBytesNode = this.nBytesArc + this.nBytesNodeAddress; let iAddr = this.oRoot.arcs.size * nBytesNode; for (let oNode of this.dMinimizedNodes.values()) { oNode.addr = iAddr; iAddr += Math.max(oNode.arcs.size, 1) * nBytesNode; } } _binaryToList () { this.lByDic = []; let nAcc = 0; let lBytesBuffer = []; let nDivisor = (this.nBytesArc + this.nBytesNodeAddress) / 2; for (let i = 0; i < this.sByDic.length; i+=2) { lBytesBuffer.push(parseInt(this.sByDic.slice(i, i+2), 16)); if (nAcc == (this.nBytesArc - 1)) { this.lByDic.push(this._convBytesToInteger(lBytesBuffer)); lBytesBuffer = []; } else if (nAcc == (this.nBytesArc + this.nBytesNodeAddress - 1)) { this.lByDic.push(Math.round(this._convBytesToInteger(lBytesBuffer) / nDivisor)); // Math.round should be useless, BUT with JS who knowns what can happen… lBytesBuffer = []; nAcc = -1; } nAcc = nAcc + 1; } } _convBytesToInteger (aBytes) { // Byte order = Big Endian (bigger first) let nVal = 0; let nWeight = (aBytes.length - 1) * 8; for (let n of aBytes) { nVal += n << nWeight; nWeight = nWeight - 8; } return nVal; } createBinaryJSON (nCompressionMethod=1) { this._calculateBinary(nCompressionMethod); this._binaryToList(); let oJSON = { "sHeader": "/grammalecte-fsa/", "sLangCode": this.sLangCode, "sLangName": this.sLangName, "sDicName": this.sDicName, "sDescription": this.sDescription, "sFileName": "[none]", |
︙ | ︙ | |||
396 397 398 399 400 401 402 | "nArc": this.nArc, "lArcVal": this.lArcVal, "nArcVal": this.nArcVal, "nCompressionMethod": nCompressionMethod, "nBytesArc": this.nBytesArc, "nBytesNodeAddress": this.nBytesNodeAddress, "nBytesOffset": this.nBytesOffset, | | > | 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | "nArc": this.nArc, "lArcVal": this.lArcVal, "nArcVal": this.nArcVal, "nCompressionMethod": nCompressionMethod, "nBytesArc": this.nBytesArc, "nBytesNodeAddress": this.nBytesNodeAddress, "nBytesOffset": this.nBytesOffset, //"sByDic": this.sByDic, // binary word graph "lByDic": this.lByDic, "l2grams": Array.from(this.a2grams) }; return oJSON; } _getDate () { let oDate = new Date(); |
︙ | ︙ |
Modified graphspell-js/ibdawg.js from [280555859f] to [8c83fe3dde].
︙ | ︙ | |||
150 151 152 153 154 155 156 | sName, nCompressionMethod, sHeader, lArcVal, nArcVal, sByDic, sLang, nChar, nBytesArc, nBytesNodeAddress, nEntry, nNode, nArc, nAff, cStemming, nTag, dChar, nBytesOffset, */ if (!(this.sHeader.startsWith("/grammalecte-fsa/") || this.sHeader.startsWith("/pyfsa/"))) { throw TypeError("# Error. Not a grammalecte-fsa binary dictionary. Header: " + this.sHeader); } | < < < > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | sName, nCompressionMethod, sHeader, lArcVal, nArcVal, sByDic, sLang, nChar, nBytesArc, nBytesNodeAddress, nEntry, nNode, nArc, nAff, cStemming, nTag, dChar, nBytesOffset, */ if (!(this.sHeader.startsWith("/grammalecte-fsa/") || this.sHeader.startsWith("/pyfsa/"))) { throw TypeError("# Error. Not a grammalecte-fsa binary dictionary. Header: " + this.sHeader); } // <dChar> to get the value of an arc, <dCharVal> to get the char of an arc with its value this.dChar = helpers.objectToMap(this.dChar); this.dCharVal = this.dChar.gl_reverse(); this.a2grams = (this.l2grams) ? new Set(this.l2grams) : null; if (!this.hasOwnProperty("lByDic")) { // old dictionary version if (!this.hasOwnProperty("sByDic")) { throw TypeError("# Error. No usable data in the dictionary."); } this.lByDic = []; let nAcc = 0; let lBytesBuffer = []; let nDivisor = (this.nBytesArc + this.nBytesNodeAddress) / 2; for (let i = 0; i < this.sByDic.length; i+=2) { lBytesBuffer.push(parseInt(this.sByDic.slice(i, i+2), 16)); if (nAcc == (this.nBytesArc - 1)) { this.lByDic.push(this._convBytesToInteger(lBytesBuffer)); lBytesBuffer = []; } else if (nAcc == (this.nBytesArc + this.nBytesNodeAddress - 1)) { this.lByDic.push(Math.round(this._convBytesToInteger(lBytesBuffer) / nDivisor)); // Math.round should be useless, BUT with JS who knowns what can happen… lBytesBuffer = []; nAcc = -1; } nAcc = nAcc + 1; } } // masks this._arcMask = (2 ** ((this.nBytesArc * 8) - 3)) - 1; this._finalNodeMask = 1 << ((this.nBytesArc * 8) - 1); this._lastArcMask = 1 << ((this.nBytesArc * 8) - 2); // function to decode the affix/suffix code if (this.cStemming == "S") { this.funcStemming = str_transform.changeWordWithSuffixCode; } else if (this.cStemming == "A") { this.funcStemming = str_transform.changeWordWithAffixCode; } else { this.funcStemming = str_transform.noStemming; } //console.log(this.getInfo()); this.bAcronymValid = true; this.bNumAtLastValid = false; // lexicographer module ? this.lexicographer = null; // JS still sucks: we’ll try importation when importation will be available in Workers. Still waiting... |
︙ | ︙ |
Modified graphspell/dawg.py from [8c6420c0dc] to [781b24100a].
︙ | ︙ | |||
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | # BINARY CONVERSION def _calculateBinary (self, nCompressionMethod=1): print(" > Write DAWG as an indexable binary dictionary") self.nBytesArc = ( (self.nArcVal.bit_length() + 2) // 8 ) + 1 # We add 2 bits. See DawgNode.convToBytes() self.nBytesOffset = 0 self._calcNumBytesNodeAddress() self._calcNodesAddress() print(" Arc values (chars, affixes and tags): {} -> {} bytes".format( self.nArcVal, len("\t".join(self.lArcVal).encode("utf-8")) )) print(" Arc size: {} bytes, Address size: {} bytes -> {} * {} = {} bytes".format( self.nBytesArc, self.nBytesNodeAddress, \ self.nBytesArc+self.nBytesNodeAddress, self.nArc, \ (self.nBytesArc+self.nBytesNodeAddress)*self.nArc )) def _calcNumBytesNodeAddress (self): "how many bytes needed to store all nodes/arcs in the binary dictionary" self.nBytesNodeAddress = 1 while ((self.nBytesArc + self.nBytesNodeAddress) * self.nArc) > (2 ** (self.nBytesNodeAddress * 8)): self.nBytesNodeAddress += 1 def _calcNodesAddress (self): nBytesNode = self.nBytesArc + self.nBytesNodeAddress iAddr = len(self.oRoot.arcs) * nBytesNode for oNode in self.lMinimizedNodes: oNode.addr = iAddr iAddr += max(len(oNode.arcs), 1) * nBytesNode | > > > > > > > > > > > > > > > > > > > > | < < | < | 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | # BINARY CONVERSION def _calculateBinary (self, nCompressionMethod=1): print(" > Write DAWG as an indexable binary dictionary") self.nBytesArc = ( (self.nArcVal.bit_length() + 2) // 8 ) + 1 # We add 2 bits. See DawgNode.convToBytes() self.nBytesOffset = 0 self._calcNumBytesNodeAddress() self._calcNodesAddress() self.byDic = b"" self.byDic = self.oRoot.convToBytes(self.nBytesArc, self.nBytesNodeAddress) for oNode in self.lMinimizedNodes: self.byDic += oNode.convToBytes(self.nBytesArc, self.nBytesNodeAddress) print(" Arc values (chars, affixes and tags): {} -> {} bytes".format( self.nArcVal, len("\t".join(self.lArcVal).encode("utf-8")) )) print(" Arc size: {} bytes, Address size: {} bytes -> {} * {} = {} bytes".format( self.nBytesArc, self.nBytesNodeAddress, \ self.nBytesArc+self.nBytesNodeAddress, self.nArc, \ (self.nBytesArc+self.nBytesNodeAddress)*self.nArc )) def _calcNumBytesNodeAddress (self): "how many bytes needed to store all nodes/arcs in the binary dictionary" self.nBytesNodeAddress = 1 while ((self.nBytesArc + self.nBytesNodeAddress) * self.nArc) > (2 ** (self.nBytesNodeAddress * 8)): self.nBytesNodeAddress += 1 def _calcNodesAddress (self): nBytesNode = self.nBytesArc + self.nBytesNodeAddress iAddr = len(self.oRoot.arcs) * nBytesNode for oNode in self.lMinimizedNodes: oNode.addr = iAddr iAddr += max(len(oNode.arcs), 1) * nBytesNode def _binaryToList (self): self.lByDic = [] nAcc = 0 byBuffer = b"" nDivisor = (self.nBytesArc + self.nBytesNodeAddress) / 2 for i in range(0, len(self.byDic)): byBuffer += self.byDic[i:i+1] if nAcc == (self.nBytesArc - 1): self.lByDic.append(int.from_bytes(byBuffer, byteorder="big")) byBuffer = b"" elif nAcc == (self.nBytesArc + self.nBytesNodeAddress - 1): self.lByDic.append(round(int.from_bytes(byBuffer, byteorder="big") / nDivisor)) byBuffer = b"" nAcc = -1 nAcc = nAcc + 1 def getBinaryAsJSON (self, nCompressionMethod=1): "return a JSON string containing all necessary data of the dictionary (compressed as a binary string)" self._calculateBinary(nCompressionMethod) self._binaryToList() return { "sHeader": "/grammalecte-fsa/", "sLangCode": self.sLangCode, "sLangName": self.sLangName, "sDicName": self.sDicName, "sDescription": self.sDescription, "sFileName": self.sFileName, |
︙ | ︙ | |||
410 411 412 413 414 415 416 | "nCompressionMethod": nCompressionMethod, "nBytesArc": self.nBytesArc, "nBytesNodeAddress": self.nBytesNodeAddress, "nBytesOffset": self.nBytesOffset, # Mozilla’s JS parser don’t like file bigger than 4 Mb! # So, if necessary, we use an hexadecimal string, that we will convert later in Firefox’s extension. # https://github.com/mozilla/addons-linter/issues/1361 | | > | | | 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | "nCompressionMethod": nCompressionMethod, "nBytesArc": self.nBytesArc, "nBytesNodeAddress": self.nBytesNodeAddress, "nBytesOffset": self.nBytesOffset, # Mozilla’s JS parser don’t like file bigger than 4 Mb! # So, if necessary, we use an hexadecimal string, that we will convert later in Firefox’s extension. # https://github.com/mozilla/addons-linter/issues/1361 #"sByDic": self.byDic.hex(), "lByDic": self.lByDic, "l2grams": list(self.a2grams) } def writeAsJSObject (self, spfDst, nCompressionMethod=1, bInJSModule=False): "write a file (JSON or JS module) with all the necessary data" if not spfDst.endswith(".json"): spfDst += "."+str(nCompressionMethod)+".json" with open(spfDst, "w", encoding="utf-8", newline="\n") as hDst: if bInJSModule: hDst.write('// JavaScript\n// Generated data (do not edit)\n\n"use strict";\n\nconst dictionary = ') hDst.write( json.dumps(self.getBinaryAsJSON(nCompressionMethod), ensure_ascii=False) ) if bInJSModule: hDst.write(";\n\nexports.dictionary = dictionary;\n") def _getDate (self): return time.strftime("%Y-%m-%d %H:%M:%S") def _writeNodes (self, sPathFile, nCompressionMethod=1): |
︙ | ︙ | |||
452 453 454 455 456 457 458 | def __init__ (self): self.i = DawgNode.NextId DawgNode.NextId += 1 self.final = False self.arcs = {} # key: arc value; value: a node self.addr = 0 # address in the binary dictionary self.pos = 0 # position in the binary dictionary (version 2) | < | 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | def __init__ (self): self.i = DawgNode.NextId DawgNode.NextId += 1 self.final = False self.arcs = {} # key: arc value; value: a node self.addr = 0 # address in the binary dictionary self.pos = 0 # position in the binary dictionary (version 2) @classmethod def resetNextId (cls): "set NextId to 0 " cls.NextId = 0 def setPos (self): # version 2 |
︙ | ︙ |
Modified graphspell/ibdawg.py from [afeca057d2] to [953707753a].
︙ | ︙ | |||
119 120 121 122 123 124 125 | raise OSError("# Error. File not found or not loadable: "+source) self.sFileName = source oData = json.loads(by.decode("utf-8")) #json.loads(by) # In Python 3.6, can read directly binary strings else: self.sFileName = "[None]" oData = source | < < > > | | | > > | | < | | | | | | | | | | | | 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | raise OSError("# Error. File not found or not loadable: "+source) self.sFileName = source oData = json.loads(by.decode("utf-8")) #json.loads(by) # In Python 3.6, can read directly binary strings else: self.sFileName = "[None]" oData = source self.__dict__.update(oData) self.dCharVal = { v: k for k, v in self.dChar.items() } self.a2grams = set(getattr(self, 'l2grams')) if hasattr(self, 'l2grams') else None if "lByDic" not in oData: print(">>>> lByDic not in oData") if "sByDic" not in oData: raise TypeError("# Error. No usable data in the dictionary.") # old dictionary version self.lByDic = [] self.byDic = binascii.unhexlify(oData["sByDic"]) nAcc = 0 byBuffer = b"" nDivisor = (self.nBytesArc + self.nBytesNodeAddress) / 2 for i in range(0, len(self.byDic)): byBuffer += self.byDic[i:i+1] if nAcc == (self.nBytesArc - 1): self.lByDic.append(int.from_bytes(byBuffer, byteorder="big")) byBuffer = b"" elif nAcc == (self.nBytesArc + self.nBytesNodeAddress - 1): self.lByDic.append(round(int.from_bytes(byBuffer, byteorder="big") / nDivisor)) byBuffer = b"" nAcc = -1 nAcc = nAcc + 1 # masks self._arcMask = (2 ** ((self.nBytesArc * 8) - 3)) - 1 self._finalNodeMask = 1 << ((self.nBytesArc * 8) - 1) self._lastArcMask = 1 << ((self.nBytesArc * 8) - 2) # function to decode the affix/suffix code |
︙ | ︙ |