-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Getting the GUI working. Storing the passed string in the WordMap.
- Loading branch information
Showing
7 changed files
with
302 additions
and
119 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.