Restructure the source code like the JS/Go versions#30
Merged
Conversation
1a8f6a7 to
82b5a6f
Compare
deldesir
added a commit
to deldesir/rivescript-python
that referenced
this pull request
Mar 21, 2026
Restructure the source code like the JS/Go versions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This brings a long-needed code structure update to the Python version to put it on par with what the JS and Go versions are doing.
Testing
You can install this branch with pip:
Code Reorganization
This mostly breaks the monolithic
rivescript.pyinto a handful of smaller, more focused pieces, and moves some functions around.rivescript.parsernow contains all the parsing logic used under the hood by functions likeload_file()andstream(). Itsparse()function is decoupled from RiveScript (apart from debug log methods) and it returns a data structure of what was parsed from the file. This may be useful to third-party developers if they just want to parse RiveScript code.rivescript.brainnow holds the logic for actually fetching a reply for the user. This includes the implementation of thereply()method and helper functions (tag processing, input message formatting, regexp compiling)rivescript.inheritanceincludes utility functions for crawling the inheritance tree to find triggers.rivescript.sortingincludes the functions for sorting replies.rivescript.utilshas the miscellaneous internal utility functions.The biggest change is with regards to how triggers are stored internally. The old way had the
self._topicsstructure contain a bunch of nested dictionaries, with keys like:This way had problems such as how to handle duplicate triggers and it made it messier to keep track of triggers that had
%Previoustags apart from those that do not. The new way keeps the triggers in an array, and includes a pointer to the trigger's data along with the text itself (no needing to do separate lookups! For example, there used to be athattrigmap that correlated%Previoustriggers with their data locations... and the inheritance functions used to have a method offind_trigger_by_inheritanceand that's no longer needed either).It now looks more like:
API Shuffling
Some functions were moved out of the
rivescript.rivescriptmodule and into their own, more relevant spots. Other functions were no longer needed and were removed.Most of these were internal, private use functions that you shouldn't have been using anyway, but here's the complete list:
rivescript.rs_versionattribute now belongs tobrain.rs_version_initTT()is now insorting.init_topic()and does a lot less work._sort_that_triggers()is removed._sort_trigger_set()is moved tosorting.sort_trigger_set()_sort_list()is moved tosorting.sort_list()_init_sort_track()is moved tosorting.init_sort_track()_format_message(),_getreply(),_substitute(),_do_expand_array(),_expand_array(),_reply_regexp(),_process_tags()and the implementation ofreply()have all been moved torivescript.brain_string_format,_word_count,_is_atomicand_strip_nastiesare all moved toutils_topic_triggersis now inrivescript.inheritance.get_topic_triggers_find_trigger_by_inheritanceis removed._get_topic_treeis now inrivesript.inheritance.get_topic_treeOther Changes
rivescript.interactivescript to useargparseinstead ofgetoptand add a pretty logo.shell.pyas an (easier?) way to invoke the interactive mode.Things To Do
At some point in the future I want to:
deparse()and related functions into its own modulerivescript.pythonas something likerivescript.lang.pythonfor consistency with the other implementations.