diff options
| author | Karl Heuer | 1996-02-16 00:36:52 +0000 |
|---|---|---|
| committer | Karl Heuer | 1996-02-16 00:36:52 +0000 |
| commit | ddfb0a34ce527efab19ee4458f8d31fd1bde5dc5 (patch) | |
| tree | dc102211ebe392f31e4c07ef35594de4d609ea97 | |
| parent | 090589dd2be896f762b91ddecc06a85cf0b484a2 (diff) | |
| download | emacs-ddfb0a34ce527efab19ee4458f8d31fd1bde5dc5.tar.gz emacs-ddfb0a34ce527efab19ee4458f8d31fd1bde5dc5.zip | |
Doc changes.
(decipher-char): Added defvar (and also for following variables).
(decipher--prev-char): Renamed from decipher-prev-char.
(decipher--digram): Renamed from digram.
(decipher--digram-list): Renamed from digram-list.
(decipher--before): Renamed from before-array.
(decipher--after): Renamed from after-array.
(decipher--freqs): Renamed from freq-array.
| -rw-r--r-- | lisp/play/decipher.el | 91 |
1 files changed, 54 insertions, 37 deletions
diff --git a/lisp/play/decipher.el b/lisp/play/decipher.el index f911a5631ca..fc78e83a966 100644 --- a/lisp/play/decipher.el +++ b/lisp/play/decipher.el | |||
| @@ -73,7 +73,11 @@ | |||
| 73 | 73 | ||
| 74 | ;;; Things To Do: | 74 | ;;; Things To Do: |
| 75 | ;; | 75 | ;; |
| 76 | ;; 1. More functions for analyzing ciphertext | 76 | ;; Email me if you have any suggestions or would like to help. |
| 77 | ;; But be aware that I work on Decipher only sporadically. | ||
| 78 | ;; | ||
| 79 | ;; 1. The consonant-line shortcut | ||
| 80 | ;; 2. More functions for analyzing ciphertext | ||
| 77 | 81 | ||
| 78 | ;;;=================================================================== | 82 | ;;;=================================================================== |
| 79 | ;;; Variables: | 83 | ;;; Variables: |
| @@ -174,6 +178,18 @@ list of such cons cells.") | |||
| 174 | 178 | ||
| 175 | (defvar decipher-pending-undo-list nil) | 179 | (defvar decipher-pending-undo-list nil) |
| 176 | 180 | ||
| 181 | ;; The following variables are used by the analysis functions | ||
| 182 | ;; and are defined here to avoid byte-compiler warnings. | ||
| 183 | ;; Don't mess with them unless you know what you're doing. | ||
| 184 | (defvar decipher-char nil | ||
| 185 | "See the functions decipher-loop-with-breaks and decipher-loop-no-breaks.") | ||
| 186 | (defvar decipher--prev-char) | ||
| 187 | (defvar decipher--digram) | ||
| 188 | (defvar decipher--digram-list) | ||
| 189 | (defvar decipher--before) | ||
| 190 | (defvar decipher--after) | ||
| 191 | (defvar decipher--freqs) | ||
| 192 | |||
| 177 | ;;;=================================================================== | 193 | ;;;=================================================================== |
| 178 | ;;; Code: | 194 | ;;; Code: |
| 179 | ;;;=================================================================== | 195 | ;;;=================================================================== |
| @@ -750,42 +766,43 @@ TOTAL is the total number of letters in the ciphertext." | |||
| 750 | ;; Perform frequency analysis on ciphertext. | 766 | ;; Perform frequency analysis on ciphertext. |
| 751 | ;; | 767 | ;; |
| 752 | ;; This function is called repeatedly with decipher-char set to each | 768 | ;; This function is called repeatedly with decipher-char set to each |
| 753 | ;; character of ciphertext. It uses decipher-prev-char to remember | 769 | ;; character of ciphertext. It uses decipher--prev-char to remember |
| 754 | ;; the previous ciphertext character. | 770 | ;; the previous ciphertext character. |
| 755 | ;; | 771 | ;; |
| 756 | ;; It builds several data structures, which must be initialized | 772 | ;; It builds several data structures, which must be initialized |
| 757 | ;; before the first call to decipher--analyze. The arrays are | 773 | ;; before the first call to decipher--analyze. The arrays are |
| 758 | ;; indexed with A = 0, B = 1, ..., Z = 25, SPC = 26 (if used). | 774 | ;; indexed with A = 0, B = 1, ..., Z = 25, SPC = 26 (if used). |
| 759 | ;; after-array: (initialize to zeros) | 775 | ;; decipher--after: (initialize to zeros) |
| 760 | ;; A vector of 26 vectors of 27 integers. The first vector | 776 | ;; A vector of 26 vectors of 27 integers. The first vector |
| 761 | ;; represents the number of times A follows each character, the | 777 | ;; represents the number of times A follows each character, the |
| 762 | ;; second vector represents B, and so on. | 778 | ;; second vector represents B, and so on. |
| 763 | ;; before-array: (initialize to zeros) | 779 | ;; decipher--before: (initialize to zeros) |
| 764 | ;; The same as after-array, but representing the number of times | 780 | ;; The same as decipher--after, but representing the number of |
| 765 | ;; the character precedes each other character. | 781 | ;; times the character precedes each other character. |
| 766 | ;; digram-list: (initialize to nil) | 782 | ;; decipher--digram-list: (initialize to nil) |
| 767 | ;; An alist with an entry for each digram (2-character sequence) | 783 | ;; An alist with an entry for each digram (2-character sequence) |
| 768 | ;; encountered. Each element is a cons cell (DIGRAM . FREQ), | 784 | ;; encountered. Each element is a cons cell (DIGRAM . FREQ), |
| 769 | ;; where DIGRAM is a 2 character string and FREQ is the number | 785 | ;; where DIGRAM is a 2 character string and FREQ is the number |
| 770 | ;; of times it occurs. | 786 | ;; of times it occurs. |
| 771 | ;; freq-array: (initialize to zeros) | 787 | ;; decipher--freqs: (initialize to zeros) |
| 772 | ;; A vector of 26 integers, counting the number of occurrences | 788 | ;; A vector of 26 integers, counting the number of occurrences |
| 773 | ;; of the corresponding characters. | 789 | ;; of the corresponding characters. |
| 774 | (setq digram (format "%c%c" decipher-prev-char decipher-char)) | 790 | (setq decipher--digram (format "%c%c" decipher--prev-char decipher-char)) |
| 775 | (incf (cdr (or (assoc digram digram-list) | 791 | (incf (cdr (or (assoc decipher--digram decipher--digram-list) |
| 776 | (car (push (cons digram 0) digram-list))))) | 792 | (car (push (cons decipher--digram 0) |
| 777 | (and (>= decipher-prev-char ?A) | 793 | decipher--digram-list))))) |
| 778 | (incf (aref (aref before-array (- decipher-prev-char ?A)) | 794 | (and (>= decipher--prev-char ?A) |
| 795 | (incf (aref (aref decipher--before (- decipher--prev-char ?A)) | ||
| 779 | (if (equal decipher-char ?\ ) | 796 | (if (equal decipher-char ?\ ) |
| 780 | 26 | 797 | 26 |
| 781 | (- decipher-char ?A))))) | 798 | (- decipher-char ?A))))) |
| 782 | (and (>= decipher-char ?A) | 799 | (and (>= decipher-char ?A) |
| 783 | (incf (aref freq-array (- decipher-char ?A))) | 800 | (incf (aref decipher--freqs (- decipher-char ?A))) |
| 784 | (incf (aref (aref after-array (- decipher-char ?A)) | 801 | (incf (aref (aref decipher--after (- decipher-char ?A)) |
| 785 | (if (equal decipher-prev-char ?\ ) | 802 | (if (equal decipher--prev-char ?\ ) |
| 786 | 26 | 803 | 26 |
| 787 | (- decipher-prev-char ?A))))) | 804 | (- decipher--prev-char ?A))))) |
| 788 | (setq decipher-prev-char decipher-char)) | 805 | (setq decipher--prev-char decipher-char)) |
| 789 | 806 | ||
| 790 | (defun decipher--digram-counts (counts) | 807 | (defun decipher--digram-counts (counts) |
| 791 | "Generate the counts for an adjacency list." | 808 | "Generate the counts for an adjacency list." |
| @@ -815,29 +832,29 @@ TOTAL is the total number of letters in the ciphertext." | |||
| 815 | (defun decipher-analyze-buffer () | 832 | (defun decipher-analyze-buffer () |
| 816 | "Perform frequency analysis and store results in statistics buffer. | 833 | "Perform frequency analysis and store results in statistics buffer. |
| 817 | Creates the statistics buffer if it doesn't exist." | 834 | Creates the statistics buffer if it doesn't exist." |
| 818 | (let ((decipher-prev-char (if decipher-ignore-spaces ?\ ?\*)) | 835 | (let ((decipher--prev-char (if decipher-ignore-spaces ?\ ?\*)) |
| 819 | (before-array (make-vector 26 nil)) | 836 | (decipher--before (make-vector 26 nil)) |
| 820 | (after-array (make-vector 26 nil)) | 837 | (decipher--after (make-vector 26 nil)) |
| 821 | (freq-array (make-vector 26 0)) | 838 | (decipher--freqs (make-vector 26 0)) |
| 822 | (total-chars 0) | 839 | (total-chars 0) |
| 823 | digram digram-list freq-list) | 840 | decipher--digram decipher--digram-list freq-list) |
| 824 | (message "Scanning buffer...") | 841 | (message "Scanning buffer...") |
| 825 | (let ((i 26)) | 842 | (let ((i 26)) |
| 826 | (while (>= (decf i) 0) | 843 | (while (>= (decf i) 0) |
| 827 | (aset before-array i (make-vector 27 0)) | 844 | (aset decipher--before i (make-vector 27 0)) |
| 828 | (aset after-array i (make-vector 27 0)))) | 845 | (aset decipher--after i (make-vector 27 0)))) |
| 829 | (if decipher-ignore-spaces | 846 | (if decipher-ignore-spaces |
| 830 | (progn | 847 | (progn |
| 831 | (decipher-loop-no-breaks 'decipher--analyze) | 848 | (decipher-loop-no-breaks 'decipher--analyze) |
| 832 | ;; The first character of ciphertext was marked as following a space: | 849 | ;; The first character of ciphertext was marked as following a space: |
| 833 | (let ((i 26)) | 850 | (let ((i 26)) |
| 834 | (while (>= (decf i) 0) | 851 | (while (>= (decf i) 0) |
| 835 | (aset (aref after-array i) 26 0)))) | 852 | (aset (aref decipher--after i) 26 0)))) |
| 836 | (decipher-loop-with-breaks 'decipher--analyze)) | 853 | (decipher-loop-with-breaks 'decipher--analyze)) |
| 837 | (message "Processing results...") | 854 | (message "Processing results...") |
| 838 | (setcdr (last digram-list 2) nil) ;Delete the phony "* " digram | 855 | (setcdr (last decipher--digram-list 2) nil) ;Delete the phony "* " digram |
| 839 | ;; Sort the digram list by frequency and alphabetical order: | 856 | ;; Sort the digram list by frequency and alphabetical order: |
| 840 | (setq digram-list (sort (sort digram-list | 857 | (setq decipher--digram-list (sort (sort decipher--digram-list |
| 841 | (lambda (a b) (string< (car a) (car b)))) | 858 | (lambda (a b) (string< (car a) (car b)))) |
| 842 | (lambda (a b) (> (cdr a) (cdr b))))) | 859 | (lambda (a b) (> (cdr a) (cdr b))))) |
| 843 | ;; Generate the frequency list: | 860 | ;; Generate the frequency list: |
| @@ -849,11 +866,11 @@ Creates the statistics buffer if it doesn't exist." | |||
| 849 | (while (>= (decf i) 0) | 866 | (while (>= (decf i) 0) |
| 850 | (setq freq-list | 867 | (setq freq-list |
| 851 | (cons (list (+ i ?A) | 868 | (cons (list (+ i ?A) |
| 852 | (aref freq-array i) | 869 | (aref decipher--freqs i) |
| 853 | (decipher--digram-total (aref before-array i) | 870 | (decipher--digram-total (aref decipher--before i) |
| 854 | (aref after-array i))) | 871 | (aref decipher--after i))) |
| 855 | freq-list) | 872 | freq-list) |
| 856 | total-chars (+ total-chars (aref freq-array i))))) | 873 | total-chars (+ total-chars (aref decipher--freqs i))))) |
| 857 | (save-excursion | 874 | (save-excursion |
| 858 | ;; Switch to statistics buffer, creating it if necessary: | 875 | ;; Switch to statistics buffer, creating it if necessary: |
| 859 | (set-buffer (decipher-stats-buffer t)) | 876 | (set-buffer (decipher-stats-buffer t)) |
| @@ -874,11 +891,11 @@ Creates the statistics buffer if it doesn't exist." | |||
| 874 | freq-list nil) | 891 | freq-list nil) |
| 875 | "\n\n") | 892 | "\n\n") |
| 876 | ;; Display list of digrams in order of frequency: | 893 | ;; Display list of digrams in order of frequency: |
| 877 | (let* ((rows (floor (+ (length digram-list) 9) 10)) | 894 | (let* ((rows (floor (+ (length decipher--digram-list) 9) 10)) |
| 878 | (i rows) | 895 | (i rows) |
| 879 | temp-list) | 896 | temp-list) |
| 880 | (while (> i 0) | 897 | (while (> i 0) |
| 881 | (setq temp-list digram-list) | 898 | (setq temp-list decipher--digram-list) |
| 882 | (while temp-list | 899 | (while temp-list |
| 883 | (insert (caar temp-list) | 900 | (insert (caar temp-list) |
| 884 | (format "%3d " | 901 | (format "%3d " |
| @@ -886,7 +903,7 @@ Creates the statistics buffer if it doesn't exist." | |||
| 886 | (setq temp-list (nthcdr rows temp-list))) | 903 | (setq temp-list (nthcdr rows temp-list))) |
| 887 | (delete-horizontal-space) | 904 | (delete-horizontal-space) |
| 888 | (insert ?\n) | 905 | (insert ?\n) |
| 889 | (setq digram-list (cdr digram-list) | 906 | (setq decipher--digram-list (cdr decipher--digram-list) |
| 890 | i (1- i)))) | 907 | i (1- i)))) |
| 891 | ;; Display adjacency list for each letter, sorted in descending | 908 | ;; Display adjacency list for each letter, sorted in descending |
| 892 | ;; order of the number of adjacent letters: | 909 | ;; order of the number of adjacent letters: |
| @@ -899,13 +916,13 @@ Creates the statistics buffer if it doesn't exist." | |||
| 899 | nil ;This letter was not used | 916 | nil ;This letter was not used |
| 900 | (setq i (- (car entry) ?A)) | 917 | (setq i (- (car entry) ?A)) |
| 901 | (insert ?\n " " | 918 | (insert ?\n " " |
| 902 | (decipher--digram-counts (aref before-array i)) ?\n | 919 | (decipher--digram-counts (aref decipher--before i)) ?\n |
| 903 | (car entry) | 920 | (car entry) |
| 904 | ": A B C D E F G H I J K L M N O P Q R S T U V W X Y Z *" | 921 | ": A B C D E F G H I J K L M N O P Q R S T U V W X Y Z *" |
| 905 | (format "%4d %4d %3d%%\n " | 922 | (format "%4d %4d %3d%%\n " |
| 906 | (third entry) (second entry) | 923 | (third entry) (second entry) |
| 907 | (/ (* 100 (second entry)) total-chars)) | 924 | (/ (* 100 (second entry)) total-chars)) |
| 908 | (decipher--digram-counts (aref after-array i)) ?\n)))) | 925 | (decipher--digram-counts (aref decipher--after i)) ?\n)))) |
| 909 | (setq buffer-read-only t) | 926 | (setq buffer-read-only t) |
| 910 | (set-buffer-modified-p nil) | 927 | (set-buffer-modified-p nil) |
| 911 | )) | 928 | )) |