Presentation is loading. Please wait.

Presentation is loading. Please wait.

Text Analyzer BIS1523 – Lecture 14.

Similar presentations


Presentation on theme: "Text Analyzer BIS1523 – Lecture 14."— Presentation transcript:

1 Text Analyzer BIS1523 – Lecture 14

2 Program Overview Todays program takes a block of text entered by the user, and calculates: The total number of words in the text The average word length The total number of times a specific word occurs (optional) The total number of words larger than a limit (optional) A table listing of all of the ‘large’ words found. (optional)

3 Variables To store all of these results, we will use the following variables: Inputs: $wordlist: an array of all the words $search_term: a word to search for $search_length: the size of ‘long’ words to look for Outputs: $numwords: The total number of words in the text $total_length: The total length of all the words added together $term_count: Total times our search term appeared $long_list: An array containing all of the ‘long’ words $avg_length: Average length of all words ($total_length / $numwords)

4 Initialization We will start by reading in the inputs, and initializing our counters to 0. We use the ‘explode’ function to break the text up based on the space character. Once that is done, the $wordlist array will have one element for each word that was in the textarea.

5 Count function Counting the number of words, we need to count the number of elements in the $wordlist array. PHP has a built in function that returns the number of words in an array, called count To use it, we simply: We can then print out $numwords in our summary section:

6 Average Length In order to calculate an average, we have to total up the length of every word (then divide by # of words). To do this, we will use a foreach loop to go through the entire array. Then, to calculate and print the average:

7 Counting Other Words There are 2 other types of words we want to count, ones that match what the user entered, and ones that are longer than the number the user entered. The user could have, however, left those entries blank. So we need to check to see if they are empty before calculating. The strlen function returns the length of a string. To add an element to an array, we use the assignment command. Once this loop is done, we will have the array $long_list that contains every word that was longer than what the user entered.

8 Output Before we output the search_count and long_count, we need to check again to see if the user entered in something into those blanks We use the count function on the $long_list array to count it.

9 Outputting the Table First, we need to use an IF to make sure the user checked the “detailed output” checkbox. If they did, we output a table with a row for everything in $long_list

10 Str_replace If you examine our output in the detail table, you will notice that punctuation is being included in our words. In order to get a more accurate count, we want to remove punctuation from our input. To achieve this, we will use the str_replace function This function replaces every occurrence of $needle found in $haystack with $replacement. Example:

11 Str_replace The replacement string can be empty (“”), in which case every occurrence of $needle would just be removed. The $needle can also be an array, in which case you could replace several things with nothing (removing them) with one str_replace command. In our program we are going to do 2 things, first, add a space to the end of every line, and then remove every other punctuation character.

12 Further Exercise Add code to make sure the textarea wasn’t empty


Download ppt "Text Analyzer BIS1523 – Lecture 14."

Similar presentations


Ads by Google