Index: darg.py ================================================================== --- darg.py +++ darg.py @@ -17,11 +17,11 @@ class DARG: """DIRECT ACYCLIC RULE GRAPH""" # This code is inspired from Steve Hanov’s DAWG, 2011. (http://stevehanov.ca/blog/index.php?id=115) def __init__ (self, lRule, sLangCode): - print("===== Direct Acyclic Token Graph - Minimal Acyclic Finite State Automaton =====") + print("===== Direct Acyclic Rule Graph - Minimal Acyclic Finite State Automaton =====") # Preparing DARG print(" > Preparing list of tokens") self.sLangCode = sLangCode self.nRule = len(lRule) @@ -66,14 +66,14 @@ oNode = self.oRoot else: oNode = self.lUncheckedNodes[-1][2] iToken = nCommonPrefix - for token in aRule[nCommonPrefix:]: + for sToken in aRule[nCommonPrefix:]: oNextNode = Node() - oNode.dArcs[token] = oNextNode - self.lUncheckedNodes.append((oNode, token, oNextNode)) + oNode.dArcs[sToken] = oNextNode + self.lUncheckedNodes.append((oNode, sToken, oNextNode)) if iToken == (len(aRule) - 2): oNode.bFinal = True iToken += 1 oNode = oNextNode oNode.bFinal = True @@ -84,14 +84,14 @@ self._minimize(0) def _minimize (self, downTo): # proceed from the leaf up to a certain point for i in range( len(self.lUncheckedNodes)-1, downTo-1, -1 ): - oNode, token, oChildNode = self.lUncheckedNodes[i] + oNode, sToken, oChildNode = self.lUncheckedNodes[i] if oChildNode in self.lMinimizedNodes: # replace the child with the previously encountered one - oNode.dArcs[token] = self.lMinimizedNodes[oChildNode] + oNode.dArcs[sToken] = self.lMinimizedNodes[oChildNode] else: # add the state to the minimized nodes. self.lMinimizedNodes[oChildNode] = oChildNode self.lUncheckedNodes.pop() @@ -100,18 +100,10 @@ def countArcs (self): self.nArc = 0 for oNode in self.lMinimizedNodes: self.nArc += len(oNode.dArcs) - - def lookup (self, sWord): - oNode = self.oRoot - for c in sWord: - if c not in oNode.dArcs: - return False - oNode = oNode.dArcs[c] - return oNode.bFinal def displayInfo (self): print(" * {:<12} {:>16,}".format("Rules:", self.nRule)) print(" * {:<12} {:>16,}".format("Nodes:", self.nNode)) print(" * {:<12} {:>16,}".format("Arcs:", self.nArc)) Index: gc_core/py/lang_core/gc_sentence.py ================================================================== --- gc_core/py/lang_core/gc_sentence.py +++ gc_core/py/lang_core/gc_sentence.py @@ -20,11 +20,11 @@ lPointer = [] for dToken in self.lToken: for i, dPointer in enumerate(lPointer): bValid = False for dNode in self._getNextMatchingNodes(dToken, dPointer["dNode"]): - dPointer["nOffset"] += 1 + dPointer["nOffset"] = dToken["i"] dPointer["dNode"] = dNode bValid = True if not bValid: del lPointer[i] for dNode in self._getNextMatchingNodes(dToken, dGraph): @@ -52,26 +52,30 @@ for sArc in dGraph[nextNodeKey]: bCondMemo = None sFuncCond, cActionType, sWhat, *eAct = dRule[sArc] # action in lActions: [ condition, action type, replacement/suggestion/action[, iGroupStart, iGroupEnd[, message, URL]] ] try: - bCondMemo = not sFuncCond or globals()[sFuncCond](s, sx, m, dDA, sCountry, bCondMemo) + bCondMemo = not sFuncCond or globals()[sFuncCond](self, dDA, sCountry, bCondMemo) if bCondMemo: if cActionType == "-": # grammar error - nErrorStart = nOffset + m.start(eAct[0]) - nErrorEnd = nOffset + m.start(eAct[1]) + nErrorStart = nSentenceOffset + m.start(eAct[0]) + nErrorEnd = nSentenceOffset + m.start(eAct[1]) if nErrorStart not in dErrs or nPriority > dPriority[nErrorStart]: - dErrs[nErrorStart] = _createError(self.lToken, self.sSentence0, sWhat, nOffset, m, nErrorStart, nErrorEnd, sLineId, bUppercase, eAct[2], eAct[3], bIdRule, sOption, bContext) + dErrs[nErrorStart] = _createError(self, sWhat, nErrorStart, nErrorEnd, sLineId, bUppercase, eAct[2], eAct[3], bIdRule, sOption, bContext) dPriority[nErrorStart] = nPriority elif cActionType == "~": # text processor - self.lToken = _rewrite(self.lToken, sWhat, bUppercase) + self.lToken = _rewrite(self, sWhat, nErrorStart, nErrorEnd, bUppercase) + bChange = True + elif cActionType == "@": + # text processor + self.lToken = _rewrite(self, sWhat, nErrorStart, nErrorEnd, bUppercase) bChange = True elif cActionType == "=": # disambiguation - globals()[sWhat](self.lToken, dDA) + globals()[sWhat](self, dDA) elif cActionType == ">": # we do nothing, this test is just a condition to apply all following actions pass else: echo("# error: unknown action at " + sLineId) @@ -85,5 +89,53 @@ return d def _createDictError (self): d = {} return d + + +#### Common functions + +def option (): + pass + + +#### Analyse tokens + +def morph (): + pass + +def morphex (): + pass + +def analyse (): + pass + +def analysex (): + pass + + +#### Go outside scope + +def nextToken (): + pass + +def prevToken (): + pass + +def look (): + pass + +def lookAndCheck (): + pass + + +#### Disambiguator + +def select (): + pass + +def exclude (): + pass + +def define (): + pass