Markov Chain Text Gen

alice-in-wonderland[1].jpg

Markov chains are a list ngrams (grouped portions of text) from a work of text with probabilities of what will come next grouped to each ngram. You can then use this to generate new text based on those probabilities.

Here’s some made with Alice in Wonderland:

cupboards as long ago anything; she had began dream. There isn't,' said to Alice laughed so far

'Ugh!' said the puppy began, in a low, trembling voice. 'I've offended tone, and raised him declare it's

ven't think I can't help that,' said the Hatter. He had caught there ought Alice thought she went on,


HashMap> createTextMarkov(String txt, int order) {
    HashMap> ngrams = new HashMap>();
    for (int i = 0; i < txt.length() - order; i++) {
      String gram = txt.substring(i, i + order);

      if (!ngrams.containsKey(gram)) {
        ngrams.put(gram, new ArrayList());
      }
      ngrams.get(gram).add(txt.charAt(i + order));
    }
    return ngrams;
  }
  
  
  String genText(HashMap> markovMap) {
    Object[] keyArray = markovMap.keySet().toArray();
    String starting = (String) keyArray[(int) random(0, keyArray.length)];
    int order = starting.length();
    String result = starting;
    String currentGram = starting;

    for (int i = 0; i < 100; i++) {
      if (!markovMap.containsKey(currentGram)) {
        break;
      }
      ArrayList possibilities = markovMap.get(currentGram);

      String next = "" + possibilities.get((int) random(0, possibilities.size()));
      result += next;
      currentGram = result.substring(result.length() - order, result.length());
    }
    return result;
  }


A Markov chain can also be made with words instead of ngrams.

dogs The Pool of uglifying it flashed across her draw the baby at last concert on in a crimson velvet cushion resting their slates when he went Alice its voice Fetch me for some executions I make it unfolded the Pigeon but it was too much of course the other the Mouse who will you want to the March Hare I didnt interrupted in Alice who had put his scaly friend replied counting off her hand round THAT direction waving its full of mine said to set the Duchess asked triumphantly pointing to your waist the slate with Seaography then another moment he knows it is wrong and marked poison or something better



HashMap> createTextMarkovByWord(String txt) { 
  HashMap> ngrams = new HashMap>();
  
  txt = txt.replace('\n',' ').replace("-", "").replace("\"", "").replace("'", "").replace(".", "").replace("?", "").replace("!", "").replace(",", "").replace("\t", "").replace("\r", "").replace(":", "").replace(";", "");
  
  String[] words = txt.split(" ");
  for (int i = 0; i < words.length - 1; i++) {
      String gram = words[i];

      if (!ngrams.containsKey(gram)) {
        ngrams.put(gram, new ArrayList());
      }
      ngrams.get(gram).add(words[i+1]);
    }
  
  
  return ngrams;
}

String genWordsText(HashMap> markovMap) {
    Object[] keyArray = markovMap.keySet().toArray();
    String starting = (String) keyArray[(int) random(0, keyArray.length)];
    String result = starting;
    String currentGram = starting;

    for (int i = 0; i < 1000; i++) {
      if (!markovMap.containsKey(currentGram)) {
        break;
      }
      ArrayList possibilities = markovMap.get(currentGram);

      String next = possibilities.get((int) random(0, possibilities.size()));
      result += " " + next;
      currentGram = next;
    }
    return result;
  }


Superformula

1.png
float n1 = 0;
float n2 = 0;
float n3 = 0;
float m = 0;

float rotY = 0;
float rotX = 0;

void setup() {
  size(1280, 720, P3D); 
  n1 = random(1,100);
  n2 = random(1,100);
  n3 = random(1,100);
  m = (int)random(2,10);
}

void draw() {
  background(255);
  translate(width/2, height/2);
  rotateY(rotY);
  rotateX(rotX);
  noStroke();
  lights();
  int total = 75;
  PVector[][] globe = new PVector[total+1][total+1];
  float r = 300;
  for (int i = 0; i < total+1; i++) {
    float lat = map(i, 0, total, -HALF_PI, HALF_PI);
    float r2 = superFormulaR(m, n1, n2, n3, lat);//superShape3D(lat, m, 0.2, 1.7, 1.7);
    //float r2 = supershape(lat, 2, 10, 10, 10);
    for (int j = 0; j < total+1; j++) {
      float lon = map(j, 0, total, -PI, PI);
      float r1 = superFormulaR(m, n1, n2, n3, lon);//supershape(lon, m, 0.2, 1.7, 1.7);
      //float r1 = supershape(lon, 8, 60, 100, 30);
      float x = r * r1 * cos(lon) * r2 * cos(lat);
      float y = r * r1 * sin(lon) * r2 * cos(lat);
      float z = r * r2 * sin(lat);
      globe[i][j] = new PVector(x, y, z);
    }
  }

  //stroke(255);
  //fill(255);
  //noFill();
  //offset += 5;
  pushMatrix();
  colorMode(HSB);
  for (int i = 0; i < total; i++) {
    float hu = map(i, 0, total, 0, 255*6);
    fill((hu + 0) % 255 , (hu *10) % 255, (hu /2) % 255);
    beginShape(TRIANGLE_STRIP);
    for (int j = 0; j < total+1; j++) {
      PVector v1 = globe[i][j];
      vertex(v1.x, v1.y, v1.z);
      PVector v2 = globe[i+1][j];
      vertex(v2.x, v2.y, v2.z);
    }
    endShape();
  }
  popMatrix();
    rotY += PConstants.TWO_PI / 1000;
    rotX += PConstants.TWO_PI / 1000;
    if (frameCount % 350 == 0) {
      n1 = random(1,100);
      n2 = random(1,100);
      n3 = random(1,100);
      m = (int)random(2,20);  
    }
}

public PVector superFormula3D(float m, float n1, float n2, float n3, float phi, float theta)
{
   float r1 = superFormulaR(m, n1, n2, n3, theta);
   float r2 = superFormulaR(m, n1, n2, n3, phi);
   return new PVector(r1 * cos(theta) * r2 * cos(phi), r1 * sin(theta) * r2 * cos(phi), r2 * sin(phi));
}

public float superFormulaR(float m, float n1, float n2, float n3, float phi)
{
   float t1,t2;
   float a=1,b=1;

   t1 = cos(m * phi / 4) / a;
   t1 = abs(t1);
   t1 = pow(t1,n2);

   t2 = sin(m * phi / 4) / b;
   t2 = abs(t2);
   t2 = pow(t2,n3);

   return 1/pow(t1+t2,-1/n1);
}



public PVector superFormula(float m, float n1, float n2, float n3, float phi)
{
   float r;
   float t1,t2;
   float a=1,b=1;

   t1 = cos(m * phi / 4) / a;
   t1 = abs(t1);
   t1 = pow(t1,n2);

   t2 = sin(m * phi / 4) / b;
   t2 = abs(t2);
   t2 = pow(t2,n3);

   r = pow(t1+t2,1/n1);
   if (abs(r) == 0) {
      return new PVector(0,0);
   } else {
      r = 1 / r;
      return new PVector(r * cos(phi),r * sin(phi));
   }
}

Wiki: here

Glitched Block Matching Algorithm

391zuf.gif


Glitched Block matching algorithm. Usually used in video compression

  PImage img1;
  PImage img2;
  ArrayList mv;
  
void setup(){
  size(1280,720);
  img1 = loadImage("video\\thumb0087.jpg");
  img2 = loadImage("video\\thumb0088.jpg");
  
  mv = blockMatch(img1,img2,4);
}


ArrayList blockMatch(PImage img1, PImage img2, int N) {
  ArrayList moveVectors3 = new ArrayList();
  for (int oi = 0; oi < 1280; oi +=N) {
    for (int oj = 0; oj < 720; oj +=N) {
        PImage searchImage = img1.get(oi - N, oj - N, N*3, N*3);
        PImage R = img2.get(oi, oj, N, N);
        float mad = MeanDifference(searchImage, R, N, N, N);
        PVector sp = new PVector(oi, oj);
        PVector ep = null;
        int step = N;
        while (step!=1) {
          for (int i = 0; i < N*3; i+=N) {
              for (int j = 0; j < N*3; j+=N) {
                  float nmad = MeanDifference(searchImage, R, N, i, j);
                  if (nmad < mad) {
                      mad = nmad;
                      ep = new PVector(i + oi- N, j + oj- N);
                  }
              }
          }
          step/=2;
        }
        if (ep != null){
          //moveVectors.add(img1.get((int)ep.x, (int)ep.y,N,N));
          //moveVectors2.add(sp);
          moveVectors3.add(new PVector[]{sp,ep});
          //println(oi + "  " + oj);
        }
    }
  }
  return moveVectors3;
}

void printFromVec(PImage img, PVector[] moveVectors, int N) {
    image(img.get((int)moveVectors[1].x, (int)moveVectors[1].y, N,N), (int)moveVectors[0].x, (int)moveVectors[0].y);
}

void printAllFVec(PImage img, ArrayList moveVectors, int N) {
  for (int i = 0; i <  moveVectors.size(); i++) {
    printFromVec(img, moveVectors.get(i), N);
  }
}

void drawToGrap(PImage img, PImage base, PGraphics grap, ArrayList moveVectors, int N) {
  grap.beginDraw();
  grap.background(0);
  grap.image(base,0,0);
  for (int i = 0; i <  moveVectors.size(); i++) {
    grap.image(img.get((int)moveVectors.get(i)[1].x, (int)moveVectors.get(i)[1].y, N,N), (int)moveVectors.get(i)[0].x, (int)moveVectors.get(i)[0].y);
  }
  grap.endDraw();
}
  

float MeanDifference(PImage C, PImage R, int N, int x, int y) {
  float MAD = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (j + y >= N*3 || i + x >= N*3) continue;
      MAD += Math.abs(hue(C.get(i + x, j + y)) - hue(R.get(i,j)));
    }
  }
  return MAD /= (N*N);
}



void draw() {
  image(img1,0,0);
    noFill();
    stroke(255);
  PGraphics g1 = createGraphics(width, height);
  drawToGrap(img2, img1, g1, mv, 4);
  image(g1,  0,0);
}

Wiki: here