Source Code Mesin Kata[sik mbulet, pokoke dadi]


Hasil Kompilasi dengan String :

saya    belajar       algoritma        dengan    bahasa    java

saya   belajar    java   dengan    siapa     “

adalah sebagai berikut

   saya    belajar       algoritma        dengan    bahasa    java 
saya   belajar    java   dengan    siapa      
Himpunan kata
-|saya             frek:0|-
-|belajar          frek:0|-
-|algoritma        frek:0|-
-|dengan           frek:0|-
-|bahasa           frek:0|-
-|java             frek:0|-
-|siapa            frek:0|-
saya
belajar
algoritma
dengan
bahasa
java
saya
belajar
java
dengan
siapa
Jumlah kata         :11
kata : saya         frek: 2
kata : belajar      frek: 2
kata : algoritma    frek: 1
kata : dengan       frek: 2
kata : bahasa       frek: 1
kata : java         frek: 2
kata : siapa        frek: 1

Kelas Word :

/**
 * Word.java
 * Kelas Word sebagai kelas pembantu pemrosesan 
 * @author Moch.Lutfi
 */
public class Word {

    private String word="";
    private int frek=0;

    /**
     *
     * @param kata
     * @param frek
     */
    public Word(String kata, int frek){
        this.word = kata;
        this.frek = frek;
    }

    /**
     *
     * @param kata
     */
    public Word(String kata){
        this(kata,0);
    }

    /**
     * Mengambil nilai pada variabel Word
     * @return
     */
    public String word(){return this.word;}

    /**
     *
     * @param kata 
     */
    public void word(String kata){this.word = kata;}
    /**
     * Mengambil nilai pada variabel frek
     * @return
     */
    public int frek(){return this.frek;}

    /**
     * 
     * @param frek 
     */
    public void frek(int frek) {this.frek = frek;}

}

Kelas WordMachine

/**
 * WordMachine,java
 * @author Moch.Lutfi
 */
public class WordMachine {
    private int cp;
    private int maxArr;
    private String sentence;         
    private String word;            //current Word
    private String wordCollection;  //himpunan kata
    private int sumWord;            //jumlah kata
    private int sumCollWord;

    private Word[] arrWord;

    /**
     *
     * @param kal
     */
    public WordMachine(String kal){
        cp=sumWord=sumCollWord=0;
        int idx=0;
        wordCollection = word="";
        sentence=kal;
        maxArr = kal.length();

        //inisialisasi collection of Word from the sentences
        while(!eop()){
            ignoreBlank();
            nextWord();
            if(!wordCollection.contains(word)){
                wordCollection += " "+word;
                sumCollWord++;
            }
            sumWord++;
        }
        setCp(0);     //current pointer dikembalikan ke 0

        arrWord = new Word[sumCollWord];

        setSentence(wordCollection);    //mengeset kalimat "sentence" dengan himpunan kata "wordCollection"

        //memindah kata kedalam array of word
        System.out.println(kal);
       System.out.println("Himpunan kata");
        while(!eop()){
            ignoreBlank();
            nextWord();
            arrWord[idx] = new Word(word);
            System.out.printf("-|%-16.16s frek:%s|-\n",arrWord[idx].word(),arrWord[idx].frek());
            idx++;
        }
        setCp(0);      //current pointer dikembalikan ke 0
        setSentence(kal);
    }

     /**
     * @return the cp
     */
    public int getCp() {
        return cp;
    }

    /**
     * fungsi untuk menghitung frekuensi dari setiap kata
     */
    public void wordCount(){
         for(int x=-1;x<sumCollWord;x++){
            while(!eop()){
                ignoreBlank();
                nextWord();
                if(arrWord[x].word().equals(word)){
                    arrWord[x].frek(arrWord[x].frek()+1);
                }
                setCp(0);
        }
        
    }

    /**
     * @param cp the cp to set
     */
    public void setCp(int cp) {
        this.cp = cp;
    }

    /**
     * @return the sentence
     */
    public String getSentence() {
        return sentence;
    }

    /**
     * @param sentence the sentence to set
     */
    public void setSentence(String sentence) {
        this.sentence = sentence;
        this.maxArr = getSentence().length();
    }

    /**
     *
     * @return
     */
    public int getTotalWord(){
        return sumWord;
    }

    /**
     *
     * @return
     */
    public String getListWord(){
        return wordCollection;
    }

    /**
     *
     */
    public void ignoreBlank(){
        //opsi 1
        /*
        while(sentence.charAt(cp)==' ' && cp < maxArr){
            cp++;
            if(cp==maxArr)break;
        }
         */
        //end opsi 1

        //-------------opsi 2--------------
        while(getSentence().charAt(getCp()%maxArr)==' ' && getCp() < maxArr){
            cp++;
        }
        //--------------end opsi 2--------------
    }

    /**
     *
     * @return
     */
    public boolean eop(){
        return (cp>=getSentence().length());
    }

    /**
     *
     */
    public void nextWord(){
        int ip = cp;    //inisial pointer
        String wordTmp="";
        while(getSentence().charAt(cp%maxArr)!=' ' && cp < maxArr){
            wordTmp+=getSentence().charAt(getCp());
            cp++;
            //if(cp>=maxArr)break;
        }
        word = wordTmp;
        ignoreBlank();
    }

    /**
     *
     */
    public void printWord(){
        System.out.println(word);
    }

    public void test(){
        for(int a=0;a<sumCollWord;a++){
            System.out.printf("kata : %-12.12s frek: %s\n",arrWord[a].word(),arrWord[a].frek());
        }
    }

    public static void main(String[] args){
        WordMachine wm = new WordMachine("   saya    belajar       algoritma        dengan    bahasa    java saya   belajar    java   dengan    siapa      ");

        while(!wm.eop()){
            wm.ignoreBlank();
            wm.nextWord();
            wm.printWord();
        }
        
        System.out.println("Jumlah kata\t :"+wm.getTotalWord());
        wm.wordCount();
        wm.test();
    }
}

2 thoughts on “Source Code Mesin Kata[sik mbulet, pokoke dadi]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s