-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail-extraction.js
More file actions
118 lines (86 loc) · 2.99 KB
/
email-extraction.js
File metadata and controls
118 lines (86 loc) · 2.99 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const fs = require('fs')
const readline = require('readline-sync');
let inputFile = fs.readFileSync('C:\\Users\\bharam\\Downloads\\test.txt', { encoding: 'utf8' });
//console.log(inputFile);
//checking the number of times @softwire.com has appeared
function countNumberOfIds(inputText) {
let counter = 0;
for (let i = 0; i < inputText.length; i++) {
if ((inputText.substring(i, i + 13) == '@softwire.com')) {
counter++;
}
}
return counter;
}
function countNumberOfEmailIds(inputText) {
return inputText.length;
}
let countId = countNumberOfIds(inputFile);
console.log(`No. of email ids present with @softwire.com: ${countId}`);
let softwireEmailId = inputFile.match(/(@softwire.com)/g);
let countWithReg = countNumberOfEmailIds(softwireEmailId);
console.log(`No. of email ids present with @softwire.com: ${countWithReg}`);
//console.log(softwireEmailId);
//counting the number of times any email id has appeared
let emailIdMatch = inputFile.match(/@[A-Za-z0-9]+.[A-Za-z.]+(?=\s)/g);
//console.log(emailIdMatch);
let countNoOfIds = countNumberOfEmailIds(emailIdMatch);
console.log(`No. of email ids present : ${countNoOfIds}`);
//creating a dictionary
let countOfRepetition = [];
addIt = 0;
let repeatedIds = [...new Set(emailIdMatch)];
//console.log(repeatedIds);
for (let i = 0; i < repeatedIds.length; i++) {
for (let j = 0; j < emailIdMatch.length; j++) {
if (repeatedIds[i] === emailIdMatch[j]) {
addIt++;
}
}
countOfRepetition.push(addIt);
addIt = 0;
}
//console.log(countOfRepetition);
let idsName = [];
for (let i = 0; i < repeatedIds.length; i++) {
idsName[i] = repeatedIds[i].slice(1);
}
//console.log(idsName);
let dictionary = {};
for (let i = 0; i < idsName.length; i++) {
dictionary[idsName[i]] = countOfRepetition[i];
}
console.log(dictionary);
//sorting the array to display maxoccured domain
let dictionaryArray = [];
for (let i = 0; i < idsName.length; i++) {
dictionaryArray[i] = [idsName[i], countOfRepetition[i]];
}
//console.log(dictionaryArray);
let tempId;
let tempValue;
let sortedArray = [];
for (let i = 0; i < dictionaryArray.length; i++) {
for (let j = i + 1; j < dictionaryArray.length; j++) {
if (dictionaryArray[i][1] < dictionaryArray[j][1]) {
tempId = dictionaryArray[i][0];
tempValue = dictionaryArray[i][1];
dictionaryArray[i] = [dictionaryArray[j][0], dictionaryArray[j][1]];
dictionaryArray[j] = [tempId, tempValue];
}
}
}
//console.log(dictionaryArray);
//displaying top 10 common domains
console.log('The 10 most common domains are:');
for (let i = 0; i < 10; i++) {
console.log(dictionaryArray[i][0]);
}
//displaying domains that exceed the freq entered
console.log('Enter the minimum frequency at which the domain names are used:');
let frequency = readline.prompt();
for (let i = 0; i < dictionaryArray.length; i++) {
if (dictionaryArray[i][1]>=frequency){
console.log(dictionaryArray[i][0]);
}
}