Package: automata-tools
Automata ToolsTools to build automata from your custom rule.This package provides a set of handy tools to programmatically build automata, so you can build NFA、DFA、MinimizedDFA、WFA from any custom rules.UsageInstallcondainstall-cconda-forgeautomata-tools# not available yet# orpipinstallautomata-toolsImportSee example inexamples/NFAfromCustomRule.pyfromtypingimportListfromautomata_toolsimportBuildAutomata,Automataautomata:List[Automata]=[]BuildAutomatacharacterStructBuild simple(0)-[a]->(1)automataautomata.append(BuildAutomata.characterStruct(char))unionStructBuild automata that is an "or" of two sub-automata(1)<-[a]-(0)-[b]->(1)# to match "a|b"a=automata.pop()b=automata.pop()ifoperator=="|":automata.append(BuildAutomata.unionStruct(b,a))concatenationStructBuild automata that is an "and" of two sub-automata(0)-[a]->(1)-[b]->(2)# to match "ab"a=automata.pop()b=automata.pop()automata.append(BuildAutomata.concatenationStruct(b,a))starStructBuild automata that looks like the "Kleene closure"# to match "a*"ifoperator=="*":a=automata.pop()automata.append(BuildAutomata.starStruct(a))skipStructBuild automata that looks like the "Kleene closure" but without the loop back(1)<-[ε]-(2), so it only match the token once at most.# to match "a*"ifoperator=="?":a=automata.pop()automata.append(BuildAutomata.skipStruct(a))repeatRangeStructBuild automata that will match the same token for several times(0)-[a]->(1)-[a]->(2)-[a]->(3)# to match "a{3}"repeatedAutomata=BuildAutomata.repeatStruct(automata,3)repeatStructBuild automata that will match the same token for n to m times(0)-[a]->(1)-[a]->(4), (0)-[a]->(2)-[a]->(3)-[a]->(4)# to match "a{2,3}"repeatedAutomata=BuildAutomata.repeatRangeStruct(automata,2,3)AutomataSee example infeatures/steps/customRule.pyfromautomata_toolsimportDFAFromNFA,Automatafromyour_implementationimportNFAFromRegex,executornfa:Automata=NFAFromRegex().buildNFA(rule)minDFA:Automata=DFAFromNFA(nfa).getMinimizedDFA()minDFA.setExecuter(executor)print(minDFA.execute(someText))whereexecutoris a function like the one inexamples/NFAfromCustomRule.py:defexecutor(tokens,startState,finalStates,transitions):returnTruesetExecuterSet an executor to the automata that can freely use state and transition of the automata, and return a boolean value.fromautomata_toolsimportIAutomataExecutordefaultExecuter:IAutomataExecutor=lambdatokens,startState,finalStates,transitions:TrueminDFA.setExecuter(defaultExecuter)setTokenizerSet an tokenizer to the automata that can transform string to list of string token, which will be used by the executer.minDFA.setExecuter(lambdainput:input.split(' ')[::-1])NFAtoDFAMake automata state transitions not so ambiguousnfa=NFAFromRegex().buildNFA(rule)dfa=NFAtoDFA(nfa)DFAtoMinimizedDFAAllow you minify Automata statenfa=NFAFromRegex().buildNFA(rule)minDFA=DFAtoMinimizedDFA(NFAtoDFA(nfa))Weighted Finite AutomataWFA, it can execute automata use matrix multiplication, so it can be very fast compare to brute force execution, especially when state space is large.fromautomata_toolsimportWFA,get_word_to_index_,wordToIndex=get_word_to_index([ruleParser(context.rule),tokenizer(text)])wfa=WFA(minDFA,wordToIndex,dfa_to_tensor)wfa.execute(text)get_word_to_indexGiven[['token', 'another'], ['token_in_rule']], return something like{'token':0,'another':1,...}So we can translate automata state to a matrix.WFAGiven an automata, a word index like{'token': 0, 'another': 1, ...}, and a function that transform automata to tensor (see example atcustomRuleDFAToTensor), return a WFA instance.DevelopmentEnvironmentCreate environment from the text file:condaenvcreate--fileautomataTools-env.txt
condaactivateautomataToolsSave env file:conda list --explicit > automataTools-env.txtPython PathCreate a.envfile with contentPYTHONPATH=automataToolsPublishTo pypirm-rf./dist&&rm-rf./build&&rm-rf./automata_tools.egg-info&&python3setup.pysdistbdist_wheel&&twineuploaddist/*To Conda# I'm learning how to do...ResourcesAutomata Theory Course SlidesProbably the original referencesource