Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209271
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: Java

2011-07-07 15:38:32

Class 1: Prefix.java
  1. import java.util.Vector;

  2. public class Prefix {
  3.     static final int MULTIPLIER = 31; // for hash code
  4.     public Vector pref; //NPREF adjacent words from input
  5.     
  6.     public Prefix(int n, String str) {
  7.         pref = new Vector();
  8.         for (int i = 0; i < n ; i++)
  9.             pref.addElement(str);
  10.         
  11.     }
  12.     public Prefix (Prefix p) {
  13.         pref = (Vector)p.pref.clone();
  14.     }
  15.     
  16.     public int hashCode() {
  17.         int h = 0;
  18.         for(int i = 0; i < pref.size(); i++)
  19.             h = MULTIPLIER * h + pref.elementAt(i).hashCode();
  20.         return h;
  21.     }
  22.     
  23.     public boolean equals(Object o) {
  24.         Prefix p = (Prefix) o;
  25.         for (int i = 0; i < pref.size(); i++)
  26.             if(!pref.elementAt(i).equals(p.pref.elementAt(i)))
  27.                 return false;
  28.             return true;
  29.     }
  30. }

Class 2: Chain.java
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.StreamTokenizer;
  5. import java.util.Hashtable;
  6. import java.util.Iterator;
  7. import java.util.Random;
  8. import java.util.Vector;
  9. public class Chain {
  10.     static final int NPREF = 2; //size of prefix
  11.     static final String NONWORD = "#"; //"word" that can't appear
  12.     Hashtable states = new Hashtable(); //key = Prefix, value = suffix Vector
  13.     
  14.     Prefix prefix = new Prefix(NPREF, NONWORD); //initial prefix @1.1 : [#,#]
  15.     Random rand = new Random();
  16.     
  17.     public void build(FileReader file) throws IOException {//@2
  18.         StreamTokenizer st = new StreamTokenizer(new BufferedReader(file));
  19.         st.resetSyntax();
  20.         st.wordChars(0, Character.MAX_VALUE);
  21.         st.whitespaceChars(0, ' ');
  22.         try{
  23.             while (st.nextToken() != st.TT_EOF)
  24.                 add(st.sval);//@2.1:
  25.             add(NONWORD);//@2.2    
  26.         } catch (IOException e){
  27.             System.err.println("st.nextToken() unsucessful");
  28.             throw e;            
  29.         }            
  30.     }
  31.     /* Chain add: add word to suffix list, update prefix */
  32.      void add(String word)    { //@2.1
  33.         Vector suffix = (Vector) states.get(prefix);
  34.         if (suffix == null) {
  35.             suffix = new Vector();
  36.             states.put(new Prefix(prefix), suffix);//first time: [#,#]
  37.         }
  38.         suffix.addElement(word);
  39.         prefix.pref.removeElementAt(0);
  40.         prefix.pref.addElement(word);
  41.     }

  42.     
  43. // Chain generate: generate output words
  44.     void generate(int nwords){//@3
  45.         prefix = new Prefix(NPREF, NONWORD);//initial prfix: [#,#]
  46.         for (int i = 0; i < nwords; i++) {
  47.             Vector suffix = (Vector) states.get(prefix);
  48.             if (suffix == null) {
  49.                 System.err.println("Markov: internal error: no state");
  50.                 System.exit(1);
  51.             }
  52.             int r = Math.abs(rand.nextInt()) % suffix.size();
  53.             String str = (String) suffix.elementAt(r);
  54.             if (str.equals(NONWORD))
  55.                 break;
  56.             System.out.println("The prefix "+prefix.pref.toString() + "'s suffix is <" + str+">");
  57.             prefix.pref.removeElementAt(0);
  58.             prefix.pref.addElement(str);
  59.             }
  60.             Iterator it = states.keySet().iterator();
  61.             while(it.hasNext()) {
  62.                 Prefix key = (Prefix)it.next();
  63.                 System.out.println("The sates are ["+key.pref.toString() +
  64.                                    "]"+states.get(key).toString() );
  65.                      
  66.         }
  67.     }

  68. }
Class 3:Markov.java
  1. import java.io.FileReader;
  2. import java.io.IOException;

  3. import java.util.Random;

  4. public class Markov {
  5.     static final int MAXGEN = 10000; //maxium words generated
  6.     private FileReader file;
  7.         
  8.     public static void main(String[] args) throws IOException{
  9.         Chain chain = new Chain();//@1
  10.         int nwords = MAXGEN;
  11.         //Markov markov = new Markov(args[0]);
  12.         FileReader file = new FileReader(args[0]);
  13.         chain.build(file);//@2
  14.         chain.generate(nwords);//@3
  15.     }
  16.   }
Input file: input.txt
  1. Here we are here we go
output:
  1. The prefix [#, #]'s suffix is
  2. The prefix [#, here]'s suffix is <we>
  3. The prefix [here, we]'s suffix is
  4. The prefix [we, are]'s suffix is <here>
  5. The prefix [are, here]'s suffix is
  6. The prefix [here, we]'s suffix is <go>
  7. The sates are [[#, #]][here]
  8. The sates are [[we, are]][here]
  9. The sates are [[here, we]][are, go]
  10. The sates are [[we, go]][#]
  11. The sates are [[#, here]][we]
  12. The sates are [[are, here]][we]

阅读(605) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~