SlideShare a Scribd company logo
1 of 11
Download to read offline
this is my code to count the frequency of words in a text file
#define _GNU_SOURCE
#include
#include
#include
#include
/******************************
here is the Structure for storing words
received from each thread
******************************/
struct return_vall{
char wordlist[100][100];
int count[100];
} *arr;
pthread_t *threads;
int lines;
/*****************************
here are Aggregates Frequency Sum of
different threads
*****************************/
int aggregate(char *a)
{
int sum = 0;
int i=0;
while(1)
{
if(i==lines) break;
int count = 0;
while(1)
{
if(arr[i].count[count] == -1)
{
break;
}
if(strcmp(a,arr[i].wordlist[count]) == 0)
{
sum += arr[i].count[count];
arr[i].count[count] = 0;
}
count++;
}
i++;
}
return sum;
}
/**************************
Function thta are executed by each
thread on seperate line.
**************************/
void *word_count(void* num)
{
int *ln = num;
unsigned int line_number = *ln;
line_number++;
//printf("Thread %d ",line_number);
char cmd_p1[9] = "sed -n '0";
char cmd_p2[2];
sprintf(cmd_p2,"%d",line_number);
char cmd_p3[14] = "p' 'file.txt'0";
char command[100];
command[0] = '0';
strcat(command,cmd_p1);
strcat(command,cmd_p2);
strcat(command,cmd_p3);
//usleep(line_number);
char cmd[100] = " | tr [:space:] ' ' | grep -v '^s*$' | sort | uniq -c | sort0";
strcat(command,cmd);
//printf("%s ",command);
//fflush(stdout);
FILE *in;
in= popen(command, "r");
rewind(in);
char buff[50];
int counter = 0;
while(fgets(buff,sizeof(buff),in))
{
char c=' ';
int i = 0;
int cnt = atoi(buff);
arr[line_number-1].count[counter] = cnt;
while(c!='0')
{
c=buff[i];
buff[i]=buff[i+6];
i++;
}
int cnnt = 0;
while(c!=' ')
{
c = buff[cnnt];
cnnt++;
}
i=0;
while(c!='0')
{
c=buff[i];
buff[i]=buff[i+cnnt];
i++;
}
sprintf(arr[line_number-1].wordlist[counter],"%s",buff);
//printf("%d %s",arr[line_number-1].count[counter],arr[line_number-
1].wordlist[counter]);
counter++;
}
arr[line_number-1].count[counter] = -1;
fclose(in);
return NULL;
}
int main(void)
{
FILE *fp;
fp = fopen("file.txt","r");
char a[200];
lines = 0;
void *status = NULL;
while(fgets(a,sizeof(a),fp) !=NULL)
{
lines++;
}
fclose(fp);
arr = (struct return_vall*) calloc(lines, sizeof(struct return_vall) * lines);
threads = malloc(sizeof(pthread_t)*lines);
int i;
for(i=0;i= 0)
{
if(arr[i].count[ctr] > 0)
{
strcpy(final[final_ctr],arr[i].wordlist[ctr]);
final_cnt[final_ctr] = arr[i].count[ctr];
arr[i].count[ctr] = 0;
int sum = aggregate(arr[i].wordlist[ctr]);
final_cnt[final_ctr] += sum;
printf("%d %s",final_cnt[final_ctr],final[final_ctr]);
}
ctr++; final_ctr++;
}
i++;
if(i==lines)
{
break;
}
}
free(arr);
return 0;
}
Solution
this is my code to count the frequency of words in a text file
#define _GNU_SOURCE
#include
#include
#include
#include
/******************************
here is the Structure for storing words
received from each thread
******************************/
struct return_vall{
char wordlist[100][100];
int count[100];
} *arr;
pthread_t *threads;
int lines;
/*****************************
here are Aggregates Frequency Sum of
different threads
*****************************/
int aggregate(char *a)
{
int sum = 0;
int i=0;
while(1)
{
if(i==lines) break;
int count = 0;
while(1)
{
if(arr[i].count[count] == -1)
{
break;
}
if(strcmp(a,arr[i].wordlist[count]) == 0)
{
sum += arr[i].count[count];
arr[i].count[count] = 0;
}
count++;
}
i++;
}
return sum;
}
/**************************
Function thta are executed by each
thread on seperate line.
**************************/
void *word_count(void* num)
{
int *ln = num;
unsigned int line_number = *ln;
line_number++;
//printf("Thread %d ",line_number);
char cmd_p1[9] = "sed -n '0";
char cmd_p2[2];
sprintf(cmd_p2,"%d",line_number);
char cmd_p3[14] = "p' 'file.txt'0";
char command[100];
command[0] = '0';
strcat(command,cmd_p1);
strcat(command,cmd_p2);
strcat(command,cmd_p3);
//usleep(line_number);
char cmd[100] = " | tr [:space:] ' ' | grep -v '^s*$' | sort | uniq -c | sort0";
strcat(command,cmd);
//printf("%s ",command);
//fflush(stdout);
FILE *in;
in= popen(command, "r");
rewind(in);
char buff[50];
int counter = 0;
while(fgets(buff,sizeof(buff),in))
{
char c=' ';
int i = 0;
int cnt = atoi(buff);
arr[line_number-1].count[counter] = cnt;
while(c!='0')
{
c=buff[i];
buff[i]=buff[i+6];
i++;
}
int cnnt = 0;
while(c!=' ')
{
c = buff[cnnt];
cnnt++;
}
i=0;
while(c!='0')
{
c=buff[i];
buff[i]=buff[i+cnnt];
i++;
}
sprintf(arr[line_number-1].wordlist[counter],"%s",buff);
//printf("%d %s",arr[line_number-1].count[counter],arr[line_number-
1].wordlist[counter]);
counter++;
}
arr[line_number-1].count[counter] = -1;
fclose(in);
return NULL;
}
int main(void)
{
FILE *fp;
fp = fopen("file.txt","r");
char a[200];
lines = 0;
void *status = NULL;
while(fgets(a,sizeof(a),fp) !=NULL)
{
lines++;
}
fclose(fp);
arr = (struct return_vall*) calloc(lines, sizeof(struct return_vall) * lines);
threads = malloc(sizeof(pthread_t)*lines);
int i;
for(i=0;i= 0)
{
if(arr[i].count[ctr] > 0)
{
strcpy(final[final_ctr],arr[i].wordlist[ctr]);
final_cnt[final_ctr] = arr[i].count[ctr];
arr[i].count[ctr] = 0;
int sum = aggregate(arr[i].wordlist[ctr]);
final_cnt[final_ctr] += sum;
printf("%d %s",final_cnt[final_ctr],final[final_ctr]);
}
ctr++; final_ctr++;
}
i++;
if(i==lines)
{
break;
}
}
free(arr);
return 0;
}

More Related Content

Similar to this is my code to count the frequency of words in a text file#.pdf

ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.ppt
LokeshK66
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
arakalamkah11
 

Similar to this is my code to count the frequency of words in a text file#.pdf (20)

Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Complete the C++ program and implement the routines that are n.docx
    Complete the C++ program and implement the routines that are n.docx    Complete the C++ program and implement the routines that are n.docx
Complete the C++ program and implement the routines that are n.docx
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
Advanced Shell Scripting
Advanced Shell ScriptingAdvanced Shell Scripting
Advanced Shell Scripting
 
Library Managment System - C++ Program
Library Managment System - C++ ProgramLibrary Managment System - C++ Program
Library Managment System - C++ Program
 
Data type in c
Data type in cData type in c
Data type in c
 
ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.ppt
 
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdfg++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
g++ -o simpleVector.exe simpleVector.cpp #include stdio.h #i.pdf
 
Pointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxPointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptx
 
slides3_077.ppt
slides3_077.pptslides3_077.ppt
slides3_077.ppt
 
Multithreaded sockets c++11
Multithreaded sockets c++11Multithreaded sockets c++11
Multithreaded sockets c++11
 
STACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTSTACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LIST
 
Regular Expression (RegExp)
Regular Expression (RegExp)Regular Expression (RegExp)
Regular Expression (RegExp)
 

More from aquapariwar

First, lets find out what we need.The question asks for the empi.pdf
First, lets find out what we need.The question asks for the empi.pdfFirst, lets find out what we need.The question asks for the empi.pdf
First, lets find out what we need.The question asks for the empi.pdf
aquapariwar
 
DiversityDiversity scheme refers to a method for improving the re.pdf
DiversityDiversity scheme refers to a method for improving the re.pdfDiversityDiversity scheme refers to a method for improving the re.pdf
DiversityDiversity scheme refers to a method for improving the re.pdf
aquapariwar
 
AnswerKidneys are the functional units of renal system of rom uri.pdf
AnswerKidneys are the functional units of renal system of rom uri.pdfAnswerKidneys are the functional units of renal system of rom uri.pdf
AnswerKidneys are the functional units of renal system of rom uri.pdf
aquapariwar
 
A)ANS)In this TCPIP - System Access Layer, you will find out ab.pdf
A)ANS)In this TCPIP - System Access Layer, you will find out ab.pdfA)ANS)In this TCPIP - System Access Layer, you will find out ab.pdf
A)ANS)In this TCPIP - System Access Layer, you will find out ab.pdf
aquapariwar
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
aquapariwar
 
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf
aquapariwar
 
1. Identify five differences between DNA replication and gene transc.pdf
1. Identify five differences between DNA replication and gene transc.pdf1. Identify five differences between DNA replication and gene transc.pdf
1. Identify five differences between DNA replication and gene transc.pdf
aquapariwar
 
1) MODERN TIMES is a very well prepared movie that describes the mis.pdf
1) MODERN TIMES is a very well prepared movie that describes the mis.pdf1) MODERN TIMES is a very well prepared movie that describes the mis.pdf
1) MODERN TIMES is a very well prepared movie that describes the mis.pdf
aquapariwar
 
Acids are substances that form hydrogen ions, H+(.pdf
                     Acids are substances that form hydrogen ions, H+(.pdf                     Acids are substances that form hydrogen ions, H+(.pdf
Acids are substances that form hydrogen ions, H+(.pdf
aquapariwar
 
ANSWER1.Apocalypse Now film overall formIt is the height o.pdf
ANSWER1.Apocalypse Now film overall formIt is the height o.pdfANSWER1.Apocalypse Now film overall formIt is the height o.pdf
ANSWER1.Apocalypse Now film overall formIt is the height o.pdf
aquapariwar
 
4 water molecules are like a magnet because beca.pdf
                     4 water molecules are like a magnet because beca.pdf                     4 water molecules are like a magnet because beca.pdf
4 water molecules are like a magnet because beca.pdf
aquapariwar
 

More from aquapariwar (19)

First, lets find out what we need.The question asks for the empi.pdf
First, lets find out what we need.The question asks for the empi.pdfFirst, lets find out what we need.The question asks for the empi.pdf
First, lets find out what we need.The question asks for the empi.pdf
 
DiversityDiversity scheme refers to a method for improving the re.pdf
DiversityDiversity scheme refers to a method for improving the re.pdfDiversityDiversity scheme refers to a method for improving the re.pdf
DiversityDiversity scheme refers to a method for improving the re.pdf
 
due to repulsions between lone pair and bond pair of electrons on Te.pdf
due to repulsions between lone pair and bond pair of electrons on Te.pdfdue to repulsions between lone pair and bond pair of electrons on Te.pdf
due to repulsions between lone pair and bond pair of electrons on Te.pdf
 
AnswerKidneys are the functional units of renal system of rom uri.pdf
AnswerKidneys are the functional units of renal system of rom uri.pdfAnswerKidneys are the functional units of renal system of rom uri.pdf
AnswerKidneys are the functional units of renal system of rom uri.pdf
 
a. In puple bacteria P870 is present. External electron donors are h.pdf
a. In puple bacteria P870 is present. External electron donors are h.pdfa. In puple bacteria P870 is present. External electron donors are h.pdf
a. In puple bacteria P870 is present. External electron donors are h.pdf
 
A)ANS)In this TCPIP - System Access Layer, you will find out ab.pdf
A)ANS)In this TCPIP - System Access Layer, you will find out ab.pdfA)ANS)In this TCPIP - System Access Layer, you will find out ab.pdf
A)ANS)In this TCPIP - System Access Layer, you will find out ab.pdf
 
The ice allows the refluxed materials to crystall.pdf
                     The ice allows the refluxed materials to crystall.pdf                     The ice allows the refluxed materials to crystall.pdf
The ice allows the refluxed materials to crystall.pdf
 
#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
 
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf
1.1Yearcash flowpresent value of cash inflow = cash flow(1+r).pdf
 
HNO2 is a much weaker acid then HNO3 (strong acid.pdf
                     HNO2 is a much weaker acid then HNO3 (strong acid.pdf                     HNO2 is a much weaker acid then HNO3 (strong acid.pdf
HNO2 is a much weaker acid then HNO3 (strong acid.pdf
 
1. Identify five differences between DNA replication and gene transc.pdf
1. Identify five differences between DNA replication and gene transc.pdf1. Identify five differences between DNA replication and gene transc.pdf
1. Identify five differences between DNA replication and gene transc.pdf
 
1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdf
1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdf1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdf
1. b2. c (bed bug)3. d (caterpillars)4. c (lyme disease)5. e.pdf
 
A. the chemical formula for the base calcium (II) hydroxide is Ca(OH.pdf
A. the chemical formula for the base calcium (II) hydroxide is Ca(OH.pdfA. the chemical formula for the base calcium (II) hydroxide is Ca(OH.pdf
A. the chemical formula for the base calcium (II) hydroxide is Ca(OH.pdf
 
1) MODERN TIMES is a very well prepared movie that describes the mis.pdf
1) MODERN TIMES is a very well prepared movie that describes the mis.pdf1) MODERN TIMES is a very well prepared movie that describes the mis.pdf
1) MODERN TIMES is a very well prepared movie that describes the mis.pdf
 
An ion is an atom that has a charge. These charges can be postive or.pdf
An ion is an atom that has a charge. These charges can be postive or.pdfAn ion is an atom that has a charge. These charges can be postive or.pdf
An ion is an atom that has a charge. These charges can be postive or.pdf
 
Acids are substances that form hydrogen ions, H+(.pdf
                     Acids are substances that form hydrogen ions, H+(.pdf                     Acids are substances that form hydrogen ions, H+(.pdf
Acids are substances that form hydrogen ions, H+(.pdf
 
ANSWER1.Apocalypse Now film overall formIt is the height o.pdf
ANSWER1.Apocalypse Now film overall formIt is the height o.pdfANSWER1.Apocalypse Now film overall formIt is the height o.pdf
ANSWER1.Apocalypse Now film overall formIt is the height o.pdf
 
15.0mL = 0.015L phenylacetic acid 0.50molL 0.01.pdf
                     15.0mL = 0.015L phenylacetic acid 0.50molL 0.01.pdf                     15.0mL = 0.015L phenylacetic acid 0.50molL 0.01.pdf
15.0mL = 0.015L phenylacetic acid 0.50molL 0.01.pdf
 
4 water molecules are like a magnet because beca.pdf
                     4 water molecules are like a magnet because beca.pdf                     4 water molecules are like a magnet because beca.pdf
4 water molecules are like a magnet because beca.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 

this is my code to count the frequency of words in a text file#.pdf

  • 1. this is my code to count the frequency of words in a text file #define _GNU_SOURCE #include #include #include #include /****************************** here is the Structure for storing words received from each thread ******************************/ struct return_vall{ char wordlist[100][100]; int count[100]; } *arr; pthread_t *threads; int lines; /***************************** here are Aggregates Frequency Sum of different threads *****************************/ int aggregate(char *a) { int sum = 0; int i=0; while(1) { if(i==lines) break; int count = 0; while(1) { if(arr[i].count[count] == -1) { break; }
  • 2. if(strcmp(a,arr[i].wordlist[count]) == 0) { sum += arr[i].count[count]; arr[i].count[count] = 0; } count++; } i++; } return sum; } /************************** Function thta are executed by each thread on seperate line. **************************/ void *word_count(void* num) { int *ln = num; unsigned int line_number = *ln; line_number++; //printf("Thread %d ",line_number); char cmd_p1[9] = "sed -n '0"; char cmd_p2[2]; sprintf(cmd_p2,"%d",line_number); char cmd_p3[14] = "p' 'file.txt'0"; char command[100];
  • 3. command[0] = '0'; strcat(command,cmd_p1); strcat(command,cmd_p2); strcat(command,cmd_p3); //usleep(line_number); char cmd[100] = " | tr [:space:] ' ' | grep -v '^s*$' | sort | uniq -c | sort0"; strcat(command,cmd); //printf("%s ",command); //fflush(stdout); FILE *in; in= popen(command, "r"); rewind(in); char buff[50]; int counter = 0; while(fgets(buff,sizeof(buff),in)) { char c=' '; int i = 0; int cnt = atoi(buff); arr[line_number-1].count[counter] = cnt; while(c!='0') { c=buff[i]; buff[i]=buff[i+6]; i++; } int cnnt = 0; while(c!=' ') {
  • 4. c = buff[cnnt]; cnnt++; } i=0; while(c!='0') { c=buff[i]; buff[i]=buff[i+cnnt]; i++; } sprintf(arr[line_number-1].wordlist[counter],"%s",buff); //printf("%d %s",arr[line_number-1].count[counter],arr[line_number- 1].wordlist[counter]); counter++; } arr[line_number-1].count[counter] = -1; fclose(in); return NULL; } int main(void) { FILE *fp; fp = fopen("file.txt","r"); char a[200]; lines = 0; void *status = NULL; while(fgets(a,sizeof(a),fp) !=NULL) {
  • 5. lines++; } fclose(fp); arr = (struct return_vall*) calloc(lines, sizeof(struct return_vall) * lines); threads = malloc(sizeof(pthread_t)*lines); int i; for(i=0;i= 0) { if(arr[i].count[ctr] > 0) { strcpy(final[final_ctr],arr[i].wordlist[ctr]); final_cnt[final_ctr] = arr[i].count[ctr]; arr[i].count[ctr] = 0; int sum = aggregate(arr[i].wordlist[ctr]); final_cnt[final_ctr] += sum; printf("%d %s",final_cnt[final_ctr],final[final_ctr]); } ctr++; final_ctr++; } i++; if(i==lines) { break; } } free(arr); return 0;
  • 6. } Solution this is my code to count the frequency of words in a text file #define _GNU_SOURCE #include #include #include #include /****************************** here is the Structure for storing words received from each thread ******************************/ struct return_vall{ char wordlist[100][100]; int count[100]; } *arr; pthread_t *threads; int lines; /***************************** here are Aggregates Frequency Sum of different threads *****************************/ int aggregate(char *a) { int sum = 0; int i=0; while(1) { if(i==lines) break; int count = 0; while(1) { if(arr[i].count[count] == -1)
  • 7. { break; } if(strcmp(a,arr[i].wordlist[count]) == 0) { sum += arr[i].count[count]; arr[i].count[count] = 0; } count++; } i++; } return sum; } /************************** Function thta are executed by each thread on seperate line. **************************/ void *word_count(void* num) { int *ln = num; unsigned int line_number = *ln; line_number++; //printf("Thread %d ",line_number); char cmd_p1[9] = "sed -n '0"; char cmd_p2[2];
  • 8. sprintf(cmd_p2,"%d",line_number); char cmd_p3[14] = "p' 'file.txt'0"; char command[100]; command[0] = '0'; strcat(command,cmd_p1); strcat(command,cmd_p2); strcat(command,cmd_p3); //usleep(line_number); char cmd[100] = " | tr [:space:] ' ' | grep -v '^s*$' | sort | uniq -c | sort0"; strcat(command,cmd); //printf("%s ",command); //fflush(stdout); FILE *in; in= popen(command, "r"); rewind(in); char buff[50]; int counter = 0; while(fgets(buff,sizeof(buff),in)) { char c=' '; int i = 0; int cnt = atoi(buff); arr[line_number-1].count[counter] = cnt; while(c!='0') { c=buff[i]; buff[i]=buff[i+6]; i++; }
  • 9. int cnnt = 0; while(c!=' ') { c = buff[cnnt]; cnnt++; } i=0; while(c!='0') { c=buff[i]; buff[i]=buff[i+cnnt]; i++; } sprintf(arr[line_number-1].wordlist[counter],"%s",buff); //printf("%d %s",arr[line_number-1].count[counter],arr[line_number- 1].wordlist[counter]); counter++; } arr[line_number-1].count[counter] = -1; fclose(in); return NULL; } int main(void) { FILE *fp; fp = fopen("file.txt","r"); char a[200]; lines = 0; void *status = NULL;
  • 10. while(fgets(a,sizeof(a),fp) !=NULL) { lines++; } fclose(fp); arr = (struct return_vall*) calloc(lines, sizeof(struct return_vall) * lines); threads = malloc(sizeof(pthread_t)*lines); int i; for(i=0;i= 0) { if(arr[i].count[ctr] > 0) { strcpy(final[final_ctr],arr[i].wordlist[ctr]); final_cnt[final_ctr] = arr[i].count[ctr]; arr[i].count[ctr] = 0; int sum = aggregate(arr[i].wordlist[ctr]); final_cnt[final_ctr] += sum; printf("%d %s",final_cnt[final_ctr],final[final_ctr]); } ctr++; final_ctr++; } i++; if(i==lines) { break; } }