Text Analyzer BIS1523 – Lecture 14.

Slides:



Advertisements
Similar presentations
LIS651 lecture 3 functions and arrays Thomas Krichel
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Designing Algorithms Csci 107 Lecture 4.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
CSC Intro. to Computing Lecture 13: PALGO. Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Manipulating Text In today’s lesson we will look at: why we might want to pick out parts of text strings some BASIC functions that can be used to chop.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
Microsoft Access. Microsoft access is a database programs that allows you to store retrieve, analyze and print information. Companies use databases for.
Overview Excel is a spreadsheet, a grid made from columns and rows. It is a software program that can make number manipulation easy and somewhat painless.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
B Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Working with PDF and eText Templates.
Form Data (part 2) MIS 3502, Fall 2015 Brad N Greenwood, PhD Department of MIS Fox School of Business Temple University 11/10/2015 Slide 1.
Strings in Python String Methods. String methods You do not have to include the string library to use these! Since strings are objects, you use the dot.
Nested Loops CS303E: Elements of Computers and Programming.
Arrays and Strings. Arrays in PHP Arrays are made up of multiple memory blocks, each with the same name and differentiated by an index number Each block.
JavaScript, Sixth Edition
Chapter 5 Validating Form Data with JavaScript
Form Data (part 2) MIS 3502, Fall 2015 Jeremy Shafer Department of MIS
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
REPETITION CONTROL STRUCTURE
Practical Office 2007 Chapter 10
Loops BIS1523 – Lecture 10.
Arrays: Checkboxes and Textareas
Database application MySQL Database and PhpMyAdmin
ECS10 10/10
Arrays and files BIS1523 – Lecture 15.
Intro to PHP & Variables
Engineering Innovation Center
Cookies BIS1523 – Lecture 23.
While Loops BIS1523 – Lecture 12.
HTML Forms and User Input
Example Programs.
More Selections BIS1523 – Lecture 9.
Arrays An Array is an ordered collection of variables
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
Python I/O.
Functions BIS1523 – Lecture 17.
One-Dimensional Array Introduction Lesson xx
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
In Class Program: Today in History
In-Class Program: Sales System
Building Web Applications
Computer Science II First With files.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Strings A collection of characters taken as a set:
int [] scores = new int [10];
In Class Programming BIS1523 – Lecture 11.
Form Data (part 2) MIS 3501 Jeremy Shafer Department of MIS
Encryption and Decryption
In Class Programming: Credit card payment
PHP.
ARRAYS 1 GCSE COMPUTER SCIENCE.
Video list editor BIS1523 – Lecture 24.
Introduction to TouchDevelop
Algorithm Discovery and Design
Embedded PHP in HTML 5 & Strings in PHP
Building Java Programs
CHAPTER 6: Control Flow Tools (for and while loops)
How to use hash tables to solve olympiad problems
Functions continued.
Introduction to Computer Science
CS 1111 Introduction to Programming Spring 2019
Computer Science II First With files.
Presentation transcript:

Text Analyzer BIS1523 – Lecture 14

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)

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)

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.

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:

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:

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.

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.

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

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:

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.

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