Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. from operator import itemgetter
  2. from string import lower
  3. import sys
  4.  
  5. if len(sys.argv)<2:
  6.     print 'Use: python frecuencia.py -h, para mostrar ayuda'
  7.     exit(1)
  8. if sys.argv[1] == '-h':
  9.     print 'Use: python frecuencia.py [opciones]'
  10.     print '-h\thelp'
  11.     print '----------------------------------'
  12.     print '-t [texto]\tPara realizar el analisis sobre un texto metido a mano(entrecomillado en caso de tener espacios entre palabras)'
  13.     print '-f [file]\tPara realizar el analisis sobre un texto en un fichero'
  14.     exit(1)
  15.    
  16. diccionario ={'a':0, 'b':0, 'c':0, 'd':0, 'e':0, 'f':0, 'g':0, 'h':0, 'i':0, 'j':0, 'k':0, 'l':0, 'm':0, 'n':0, 'o':0, 'p':0, 'q':0, 'r':0, 's':0, 't':0, 'u':0, 'v':0, 'w':0, 'x':0, 'y':0, 'z':0,' ':0}
  17.  
  18.  
  19. if sys.argv[1] == '-t':
  20.     text = sys.argv[2]
  21.    
  22. elif sys.argv[1] == '-f':
  23.     archivo = sys.argv[2]
  24.     text=''
  25.     try:
  26.         file1=open(archivo,'at+')
  27.     except:
  28.         print 'No existe el fichero '.archivo
  29.         exit(1)
  30.     while True:
  31.         foo = file1.readline()
  32.         if len(foo)<=0:
  33.             break
  34.         foo = foo[0:len(foo)-2]
  35.         text = text+foo
  36.  
  37. else:
  38.     print 'Use: python frecuencia.py [opciones]'
  39.     print '-h\thelp'
  40.     print '----------------------------------'
  41.     print '-t [texto]\tPara realizar el analisis sobre un texto metido a mano(entre comillado en cado de tener espacios entre palabras'
  42.     print '-f [file]\tPara realizar el analisis sobre un texto en un fichero'
  43.     exit(1)
  44.  
  45. print 'ANALISIS DEL TEXTO CIFRADO:'
  46. print text 
  47.  
  48. #Pasar a minusculas
  49. text1=''
  50. for x in text:
  51.     text1 = text1 +(lower(x))  
  52. text = text1
  53. #Letra a letra
  54. for x in text:
  55.     diccionario[x]+=1
  56. print'\nLETRAS'
  57. for c in range(97,123):
  58.     print '\033[1;91m'+chr(c)+':'+'\033[m', diccionario[chr(c)],'\t',
  59.     if (c-96)%10 == 0:
  60.         print ('')
  61. #Parejas
  62. i=0
  63. parejas = []
  64. for x in range(0,len(text)-1):
  65.     if (text[x]!=' ') and (text[x+1]!=' '):
  66.         parejas.append((text[x]+text[x+1]))
  67.  
  68. print '\n\nPAREJAS'
  69. l = []
  70. for c in range(0,len(parejas)):
  71.     #print '\033[1;91m'+l[c][0]+':'+'\033[m', l[c][1],'\t',
  72.     l.append((parejas[c],parejas.count(parejas[c])))
  73.     l1 = l[0:len(l)-1]
  74.     for x in l1:
  75.         if x[0] == parejas[c]:
  76.             l.pop()
  77.  
  78. l.sort(key=itemgetter(1))
  79. l.reverse()
  80. for c in range(0,len(l)):  
  81.     print '\033[1;91m'+l[c][0]+':'+'\033[m', l[c][1],'\t',
  82.     if (c+1)%10 == 0:
  83.         print '';
  84.    
  85. print ('')
  86. print 'xxxxxxxxxxxxx'
  87. print 'FAT by xgusix'
  88. print 'xxxxxxxxxxxxx'
  89. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement