Kitomas

CrapGPT v1

Apr 25th, 2025
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from sys import argv, stderr
  4. from os import system as cmd
  5. from math import floor
  6. from os.path import exists
  7. from pprint import pprint
  8. from random import choices, randint
  9.  
  10.  
  11.  
  12. def errFatal(errorString, returnCode = -1):
  13.     print('ERROR: '+errorString, file=stderr)
  14.     exit(returnCode)
  15.    
  16. def rnd(x): return floor(x+0.5) # Python's built-in rounding is stupid
  17.  
  18. def toInt(x):
  19.     try:              return rnd(eval(x))
  20.     except Exception: return None
  21.  
  22.  
  23.  
  24. wordCount   = toInt(argv[1])  if len(argv) > 1 else 50
  25. fileIn_name = argv[2]         if len(argv) > 2 else 'input.txt'
  26. word        = argv[3].lower() if len(argv) > 3 else None
  27.  
  28. if wordCount is None:
  29.     errFatal('Word count provided is not an integer value')
  30.  
  31. if not exists(fileIn_name):
  32.     errFatal(f'File "{fileIn_name}" doesn\'t exist!')
  33.  
  34.  
  35.  
  36.  
  37.  
  38. def strip_all(s):
  39.     return s.strip(" \"'!?.,;:").lower()
  40.  
  41. def split_all(s):
  42.     # Split by line breaks and spaces
  43.     l_0 = ( (' ').join(s.splitlines()) ).split(' ')
  44.     # Split by dashes
  45.     l_1 = ( ('-').join(l_0) ).split('-')
  46.     # Strip each string in list, while culling empty strings
  47.     return [strip_all(_s) for _s in l_1 if _s != '']
  48.  
  49.  
  50.  
  51.  
  52.  
  53. def convert_word_dict(d):
  54.     next_words = []
  55.     weights    = []
  56.    
  57.     for k in d:
  58.         next_words.append(k)
  59.         weights.append(d[k])
  60.        
  61.     return [next_words, weights]
  62.  
  63.  
  64.  
  65. def build_dictionary(l):
  66.     if len(l) <= 1: return None
  67.  
  68.     dict_a = {}
  69.  
  70.     for i in range(len(l)-1):
  71.         current = l[i  ]
  72.         next    = l[i+1]
  73.        
  74.         if not current in dict_a:
  75.             dict_a[current] = {}
  76.            
  77.         if not next in dict_a[current]:
  78.             dict_a[current][next] = 0
  79.            
  80.         dict_a[current][next] += 1
  81.  
  82.     dict_b = {}
  83.    
  84.     for word in dict_a:
  85.         dict_b[word] = convert_word_dict(dict_a[word])
  86.  
  87.     return dict_b
  88.  
  89.  
  90.  
  91. def get_next_word(word, dictionary):
  92.     if not word in dictionary: return None
  93.     words_and_weights = dictionary[word]
  94.     return choices(words_and_weights[0],
  95.                    words_and_weights[1], k=1)[0]
  96.  
  97.  
  98.  
  99.  
  100.  
  101. if __name__ == '__main__':
  102.     file = open(fileIn_name, 'r')
  103.     data = split_all(file.read())
  104.     file.close()
  105.    
  106.     dict = build_dictionary(data)
  107.    
  108.     if word is None:
  109.         word = data[ randint(0,len(data)-1) ]
  110.        
  111.     if get_next_word(word, dict) is None:
  112.         errFatal(f'"{word}" does not have an entry in the word dictionary!')
  113.    
  114.     for i in range(wordCount):
  115.         print(word, end=' ')
  116.         word = get_next_word(word, dict)
  117.         if word is None: word = data[ randint(0,len(data)-1) ]
  118.  
  119.  
  120.  
  121. '''
  122. 'youth': {'and': 1,
  123.          'heroes': 1,
  124.          'inclined': 1,
  125.          'into': 1,
  126.          'of': 1,
  127.          'was': 2,
  128.          'who': 1,
  129.          'with': 1},
  130. '''
Advertisement
Add Comment
Please, Sign In to add comment