-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuentaPalabras.py
More file actions
executable file
·48 lines (39 loc) · 1.41 KB
/
cuentaPalabras.py
File metadata and controls
executable file
·48 lines (39 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python3
import operator
import sys
import re
from collections import defaultdict
def main():
'''
Flujo principal del programa
'''
filename = ''
dicc = defaultdict(int) # Si no hay clave, se utiliza int para inicializarla a 0 y recuperarla
# Parametros en sys.argv
if len(sys.argv) > 2:
print('Sintaxis incorrecta. Debe usar el programa del siguiente modo:')
print('cuentaPalabras.py [fichero] \n')
print('o bien\n')
print('python cuentaPalabras.py [fichero]\n')
print('(Los corchetes ([]) indican la opcionalidad de su contenido)')
exit(2)
elif len(sys.argv) == 1:
filename = input('Introduzca el nombre exacto del fichero a procesar: ')
exit(1)
else:
filename = sys.argv[1]
# with cierra el fichero
with open(filename, 'r') as fdesc:
linea = fdesc.readline().replace('\n', '')
while linea != '':
for palabra in re.findall(r'\b\w+\b', linea):
dicc[palabra] += 1
linea = fdesc.readline()
total = sum(dicc.values())
print(total)
listaOrdenada = sorted(list(dicc.items()), key=operator.itemgetter(1), reverse=True)
with open('informe.txt', 'w') as foutput:
for (palabra, valor) in listaOrdenada:
foutput.write('{0!s: <20} {1:d} {2:2.2%}\n'.format(palabra, valor, valor / total))
if __name__ == '__main__':
main()