mad_alien

Small markov generator thingy

Dec 19th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. #  markov.py
  5. #  
  6. #  Copyleft 2015 Trellohim
  7. #  
  8. #  This program is free software; you can redistribute it and/or modify
  9. #  it under the terms of the GNU General Public License as published by
  10. #  the Free Software Foundation; either version 2 of the License, or
  11. #  (at your option) any later version.
  12. #  
  13. #  This program is distributed in the hope that it will be useful,
  14. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #  GNU General Public License for more details.
  17. #  
  18. #  You should have received a copy of the GNU General Public License
  19. #  along with this program; if not, write to the Free Software
  20. #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. #  MA 02110-1301, USA.
  22. #  
  23. #  
  24.  
  25. import random      
  26. malegothnames=  ' adalfuns adalrik alaric alarica alhreiks alhvaharyis amalareiks andagis ansila arareiks athalagild athalareiks athalaric athanagild athanareiks athanaric atta audo audoacer audvakr austraguta ausvinthus avagis badwila beremud botheric childefonsus chindasvinth chindaswintha dag eboric ebrimuth ediulf egica eiriks ermanaric ermenigeld ermingild erminigeld euric eutharic evermud evoric ferhonanths frideger frithigern fritigern geberic gisalrico giselric haduswinth hairuwulf hathus hermanaric hermanrich hermangild hermengild hermenigildo hildefons hildefuns hunuil ildefons ildefónso kindaswinth kunimund leovigild leovigildo liuva odoacer odovacar recimir richila ricimar roderic suintila svinthila tautila theoderic theodoric theudefred theudis thiudareiks thorismund thurismod thurismund tius totila ulphilas unwen veremundo vermundo vulfila wallia '
  27. class MarkovGenerator:
  28.     def __init__(self,dicti=' as long as the iterator returns the same thing a list would during iteration '):
  29.         derivationslist=[(dicti[i:i+2],dicti[i+2]) for i in range(len(dicti)-2)]
  30.         self.lexicon={}
  31.         #couldn't find a nice lambda fuction derivation
  32.         for i in derivationslist:
  33.             digram,prod=i
  34.             if digram not in self.lexicon:
  35.                 self.lexicon[digram]=[]
  36.             self.lexicon[digram].append(prod)  
  37.     def spew_word(self):
  38.         #and there is a lambda
  39.         filfun=lambda x: x[0]==' '
  40.         initials=[i for i in filter(filfun,[j for j in self.lexicon])]
  41.         starting=random.choice(initials)
  42.         production=starting
  43.         #cannot find nice, smart derivations, go the stupid way
  44.         while starting[1]!=' ':
  45.             newsymbol=random.choice(self.lexicon[starting])
  46.             production+=newsymbol
  47.             starting=starting[1]+newsymbol
  48.         return production.strip().title()
  49.    
  50.    
  51. def main():
  52.     mylex=MarkovGenerator(malegothnames)
  53.     for i in range(50): print (mylex.spew_word())
  54.     return 0
  55.  
  56. if __name__ == '__main__':
  57.     main()
Advertisement
Add Comment
Please, Sign In to add comment