Jackson Mac Low’s Diastic Algorithm

January 22nd, 2006

I have been trying to implement Jackson Mac Low’s diastic readings[link] in Java for an exercise this week. I managed to work the algorithm partly, I can choose a phrase before compile and spit out the words according to it that my program reads from an input file. Couple of things are missing though;

  • I am only deliminating the words through spaces, I am trying to get my hands dirty regex, but they are damn nonsense.
  • I need to work on errorHandling to make the program don’t go into loops. When searching for long phrases and cannot find it in the text.
  • Maybe a runtime-user input system.
  • Minor problems running the code that are stated below.
  • Texts to be read :
    Handling Errors UsingExceptions from Sun Docs
    Regular Expressions from Sun Docs
    Java Fundamental Class Reference I/O, Input Output Operations in Java
    Introduction to the java.util.regex Package – Beginning Regular Expressions by Andrew Watt (through NYU Library)

    // *************** DIASTIC TEXT ALGORITHM ****************
    // Bugs: There is this repetition of the i after
    // it finds the phrase character, could be a problem
    // if the phrase has double char like "book".
    // also i doesn't goes up to the last element.
    // *******************************************************
    // to do: use lines according to the marks.
    // make it look more like a poem.
    // *******************************************************

    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;

    public class Diasticc {
        public static void main (String[] args) throws IOException {
            FileInputStream fis = new FileInputStream(args[0]);
            FileChannel fc = fis.getChannel();
            ByteBuffer bb = ByteBuffer.allocate((int)fc.size());
            fc.read(bb);
            fc.close();
             
           
            String content = new String(bb.array());
            String phrase="poem";
            String[] word = content.split(" ");
            int i = 0;
            for (int k=0; k < phrase.length(); k++) {
                for ( i = i + 1; i<word.length; i++) {
                    if(i >= word.length-1) {
                        i = 0;
                    }
                    //System.out.println(i);
                    if(word[i].length() > k) {
                    if(word[i].charAt(k) == phrase.charAt(k)) {
                       // System.out.println(i + " " + word[i]);
                       // System.out.println(k + " " + phrase.charAt(k));
                       System.out.println(word[i]);
                        
                        
                        break;
                    }
                }
            }
            }
          }
    }


    Leave a Reply