Guest User

Untitled

a guest
Dec 14th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.90 KB | None | 0 0
  1. #lang racket
  2.  
  3. (require racket/file)
  4. (require racket/string)
  5.  
  6. ;Text processing
  7.  
  8. (define text (file->string "push.txt"))
  9.  
  10. (define (preprocess str)
  11.   (map (lambda (s) (cdr (regexp-split " "(regexp-replace* #px"\\P{L&}+" s " "))))
  12.        (regexp-split "\\." str)))
  13.  
  14. ;List of expressions
  15. (define exprs (map (lambda (l) (map string-downcase l)) (preprocess text)))
  16.  
  17. ;List of all words from text
  18. (define words (map string-downcase (flatten exprs)))
  19.  
  20. ;Key:word value:frequency in text
  21. (define word-freq (make-hash))
  22.  
  23. (for ((i (in-range 0 (length exprs) 1)))
  24.   (for ((j (in-range 0 (length (list-ref exprs i)) 1)))
  25.     (let* ((w (list-ref (list-ref exprs i) j))
  26.            (wval (hash-ref word-freq w '())))
  27.       (if (null? wval)
  28.           (hash-set! word-freq w (list 1 (list (list i j))))
  29.           (hash-set! word-freq w (list (+ 1 (list-ref wval 0))
  30.                                        (cons (list i j) (list-ref wval 1))))))))
  31.  
  32. ;(define t (for/list ((w words)) (hash-set! word-freq w
  33. ;                            (+ 1 (hash-ref word-freq w 0)))))
  34.  
  35. (define wf-l (sort (hash->list word-freq) (lambda (a b) (> (first (cdr a))
  36.                                                            (first (cdr b))))))
  37. ;Build markov model for expressions as stochastic process
  38. ;(define (build-markov-model exprs)
  39.  
  40. (define (word-follow exprs index)
  41.   (let ((expr (list-ref exprs (first index))))
  42.     (if (< (second index) (- (length expr) 1))
  43.         (list-ref expr (+ (second index) 1))
  44.         'last)))
  45.  
  46. ;List of Word and List of following words
  47. (define follow-words
  48.   (map (lambda (l)
  49.          (list
  50.           (first l)
  51.           (map (curry word-follow exprs)
  52.                (third l))))
  53.        wf-l))
  54.  
  55. ;(define (list->freq-distr lst)
  56. ;  (for/list ((w (in-list (remove-duplicates lst))))
  57. ;    (cons w (exact->inexact (/ (count (curry equal? w) lst) (length lst))))))
  58. ;
  59. ;(define follow-words-distribution
  60. ;  (map (lambda (l)
  61. ;         (list
  62. ;          (first l)
  63. ;          (list->freq-distr (second l))))
  64. ;       follow-words))
  65.  
  66. ;(define fwd-hash (make-hash))
  67. ;
  68. ;(for ((wl (in-list follow-words-distribution)))
  69. ;  (hash-set! fwd-hash (car wl) (cdr wl)))
  70.  
  71. (define fw-hash (make-hash))
  72.  
  73. (for ((wl (in-list follow-words)))
  74.   (hash-set! fw-hash (first wl) (second wl)))
  75.  
  76. (define (choose-random lst)
  77.   (list-ref lst (random (length lst))))
  78.  
  79. (define (markov-sentence start)
  80.   (let* ((next (hash-ref fw-hash start 'last)))
  81.     (if (eq? next 'last)
  82.         '()
  83.         (append (list start)
  84.                 (markov-sentence (choose-random next))))))
  85. ;(printf "[~s]\n" next)
  86. ;(printf "[~s]\n" word)
  87. ;(printf "[~s]\n" followers)))
  88.  
  89.  
  90.  
  91. ;(define unique-words (remove-duplicates words))
  92.  
  93. (markov-sentence "герцог")
  94.  
  95. (define (markov-str start)
  96.   (string-append* (map (lambda (s) (string-join (list s " ") "")) (markov-sentence start))))
  97.  
  98. ;Usage:
  99. ;Type (markov-str "ваше слово") into repl
Add Comment
Please, Sign In to add comment