Text Processing with Haskell and Python

In this article i will be calculating some basic things with Python and Haskell. The text file is from here.

Requirements:

1)Number of words

2)Number of sentences

3)Number of question marks

4)Number of unique words

5)Number of paragraphs

6)First paragraph of every section

7)Last paragraph of every section

Part1:Haskell

Haskell is a functional language and its logic is almost the base of all other languages in terms of treating everything as a function and evaluating functions.

It is extremely shortcut to find the requirements with haskell because they are straightforward and implemented inside the haskell. My code is like following;

Screenshot from 2015-12-27 10:46:45

Basically, main role is “filter” method. It filters the strings or strings inside the files and output whatever is desired.

Import clause is to import the library Data.List to indicate that we will be working with lists(to calculate paragraphs, words,etc) and this will happen by putting them into an array of strings.

putStr is to print a text into command line.

length is to calculate the size of the arrays.

I used Hugs to compile my code. Hugs is an editor to run the haskell files and haskell codes. For example, if i want to load a file into hugs and run the haskell code inside of it it will be enough to type :l <file-directory> to run the code. In my case, i used to run /tmp/count.hs with runhugs command. It is something similar to the following;(number of unique words are being calculated in the picture)

Screenshot from 2015-12-27 11:13:16

For question mark, filter filters the text with question marks(?), for sentence it filters for the dots(.), for words it filters words clause, for paragraphs it filters null lines came after a text and for unique words it uses nub function to add everything to a set to deliminate the duplicates of every word in the text.

Part2: Python

Python is a very reliable, flexible and usable language among the others. It can compile so many things without having any trouble in any operating system.

I previously installed lamp server and that included Python so i didn’t make any additional installation to my computer. Typing python to command line will be enough to open the python editor. Running our Python code is like doing a similar thing to Haskell case. Writing python <file-directory> is enough to run the python code in the gedit file. The commented code is like following for the first 5 requirements. They are very simple in terms of writing a single for loop and incrementing the values in that for loop. Since loops are not allowed in Haskell, we didn’t do that.

 

Screenshot from 2015-12-27 11:20:15Screenshot from 2015-12-27 11:21:37

 

 

For the sixth and seventh requirement, i saved the indexes of every subtitle for every chapter of the book and divided the book into subranges of string arrays. Getting every array’s first and last element is enough to do the requirement. And every paragraph is recorded into another file to see them more clearly since command line is not enough to see all of them in a row. That file is like following;

Screenshot from 2015-12-27 11:23:40

Since there are lots of unique words(around 100k for that book), calculation for both of them is taking so long in both Haskell and Python. Especially in Haskell it is so much slower.

Unique words file is also like following. It can be tested whether it has a duplicate in that array by searching a specific element in the file. I did that and didn’t find any duplicates so the code is working!

Screenshot from 2015-12-27 11:25:55
Any comments of suggestions would be very appreciated. See you soon!

A Library System with PHP

Hello again. In this article i will explain how to write a web interface with PHP, JavaScript, MySQL, CSS and HTML.

This project is developed by using WAMP server. It is a very useful tool that combines Apache, MySQL and PHP servers in which everybody can install easily and do some useful work.

First of all, we need to start all the services after installing WAMP server. After that it will be placed like following;Screenshot (2)

localhost bar will take you directly for php projects. phpmyadmin will be for phpmyadmin to control the database and the flow of data. www directory will be the place that php project files will be held in. Press start all services button.

For PHP, an editor is needed. For that reason, i used CodeLobster software to compile by code but Notepad++ or other compilers will also work. It will be enough just to place that file in the www directory after all the coding is done.

The user guide about the website can be found in here. Besides that i will explain in detail how i writed the code.

First of all, we need to connect MySQL and PHP. That is quite easy, we just have to enter some parameters about our connection information(username and password for MySQL) and the name of our database which includes our tables like following;

 

Screenshot (3)

data.php is to be used whenever a query is needed to be executed for inserting, deleting or updating the informations about the student and books.

Next, we need to design our main page. In the main page, all the students are listed and all the operations can be done by clicking the buttons. The interface is like following;

Screenshot (4)

 

All the operations listed are possible with the library system. Every single operation is done by queries and their execution with some PHP and JavaScript codes. I have 3 tables. One is to hold students’ attributes and the other one is to hold books’ values, last one is to keep every students’ book name with student’s id.

For adding a book button,it adds into the book table but if we want to add a book to a student, we have to insert the book into the database first. A student can pick any book from the listed books. If a student is going to get a book, return a book or is to be printed all the books, edit must be clicked to get to the operations part. It is like following;

Screenshot (5)

The code of that is like following;

Screenshot (6)

This is the code for choosing the operation. Link is to get the themes for the interface(green and blue colors, changed size of text boxes,etc.). At form, we implicitly indicate that we will be using a variable in the code in “adding_book.php” page and we will do that by using “$_GET[‘<variable_name>’]” declaration. $student_id is received with that way from the edit.php page and that value returns the student that was chosen from the list at the main page. We will be sending this value to adding_book.php page to use to add into the database with the name of the book. That method is used through every php page since variables are important and they are needed for making the operations.

If we click on “ADD BOOK”, we will be facing with a screen that shows the book names that are available for the student. By choosing that, student will be added with its id and with that book name into the studentbook table and when necessary, we will get the books of that specific student.

We can also delete a book from the student with the same method, the only difference will be in the query. Instead of using insert, we will be using delete.

Screenshot (7)

As can be seen in here, both the variables are reached by get method and they will be inserted into student like following;

Screenshot (8)

The logic is all the same for all every other procedures. It is all about writing simple queries, passing them as parameters into PHP functions and making them work in an efficient way.

That will be all for this project. The other party of the code can be reached here.I would be very glad with every questions or corrections. Happy codings!