/* * Copyright 2010 by Patrick Juola * All rights reserved. Unauthorized redistribution or modification is * prohibited. For development licence contact juola@mathcs.duq.edu */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; //import statements //Check if window closes automatically. Otherwise add suitable code public class Monkey extends JFrame { static JFrame f; static JLabel player_label; static JTextArea story; static JTextArea newstory; static JTextArea words; static Vector keywords = new Vector(); static Vector the_story = new Vector(); static HashSet pairs; static int player_num=0; public static void main(String args[]) { f = new JFrame("Once Upon a Time/Monkeying Around with "+args[0]); f.setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel mainPanel = new JPanel(new BorderLayout()); JPanel topPanel = new JPanel(new BorderLayout()); JPanel textPanel = new JPanel(new BorderLayout()); JPanel wordPanel = new JPanel(new FlowLayout()); JPanel buttonPanel = new JPanel(new FlowLayout()); topPanel.add(wordPanel,BorderLayout.EAST); topPanel.add(textPanel,BorderLayout.WEST); mainPanel.add(topPanel,BorderLayout.NORTH); mainPanel.add(buttonPanel,BorderLayout.SOUTH); //f.setSize(800, 800); Container content = f.getContentPane(); content.setBackground(Color.white); content.setLayout( new FlowLayout() ); //content.setSize(600, 800); JScrollPane pane = new JScrollPane(); story = new JTextArea(); story.setLineWrap(true); story.setWrapStyleWord(true); story.setEditable(false); story.setPreferredSize(new Dimension( 400,300) ); /* story.setText("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."); */ story.setText(extractStartString(args[0])); pane.getViewport().add(story); textPanel.add(pane,BorderLayout.NORTH); newstory = new JTextArea(); newstory.setPreferredSize(new Dimension( 400,100) ); newstory.setEditable(true); textPanel.add(newstory,BorderLayout.SOUTH); words = new JTextArea(); words.setEditable(false); story.setPreferredSize(new Dimension( 200,400) ); keywords = extractKeywords(args[0]); words.setText(makeWordList(keywords)); wordPanel.add(words); player_label = new JLabel("Player 0"); incrementPlayerLabel(); buttonPanel.add(player_label); JButton bS = new JButton("Submit"); bS.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { //f.setTitle("submitted text"); boolean t = testStory(story.getText(),newstory.getText()); if (t) { updateStory(newstory.getText()); strikeKeywords(newstory.getText().trim().split("\\s+"), keywords); newstory.setText(""); words.setText(makeWordList(keywords)); incrementPlayerLabel(); } } }); buttonPanel.add(bS); JButton bQ = new JButton("Quit"); bQ.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); buttonPanel.add(bQ); f.add(mainPanel); f.pack(); f.setVisible(true); pairs = extractBigrams(args[0]); } static void incrementPlayerLabel() { player_num++; if (player_num > 2) // MAX_PLAYERS player_num = 1; player_label.setText("Player " + player_num); } static void updateStory(String s) { story.setText(story.getText()+"\n"+s); } static boolean testStory(String prev, String newstuff) { String s1[] = prev.trim().split("\\s+"); String s2[] = newstuff.trim().split("\\s+"); boolean test = testBigram(s1[s1.length-1],s2[0],pairs); if (!test) { bitch("bigram " + s1[s1.length-1] + " and " + s2[0] + " failed!"); return test; } for (int i = 0;i bigrams) { String s = one + ":" + two; return bigrams.contains(s); } static void bitch(String s) { JOptionPane.showMessageDialog(null, s); } static String extractStartString(String base) { String str = null; try { BufferedReader in = new BufferedReader(new FileReader(base+".start")); str = in.readLine(); in.close(); } catch (IOException e) { System.out.println("Error opening start!"); } return str; } static HashSet extractBigrams(String base) { HashSet lexicon = new HashSet(); try { BufferedReader in = new BufferedReader(new FileReader(base+".2g")); String str; while ((str = in.readLine()) != null) { lexicon.add(str); } in.close(); } catch (IOException e) { System.out.println("Error opening 2grams!"); } return lexicon; } static Vector extractKeywords(String base) { Vector lexicon = new Vector(); Vector keywords = new Vector(); try { BufferedReader in = new BufferedReader(new FileReader(base+".1g")); String str; while ((str = in.readLine()) != null) { lexicon.add(str); } in.close(); } catch (IOException e) { System.out.println("Error opening 1grams!"); } //Random r = new Random(2001); Random r = new Random(); // Generates random starting words for (int i=0;i<5;i++) { keywords.add(lexicon.elementAt(r.nextInt(lexicon.size()))); } return keywords; } static void strikeKeywords(String[] newtext, Vector keywords) { for (String s: newtext) { int index = keywords.indexOf(s); if (index>=0) keywords.remove(s); } } static String makeWordList( Vector keywords) { String txt=""; for (int i =0;i