Package: marc
Aboutmarc is aMarkovchain generator for Python and/or SwiftPythonInstall‡pipinstallmarcQuickstart:frommarcimportMarkovChainplayer_throws="RRRSRSRRPRPSPPRPSSSPRSPSPRRRPSSPRRPRSRPRPSSSPRPRPSSRPSRPRSSPRP"sequence=[throwforthrowinplayer_throws]# ['R', 'R', 'R', 'S', 'R', 'S', 'R', ...]chain=MarkovChain(sequence)chain.update("R","S")chain["R"]# {'P': 0.5, 'R': 0.25, 'S': 0.25}player_last_throw="R"player_predicted_next_throw=chain.next(player_last_throw)# 'P'counters={"R":"P","P":"S","S":"R"}counter_throw=counters[player_predicted_next_throw]# 'S'For more inspiration see thepython/examples/directorySwiftSPM:dependencies:[.package(url:"https://github.com/maxhumber/marc.git",.upToNextMajor(from:"22.5.0"))]Quickstart:importMarcletplayerThrows="RRRSRSRRPRPSPPRPSSSPRSPSPRRRPSSPRRPRSRPRPSSSPRPRPSSRPSRPRSSPRP"letsequence=playerThrows.map{String($0)}letchain=MarkovChain(sequence)chain.update("R","S")print(chain["R"])// [("P", 0.5), ("R", 0.25), ("S", 0.25)]letplayerLastThrow="R"letplayerPredictedNextThrow=chain.next(playerLastThrow)!letcounters=["R":"P","P":"S","S":"R"]letcounterThrow=counters[playerPredictedNextThrow]!print(counterThrow)// "S"For more inspiration see theswift/Examples/directoryAPI/ComparisonPythonSwiftImportfrom marc import MarkovChainimport MarcInitializeAchain = MarkovChain()chain = MarkovChain<String>()InitializeBchain = MarkovChain(["R", "P", "S"])let chain = MarkovChain(["R", "P", "S"])Update chainchain.update("R", "P")chain.update("R", "P")Lookup transitionschain["R"]chain["R"]Generate nextchain.next("R")chain.next("R")!WhyI built the first versions ofmarcin the Fall of 2019. Back then I created, and used, it as a teaching tool (for how to build and upload a PyPI package). Since March 2020 I've been spending less and less time with Python and more and more time with Swift... and so, just kind of forgot aboutmarc.Recently, I had an iOS project come up that needed some Markov chains. After surveying GitHub and not finding any implementations that I liked (forgetting that I had already rolled my own in Python) I started from scratch on a new implementation in Swift.Just as I was finishing the Swift package I re-discoveredmarc... I had a good laugh looking back through theoriginalPython library. My feelings about the code I wrote and my abilities in 2019 can be summarized in a picture:Unable to resist a good procrasticode™ project, I cross-ported the finished Swift package to Python and polished up both codebases and documentation into this mono repo.Honestly, I had a lot of fun trying to mirror the APIs as closely as possible while doing my best to keep the Python code "Pythonic" and the Swift code "Schwifty". The whole project/exercise was incredibly rewarding, interesting, and insightful. Crudely, here's how I found working on both packages:PythonLikeDislikedefaultdict!!Clunkysetup.pypackagingrandom.choice!Setting up and working with environmentsDictionary comprehensions + sorting__init__.pyand directory issuesSwiftLikeDislikePackage.swiftand packaging in generalDictionary performance sucks... (surprising!!)Don't have to think about environmentsNeed randomness? Too bad. Go roll it yourselfXCTestis nicer/easier thanunittest/pytestPlaygrounds aren't as good as Hydrogen/JupyterSo why? For fun! And procrastination. And, more seriously, because I needed some chains in Swift. And then, because I thought it could be interesting to create a Rosetta Stone for Python and Swift... So if you, Dear Reader, are looking to use Markov chains in your Python or Swift project, or are looking to jump to or from either language, I hope you find this useful.Warning‡marc 22.5+ is incompatible with marc 2.x