Skip to content

Commit

Permalink
Getting the GUI working. Storing the passed string in the WordMap.
Browse files Browse the repository at this point in the history
  • Loading branch information
Havvy committed Jun 6, 2012
1 parent 95ad219 commit e200e87
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 119 deletions.
70 changes: 0 additions & 70 deletions Frame.java

This file was deleted.

103 changes: 103 additions & 0 deletions Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package assignment8;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main
{
static class Position implements Comparable<Position>
{
final String source;
final int line;
final int word;

public Position(String source, int line, int word)
{
this.source = source;
this.line = line;
this.word = word;
}

@Override
public int compareTo(Position o)
{
// return source.compareTo(o.source) ?: line - o.line ?: column - o.column;
int rv;
rv = source.compareTo(o.source);
if (rv != 0)
return rv;
rv = line - o.line;
if (rv != 0)
return rv;
return word - o.word;
}

@Override
public String toString()
{
return String.format("file %s, line %d, word %d", source, line, word);
}
}


private final MultiMap<String, Position> impl = new MultiMap<String, Position>();

/**
* @param args array containing: filename
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
if (args.length == 0)
{
System.err.println("Usage: assignment8.Main files-to-search");
System.exit(1);
}
Main main = new Main();
for (String filename : args)
main.feed(filename, new BufferedReader(new FileReader(filename)));

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while (prompt() && (line = in.readLine()) != null)
{
main.search(line);
}
}

private void search(String line)
{
System.out.format("\"%s\" occurs at positions:", line);
System.out.println();
for (Position i : impl.get(line.toLowerCase()))
{
System.out.format(" %s", i);
System.out.println();
}
System.out.println();
}

private static boolean prompt()
{
System.out.print("Enter a word to search for: ");
return !System.out.checkError();
}

private void feed(String name, BufferedReader in) throws IOException
{
String line;
int l = 1;
while ((line = in.readLine()) != null)
{
int w = 1;
for (String word : line.split("\\s"))
{
impl.add(word.toLowerCase(), new Position(name, l, w));
w++;
}
l++;
}
}
}
23 changes: 12 additions & 11 deletions MainCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@

public class MainCLI {

static public WordHash wordHash;
static public WordMap wordHash;
public static String inputString;
public static String[] words;

public static void main(String[] args) throws IOException {
wordHash = new WordHash(readstr());
inputString = readstr();
wordHash = new WordMap(inputString);

boolean mainrepeat = true;
while (mainrepeat) {
System.out.println("*************************************************************************");
System.out.println("Hello! Please choose an option (represented by the corresponding number):");
System.out.println("(1) Print out the string.");
System.out.println("(2) Search for a word in the string");
System.out.println("(0) exit the program.");
mainrepeat = usrIn();
}
boolean mainrepeat = true;
while (mainrepeat) {
System.out.println("*************************************************************************");
System.out.println("Hello! Please choose an option (represented by the corresponding number):");
System.out.println("(1) Print out the string.");
System.out.println("(2) Search for a word in the string");
System.out.println("(0) exit the program.");
mainrepeat = usrIn();
}
}

static public boolean usrIn() throws IOException{
Expand Down
97 changes: 89 additions & 8 deletions MainGUI.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,97 @@
/**
* @author Havvy, Joesph Obanion
* @email ryan.havvy@gmail.com, moltow@gmail.com
*/

package assignment8;

import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.*;
import java.util.Arrays;
import java.util.Set;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class MainGUI {
@SuppressWarnings("serial")
public class MainGUI extends JFrame {
private final JTextArea input = new JTextArea();
private final JLabel labFindWord = new JLabel("Find Word: ");
private final JTextField findWord = new JTextField();
private final JTextArea output = new JTextArea();
private final Listener keyListener = new Listener();

private String priorInput = "";
private WordMap words;

public MainGUI () {
super("Word Finder");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);

Container window = getContentPane();
window.setMinimumSize(new Dimension(600, 1200));

input.setPreferredSize(new Dimension(600, 300));
input.addKeyListener(keyListener);
window.add(input, BorderLayout.NORTH);

window.add(labFindWord, BorderLayout.WEST);

findWord.setPreferredSize(new Dimension(590 - labFindWord.getWidth(), 12));
findWord.addKeyListener(keyListener);
window.add(findWord, BorderLayout.EAST);

output.setPreferredSize(new Dimension(600, 300));
output.setEnabled(false);
window.add(output, BorderLayout.SOUTH);

pack();
setVisible(true);
}

public static void main (String[] argv) {
new Frame();
new MainGUI();
}

class Listener implements KeyListener {
@Override
public void keyPressed(KeyEvent e) {}

@Override
public void keyReleased(KeyEvent evt) {
if (!(input.getText().equals(priorInput))) {
priorInput = input.getText();
words = new WordMap(priorInput);
}

output.setText("");

String searchTerm = findWord.getText();

for (int ix : words.get(searchTerm)) {
output.append(wordsNear(words.getWords(), ix));
}

}

private String wordsNear (String[] words, int ix) {
String[] nearby = null;
if (ix > 2 && ix < words.length - 3) {
nearby = Arrays.copyOfRange(words, ix - 2, ix + 2);
} else if (ix < 2) {
nearby = Arrays.copyOfRange(words, 0, 5);
} else {
nearby = Arrays.copyOfRange(words, words.length - 6, words.length - 1);
}

StringBuffer sb = new StringBuffer();
for (String s : nearby) {
sb.append(s + " ");
}
return sb.toString();
}

@Override
public void keyTyped(KeyEvent e) {}

}
}
50 changes: 50 additions & 0 deletions MultiMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package assignment8;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class MultiMap<K, V extends Comparable<V>>
{
private final Map<K, Set<V>> impl = new HashMap<K, Set<V>>();

public boolean add(K key, V value)
{
Set<V> vs = impl.get(key);
if (vs == null)
{
vs = new TreeSet<V>();
vs.add(value);
impl.put(key, vs);
return true;
}
return vs.add(value);
}

public boolean remove(K key, V value)
{
Set<V> vs = impl.get(key);
if (vs == null)
return false;
boolean rv = vs.remove(value);
if (rv && vs.isEmpty())
impl.remove(key);
return rv;
}

public boolean remove_all(K key)
{
return impl.remove(key) != null;
}

public Iterable<V> get(K key)
{
Set<V> vs = impl.get(key);
if (vs != null)
return Collections.unmodifiableSet(vs);
else
return Collections.emptySet();
}
}
30 changes: 0 additions & 30 deletions WordHash.java

This file was deleted.

Loading

0 comments on commit e200e87

Please sign in to comment.