Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"Utility functions on strings" from __future__ import absolute_import
#***************************************************************************** # Copyright (C) 2007 David Kohel <kohel@maths.usyd.edu.au> # # Distributed under the terms of the GNU General Public License (GPL) # # http://www.gnu.org/licenses/ #*****************************************************************************
from sage.rings.all import RealField from .string_monoid_element import StringMonoidElement
def strip_encoding(S): """ The upper case string of S stripped of all non-alphabetic characters.
EXAMPLES::
sage: S = "The cat in the hat." sage: strip_encoding(S) 'THECATINTHEHAT' """ raise TypeError("Argument S (= %s) must be a string.")
def frequency_distribution(S, n=1, field=None): """ The probability space of frequencies of n-character substrings of S. """ S = list(S) S = [ S[i:i+n] for i in range(len(S)-n+1) ] else: raise TypeError("Argument S (= %s) must be a string, list, or tuple.")
def coincidence_index(S,n=1): """ The coincidence index of the string S.
EXAMPLES::
sage: S = strip_encoding("The cat in the hat.") sage: coincidence_index(S) 0.120879120879121 """ try: S.coincidence_index(n) except AttributeError: raise TypeError("Argument S (= %s) must be a string.") else:
def coincidence_discriminant(S,n=2): """ Input: A tuple of strings, e.g. produced as decimation of transposition ciphertext, or a sample plaintext. Output: A measure of the difference of probability of association of character pairs, relative to their independent one-character probabilities.
EXAMPLES::
sage: S = strip_encoding("The cat in the hat.") sage: coincidence_discriminant([ S[i:i+2] for i in range(len(S)-1) ]) 0.0827001855677322 """ raise TypeError("Argument S (= %s) must be a list or tuple" % S) raise ValueError("Argument n (= %s) is only implemented for n = 2" % n) raise TypeError("Argument S (= %s) must be a list of strings.") raise ValueError("Argument S (= %s) must be a list of strings of length 2" % S) M = S[0].parent() n = M.ngens() return sum([ (XX(M([i,j]))-X1[0](M([i]))*X1[1](M([j])))**2 for i in range(n) for j in range(n) ]) |