-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontar.js
More file actions
54 lines (47 loc) · 1.69 KB
/
contar.js
File metadata and controls
54 lines (47 loc) · 1.69 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
49
50
51
52
53
54
/**
* Programa que cuenta caracteres, palabras y frases de un texto o un archivo
* Autor: Sergio Ruiz
* Asignatura: SWCM
**/
var fs = require('fs');
var cuenta = {
leer_archivo : function(){
var datos;
if(process.argv.length!=3){
datos = ' ';
console.log('\n ****************************************************************\n Error en los argumentos. Uso: nodejs contar.js <Archivo a leer>\n ****************************************************************\n');
}
else{
datos = fs.readFileSync(process.argv[2],'utf-8');
}
return datos;
},
cuenta_caracteres : function (texto){
if(texto==null){
texto = this.leer_archivo();
}
console.log('Texto a analizar:\n---------------------\n' + texto + '\n---------------------\n');
var caracteres = texto.match(/[a-z,0-9,ñ¡!¿?"%&{}=#()'´:;.€@ºªç_üáéíóú-]/gim);
console.log('El texto tiene ' + caracteres.length + ' caracteres\n');
return caracteres.length;
},
cuenta_palabras : function(texto){
if(texto==null){
texto = this.leer_archivo();
}
console.log('Texto a analizar:\n---------------------\n' + texto + '\n---------------------\n');
var palabras = texto.match(/[a-zñçüáéíóú'´€@0-9]+(-[\n\r\f\v]*[a-zñçüáéíóú'´€@0-9]*)*/gim);
console.log('El texto tiene ' + palabras.length + ' palabras \n');
return palabras.length;
},
cuenta_frases : function(texto){
if(texto==null){
texto = this.leer_archivo();
}
console.log('Texto a analizar:\n---------------------\n' + texto + '\n---------------------\n');
var frases = texto.match(/[.!?;]+/gim);
console.log('El texto tiene ' + frases.length + ' frases\n');
return frases.length;
},
}
module.exports.cuenta = cuenta;