Friday, February 13, 2015

Spellcheck Program in C

/*So obviously the FILEPATH macro here would need to be changed for implementation, but here is a program I wrote this week that works with a dictionary to test if a user's input is in the given dictionary or not
*/

//Help Users Spell Correctly

//Hailey Hanson
//February 9th, 2015
//Systems Programming CS 360

#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
#include <unistd.h>
#define RECORD_SIZE 16
#define FILEPATH "/home/hailey/Documents/Code/WSU-V/SystemsProg/SystemsProg/lib/webster"


char*word;
int fd;
char*buffer;

char* plainWord(char*original);
int ok(int fd, char*word);

int main(int argc, char**argv){

    if(argc!=2){
        fprintf(stderr, "single word argument required\n");
        exit(1);
        }

    word = argv[1];

    fd = open(FILEPATH, O_RDONLY, 0);
    if (fd<0){
        fprintf(stderr, "couldn't open file %s : %s\n", FILEPATH, strerror(errno));
        exit(2);
    }
  
    if (ok(fd, word)) printf("yes, %s was found\n",word );
    else printf("no, %s was not found\n",word);

    close(fd);


char* plainWord(char*original){

    int i=0;
    char*dest;
    dest = calloc(strlen(original), sizeof(char));

    while(i<RECORD_SIZE){

        if(isalpha(original[i])){
            dest[i] = original[i];
        }
        i++;
    }
    dest[RECORD_SIZE] = '\0'; //this has to do with the specific dictionary I was using

    return strdup(dest);//must compile with gnu99 for strdup to work correctly
}


int ok (int fd, char*word){

    char* want = word;//from user
    char*have; //from webster

    int bot = 0;
    int fileSize = lseek(fd, 0, SEEK_END);
    int top = fileSize/RECORD_SIZE;
    buffer = calloc(RECORD_SIZE, sizeof(char));
  
    while(bot<top){
        int mid = (bot+top)/2;
        lseek(fd, mid*RECORD_SIZE, SEEK_SET);//moves me to middle of file
        read(fd, buffer, RECORD_SIZE);//fills buffer with one line

        want = plainWord(word);
        have = plainWord(buffer);
        int compare = strcmp(want, have);

        if(compare == 0){
            return 1;
        }
        else if (compare<0){
            top = mid;
        }
        else if (compare>0){
            bot = mid+1;
        }
    }
    return 0;
}   

Saturday, January 17, 2015

Internet of Things Blog Post

I wrote this blog post for a client who asked for 250+ words on the Internet of Things. He never told me where he posted it... which is annoying since it left me without a cool portfolio piece. Writing is fun for the sake of writing, though, right? 
Anyway, here it is:


While the Internet of Things (IoT) may sound like slang or an informal description of cyber-connections, the IoT is actually a formal, complex network of embedded computing devices. These devices cohabit the existing Internet infrastructure and are each identifiable under a unique IP address. The quality that makes the IoT different from the Internet as we know it is that systems within the IoT often don't require much, if any, human interaction after their initial implementation.

What can computers do without the help of humans, though? In fact, while computers are often thought to have a keyboard and a mouse, the majority of computers in the world are built into hardware to serve a purpose in their own environment. These built-in computers, known as embedded systems, include devices such as heart-rate monitors, anti-lock braking systems and thermostats. When these systems are wirelessly linked to other machines, they then become the “things” in the Internet of Things.

The unique machine-to-machine (M2M) quality of the IoT is ironically its biggest downfall though. Since there is less human interaction with M2M systems, there is more work to be done by machines; Storing massive quantities of data and effectively managing requests in real-time as opposed to a normal computer's clock time. Since the IoT is expanding so rapidly, the main objective of businesses supplying the technology is to create enough cloud storage space for all of the information to be processed and stored quickly.

Many businesses are compensating for the massive overload of data by implementing smaller data centers for initial processing so that the consumer will not have to wait for any larger, remote processes. Oftentimes, data can be stored within a device for instantaneous processes, too. It is absolutely crucial that businesses find a way to compensate for huge amounts of data before overloading consumers with faulty embedded systems.

While many believe that the IT systems of the past twenty years will not be able to handle the emergence of the IoT, it is up to the IT industry to come up with lucrative ways to increase cloud computing, storage and big-data analysis without losing automation or efficiency.

Sunday, November 16, 2014

Finite Automata Regular Expression Generator in Java

 Hailey Hanson
    HaileyLHanson@gmail.com
  
Here is a regular expression engine written in Java. The program reads in a text file as regular expressions separated by new lines.
The regular expressions are taken in in postfixed notation and then created into Nondeterministic Finite Automata (NFA).

To build the software, the text file titled test.txt will need to be present. The file can then be run within a Java compiler(I used Eclipse Kepler).

The files included in this archive are:

1) Main.java  
    My main program that calls upon my other processes
  
2) Machine.java
    Where the bulk of my program sits and constructs NFAs from reading Regular Expressions in through a text file
  
3) FinAut.java
    Where my Finite Automata object is stored
  
4) Transition
    This is where my regular expression transitions are stored as linked lists


******************************************************


package home;

//import Machine;

public class Main {

    public static void main(String[] args) {
        new Machine();

    }

}


*****************************************************
 
package home;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

public class Machine {

    private BufferedReader br;
    private String line;
    int i = 0;

    private LinkedList FAmachine;    //linkedlist of machines that will be stuck together
    private int currentState = 0;
    private FinAut FA1; // FinAut
    private FinAut FA2;

    public Machine()
    {
        FAmachine = new LinkedList();

        try
        {
            br = new BufferedReader(new FileReader("test.txt"));
        } catch (FileNotFoundException e1)
        {
            e1.printStackTrace();
        }// read in a line from RE
   
        int i = 0;
        char c;

            try
            {
                while ((line = br.readLine()) != null)
                {
                    for (i = 0; i < (line.length()); i++)
                    {
                        c = line.charAt(i);
                        System.out.println(c);

                        if (c == '&')
                        {
                            FA2=FAmachine.pop();
                            FA1=FAmachine.pop();
                            FA1.listOfTransitions.add(new transition(FA1.endState,FA2.startState,'E')); //add epsilon transition onto end of FA1
                            while (!FA2.listOfTransitions.isEmpty()){//while not at end of list
                                FA1.listOfTransitions.add(FA2.listOfTransitions.remove());//take end of FA2 and put at end of FA1
                            }
                            FA1.endState = FA2.endState; //make FA1 into the new single FA
                            FAmachine.push(FA1);// push(NFA that accepts the concatenation of
           
                            for(i=0;i                                System.out.println(FA1.listOfTransitions.get(i).state1 +" "+ FA1.listOfTransitions.get(i).state2 + " " + FA1.listOfTransitions.get(i).symbol);
                            }//end of for loop
                        }
                               
                        else if (c == '+')// push(NFA that accepts L(nFA1) + L(nFA2));
                        {
                            FA2 =FAmachine.pop();
                            FA1 = FAmachine.pop();
                            FA1.listOfTransitions.add(new transition(currentState++, FA1.startState, 'E'));//epsilon from new start to FA1 old start
                            FA1.listOfTransitions.add(new transition(currentState-1, FA2.startState, 'E'));//epsilon from new start to FA2 old start
                            FA1.listOfTransitions.add(new transition(FA1.endState, currentState++, 'E'));//epsilon from FA1 end to new end
                            FA1.listOfTransitions.add(new transition(FA2.endState, currentState-1, 'E'));//epsilon from FA2 end to new end
                           
                            FA1.startState = currentState-1;//update FA1 start to new start
                            FA1.endState = currentState; //update FA1 end to new end
                           
                            //take transitions from errthang and put into FA1
                            while (!FA2.listOfTransitions.isEmpty()){//while not at end of list
                                FA1.listOfTransitions.add(FA2.listOfTransitions.remove());//take end of FA2 and put at end of FA1
                            }
                            for(i=0;i                                System.out.println(FA1.listOfTransitions.get(i).state1 +" "+ FA1.listOfTransitions.get(i).state2 + " " + FA1.listOfTransitions.get(i).symbol);
                        //    System.out.println("+");
                           
                        } else if (c == '*')
                        {
                            FA2 =FAmachine.pop();
                            FA1 = FAmachine.pop();
                            FA1.listOfTransitions.add(new transition(currentState++, FA1.startState, 'E'));//epsilon from new start to FA1 old start
                            FA1.listOfTransitions.add(new transition(currentState-1, FA2.startState, 'E'));//epsilon from new start to FA2 old start
                            FA1.listOfTransitions.add(new transition(FA1.endState, currentState-1, 'E'));//epsilon from FA1 end to new start
                            FA1.listOfTransitions.add(new transition(FA2.endState, currentState-1, 'E'));//epsilon from FA2 end to new start
                            FA1.listOfTransitions.add(new transition(currentState++, FA1.startState, 'E'));//make all new start state with epsilon transition
                           
                            FA1.startState = currentState-1;//update FA1 start to new start
                           
                            //take transitions from errthang and put into FA1
                            while (!FA2.listOfTransitions.isEmpty()){//while not at end of list
                                FA1.listOfTransitions.add(FA2.listOfTransitions.remove());//take end of FA2 and put at end of FA1
                            }
                            for(i=0;i                                System.out.println(FA1.listOfTransitions.get(i).state1 +" "+ FA1.listOfTransitions.get(i).state2 + " " + FA1.listOfTransitions.get(i).symbol);
                        } else
                        {
                            FAmachine.push(new FinAut(currentState++, currentState++, c));
                            //System.out.println("c = " + c);
                        }

                        // System.out.println(line.next());
                        //System.out.println("line # ");

                    }
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }// end of while

        finally {
            if (br != null)
            {
            //    br.close();
            }
   
        }
    }
}// end of class Machine

******************************************************


package home;

import java.util.LinkedList;

public class FinAut {

    public int startState;
    public int endState;
    public LinkedList listOfTransitions;
   

    public FinAut(int start, int end, char c) {

        this.startState = start;
        this.endState = end;
        listOfTransitions = new LinkedList();
        listOfTransitions.add(new transition(startState, endState, c));

    }// end of FinAut method
}// end of FinAut class
 

************************************************

package home;

public class transition {
    int state1;
    int state2;
    char symbol;
   
    public transition(int startState, int endState, char c){
        this.state1=startState;
        this.state2=endState;
        this.symbol=c;
       
    }//end of transition method

}//end of class transition

Sunday, November 9, 2014

*ATTENTION*

I will be moving to a self-hosted blog in the (very) near future.

My new blog will be more up-to-date, so swing back by for the commencement of a new URL!

Wednesday, May 21, 2014

Linked List of Lists: Listception

My second assignment was pretty cool since I started separating things out into several classes (separated here by comment lines). The program description is below.

package hanson_h_2;

//hailey hanson
//Programming Assignment 2
//April 30th, 2014
//
//This program will read in from a text file, separate the words and get rid of punctuation and lowercase letters (making them all caps)
//then it will sort the words into alphabetical order and print them in a list that includes the numbered position
//of the word so that instead of listing a word twice, it will be listed as word: #occurance, #occurance, etc.
//

public class hanson_h_2{

public static void main(String[] args) {

wordIndex wi = new wordIndex("C:\\Users\\vicki\\Desktop\\java_text_file.txt");
}
}
/********************************************************************/

package hanson_h_2;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class wordIndex {

boolean statusVar;
wList myList = new wList();

int counter=0; //will keep track of word numbers

public wordIndex(String filename) {

Scanner s = null;
String word = null;

try {
s = new Scanner(new File(filename));
while(s.hasNext()){
word = s.next();
word = word.toUpperCase();
word = word.replaceAll("[^a-zA-Z]",  "");
counter++;
myList.add(word, counter);
statusVar = true;
}
} catch (FileNotFoundException e) {
statusVar = false;
}
print();
}

boolean status(){
if (statusVar == true)
return true;
else
return false;
}

void print(){
// prints the sorted list of words - one per line - followed by a space-separated
// list of references (in increasing order). The reference is the word #,
// beginning with 1 for the first word, 2 for the second, etc.
if(status() == true)
myList.print();
else if (status() == false)
System.out.println("Sorry, couldn't open file");


}

}

/********************************************************************************/

package hanson_h_2;

public class wList {

wNode header; //this is the sentinel node

public wList() {//not sure about these parameters
header = new wNode(null, null);//empty list (header points to zero and is zero and stuff)
}//end of wList constructor

void add(String data, int index){ //will add new wNode onto wList in alphabetical order
wNode newNode = null;

if (header.next == null){ //if list is empty
newNode = new wNode(data, header.next);
header.next = newNode;
newNode.list = new rList();
newNode.list.add(index);
}//end of if statement

else{ //there is already at least one thing in the list
wNode traverseNode = header.next; //will step through list to check for comparisons between these 2 nodes

while (traverseNode != null){//look through wList
if (data.equals(traverseNode.data)){ //if two wNodes have the same data
traverseNode.list.add(index); //add the index of the word onto the rList
return; //get outta town... add method town
}//end of if statement

traverseNode =  traverseNode.next; //move to  next node in wList
}//end of while loop

traverseNode = header.next; //if the node is not already in wList, move back to beginning of wList
wNode previousNode = header;

while (traverseNode != null){
if (data.compareTo(traverseNode.data) < 0){
newNode = new wNode(data, traverseNode); //make new node and put it in before right node
previousNode.next = newNode; //change previous pointer
break;
}//end of if statement
if (traverseNode.next == null){ //if this is the last thing in the list
newNode = new wNode(data, traverseNode.next); //make your node with the data piece, put it at end
traverseNode.next = newNode; //switch previous node's pointer to point to new node
break;
}//end of if statement
previousNode = traverseNode;
traverseNode = traverseNode.next; //move along through wList
}//end of while loop

newNode.list = new rList();
newNode.list.add(index);//add index number to the rList

}//end of else
}//end of add method

public void print(){
wNode currentNode = header.next; //start at beginning on wList
while (currentNode != null){
System.out.print(currentNode.data);//print the word
currentNode.list.print(); //print its position
System.out.println("\n"); //print a newline
currentNode = currentNode.next; //go to next node
}//end of while
}//end of print()

public class wNode
{

String data; //the data will be a word from my index
wNode next; //pointer to next node
rList list; //header node to rList. Is number associated with word

public wNode(String data, wNode next){
this.data = data;
this.next = next;
}//end of wNode constructor
}//end of wNode class
}//end of wList class

/********************************************************************************/

package hanson_h_2;

public class rList {

rNode header;

public rList() {
header = new rNode(0, null);
}

void add(int ref){  //adds word to list and ref to the end of the list
rNode currentNode = header.nextNode;

if (header.nextNode == null){
rNode newNode = new rNode(ref, header.nextNode);
header.nextNode = newNode;
}
else {
while(currentNode.nextNode != null) //while i am not at the end of the list
currentNode = currentNode.nextNode; //step forward one node
rNode newNode = new rNode(ref, currentNode.nextNode);
currentNode.nextNode = newNode;
}
}

public void print(){
rNode currentNode = header.nextNode;
while (currentNode != null){
System.out.print(" " + currentNode.index);
currentNode = currentNode.nextNode;
}
}

private class rNode {

int index; //data piece of node to tell where word is located in file
rNode nextNode; //pointer to next node
//should maybe set nextNode equal to null...?

public rNode(int ref, rNode next) {
index = ref;
nextNode = next;
}

}

}

The Beginnings of Java: Rational Numbers Program

So here I am just under a month away from my summer vacation. I met with an adviser this morning at WSU-Vancouver and decided that I won't be taking a class this summer since it won't take any time off of my last two years of school. So this means that my near future will consist of making coffee, enjoying the sun and hopefully getting a clue on GitHub. My adviser advised me (ha) to think about taking on an English minor since I told her I really like writing. I'm kind of torn, though, since I can't see giving more time and money to school while I'm already struggling to pay my tuition for the next two years.

The struggle is real.

I started working at a coffee shop a few weeks ago, on top of my Physics TA job, so I've been extremely busy trying to balance those, a Tech Writing class and also learning Java!
I meant to post my Java Hello World program forever and a day ago, but now it seems insignificant since I'm doing big scary programs like this. This was my first assignment in my Advanced Data Structures class and, as I've mentioned, I love clean, organized writing so I like this code :)

//Hailey Hanson
// April 18, 2014
//Programming Assignment 1-Rational Numbers
//
//This program will take in rational numbers and add, subtract, multiple, divide, reduce and convert them to doubles (if prompted)
//

public class Rat {

private int num; //numerator of fraction
private int denom; //denominator of fraction

Rat (int num, int denom)//will be referenced when there are two inputs
{
this.num = num;
this.denom = denom;
}

Rat (int num)//will be referenced with there is one input
{
this.num = num;
this.denom = 1;
}

public int num() //returns numerator of x (a rational number)
{
return num; //return x's numerator piece
}

public int denom()
{
return denom; //returns x's denominator piece
}

public Rat add(Rat n) //adds "this" Rat to n Rat
{
int newnum = (this.num()*n.denom())+(this.denom()*n.num()); //makes new numerator
int newdenom = (this.denom()*n.denom()); //makes new denominator
Rat newRat = new Rat(newnum,newdenom);//makes new Rat using new num and denom
newRat.reduce();
return newRat;
}

public Rat sub(Rat n) //subtracts "this" Rat and n Rat
{
int newnum = (this.num()*n.denom())-(this.denom()*n.num());//makes new numerator
int newdenom = (this.denom()*n.denom());//makes new denominator
Rat newRat = new Rat(newnum,newdenom);//makes new Rat
newRat.reduce();
return newRat;
}

public Rat times(Rat n) //multiplies "this" Rat and n Rat
{
Rat newRat = new Rat(n.num()*this.num(), n.denom()*this.denom());//makes new Rat
newRat.reduce();
return newRat;
}

public Rat div(Rat n)//divides "this" Rat and n Rat
{
Rat newRat = new Rat(this.num()*n.denom(), n.num()*this.denom());//makes new Rat
newRat.reduce();
return newRat;
}

public double toDouble()//converts Rat to a double
{
if (this.denom == 0)//if undefined
{
if (this.num()>0)//undefined and positive = infinity
{
return(Double.POSITIVE_INFINITY);
}
else if(this.num() == 0)//undefined and 0 = NaN
{
return(Double.NaN);
}
else if (this.num() <0 and="" negative="-infinity</p" undefined=""> {
return(Double.NEGATIVE_INFINITY);
}
}
return ((double) this.num()/this.denom());
}

private int GCF(int a,int b)//find greatest common factor to use in reduce method
{
if (a%b ==0) return b;
else while(a%b != 0)
{
int x = a%b;
a=b;
b=x;
}
return b;
}

public void reduce()//would normally be private, but will be public for testing purposes
{
if (denom !=0)
{
if(num == 0)
{
denom = 1;//special request for assignment
}
else
{
int gcf = GCF(num, denom);
num=num/gcf; ///reduce numerator
denom=denom/gcf;//reduce denominator
}
}
}

public void print() //print method
{
this.reduce(); //reduce Rat  before printing

if(denom == 1)
{
System.out.println(num);//reduce fractions with denominator of 1
}
else if (denom < 0 )
{
System.out.println(this.num*(-1) + "/"+ (-1)*this.denom()); //fix negative fractions
}
else
{
System.out.println(this.num() + "/" + this.denom()); //print normal rational fraction
}
}
}

Saturday, April 12, 2014

New Quarter - New Opportunities

Hello Blog-Readers!

          I finished out last quarter with my best GPA so far in college and although it wasn't quite as high as I was hoping, I'm still really happy with how it turned out.
         This was nice going into spring break because I was feeling good by the time I took a cross-country (vertically) roadtrip to Arizona to see my brother and meet my nephew :)
          I also stopped in some very cool places along the way including the Hoover Dam, the Grand Canyon and Zion National Park (which is totally my new second favorite place after Arches National Park)

 Just look how cool it is!!!(Yes, that's me up there)

          But anyway, I started Spring quarter last Monday and I'm really excited about my programming class. I'm taking advanced data structures and this will be my first class taught in Java. I'm really glad to be starting something more high-level and visual because C was feeling pretty dry after a year and a half on the command line.
          I also went to the North West regional Women in Computer (NWrWIC) conference today in Portland. This was definitely my favorite conference yet. I'm starting to feel more comfortable and less like I'm the biggest amateur in the room (which, yeah, I know, I've never really been), but I'm just feeling a lot more comfortable. I also went into this conference assuming that all Summer 2014 internships were already filled, so I didn't put pressure on myself to network much and it turned out to be my best networking yet! I'm up tonight applying for internships at Puppet Labs in Portland and Intel in Hillsboro. I'd be completely psyched to get either of these, so we'll see!