The overall goal of this project was to investigate file I/O and functions, then use the two to make a Word Counter that counts the number of occurrences of each word in a text file.
Task 1: File I/O
The first task was to investigate File I/O. The fs module is needed to read and write to files and the readline module is needed to read standard in from the terminal. The fs module allows the programmer to open a wide variety: txt files (which are basic ASCII text files), csv files (which are also ASCII text files), and pdfs (which are binary files), although pdfs when printed do not look like their original form because the binary version is printed:
The user can input to the file directly using the readline module by creating an interface and setting the input as the stdin and the output as the stdout (which both interact with the terminal). It is interesting because although I do the stdin interaction at the end of the program, it prints the question at the beginning of the file, and then waits for user input at the end.
The main difference between files and strings is how they are generated. Files need the module to read the file, but then the contents of the file are copied into a local variable in the program that can print it very similar to a string. Thus, once a file has been opened, its copied contents are similar to a string.
Here is the output of this file:
and an output file is created that contains the text:
This function does not return anything:
This function can return a string or an integer:
The args list can be iterated through to access all of the arguments given.
This allows a function to be overwritten when it is called so the second time it is called it uses the new function declaration.
The second option is:
which allows a new function declaration to be given in the body of this function.
While functions can be overwritten, all function calls use the most updated version of the function. For example:
Both of these hello() calls print hello, the more updated version of the function.
Functions within the same file can be called within each other with no constraints (for example the function does not need to be declared before it is called). To call a function in a different file than it is defined, the function need to be added to modules.export using:
module.exports.add = add
module.exports.hello = hello
Then in the file where it is to be called, the original file must be required as a module and set to a variable. Then it can be called by required_file.func(). For example:
Note that anything that is called in task2a is also called and printed in task2b.
The output for task2a is:
And the output for task2b is:
Task 3: Word Counter
The third task for this project was to create a Word Counter that counts the occurrences of each word in a text file. I used an array implementation. I use the fs module to open the text file given on the command line, then I split the data by whitespace and filter out any empty strings. Then I eliminate punctuation by replacing it with the empty string. Finally, I make a final list of words by looping through the list of data (which is now individual words) and checking to see if it is already in the final list. If it is, I increment the number associated with it. If its not, I add it to the list with its number as 1. Then I use the qsort function I made in project 4 with a new compare function that compares the numbers associated with each word to sort the list from most common word to least common word. Then I print the first twenty items in the final list (or the whole list of words if there are fewer than twenty words).
This is the output from calling it on wctest.txt:
- The first extension I completed was to have the word counter throw errors if a file is not given on the command line:
or if there is an error in opening the file:
This ensures that the program will not try to read the data from the file and run into a problem. If the function is called without a text file or with an invalid text file,
- The second extension I completed was to figure out how to get data from a text file over the internet. This requires the module http and the data is gathered very similar to any other text file. It can be outputted to the terminal just like any other:
This will print to the terminal:
This is the full contenets of the wctest file.