adding cs240
This commit is contained in:
Vendored
BIN
Binary file not shown.
@@ -0,0 +1,28 @@
|
||||
package spell;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface SpellCorrector {
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static class NoSimilarWordFoundException extends Exception {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells this <code>SpellCorrector</code> to use the given file as its dictionary
|
||||
* for generating suggestions.
|
||||
* @param dictionaryFileName File containing the words to be used
|
||||
* @throws IOException If the file cannot be read
|
||||
*/
|
||||
public void useDictionary(String dictionaryFileName) throws IOException;
|
||||
|
||||
/**
|
||||
* Suggest a word from the dictionary that most closely matches
|
||||
* <code>inputWord</code>
|
||||
* @param inputWord
|
||||
* @return The suggestion
|
||||
* @throws NoSimilarWordFoundException If no similar word is in the dictionary
|
||||
*/
|
||||
public String suggestSimilarWord(String inputWord) throws NoSimilarWordFoundException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package spell;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import spell.SpellCorrector.NoSimilarWordFoundException;
|
||||
import spell.Trie.Node;
|
||||
|
||||
public class SpellCorrectorImpl implements SpellCorrector {
|
||||
|
||||
private Set<String> suggestion = new TreeSet<String>();
|
||||
private TrieImpl dictionary = new TrieImpl();
|
||||
public SpellCorrectorImpl(){
|
||||
}
|
||||
|
||||
public void useDictionary(String dictionaryFileName) throws IOException {
|
||||
File srcFile = new File(dictionaryFileName);
|
||||
Scanner scanner = new Scanner(srcFile);
|
||||
dictionary = new TrieImpl();
|
||||
|
||||
while(scanner.hasNext()){
|
||||
String word = scanner.next();
|
||||
dictionary.add(word);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String suggestSimilarWord(String inputWord) throws NoSimilarWordFoundException{
|
||||
String word = inputWord.toLowerCase();
|
||||
suggestion = new TreeSet<String>();
|
||||
if(dictionary.words.contains(word)){
|
||||
return word;
|
||||
}
|
||||
// edit distance 1
|
||||
deletion(word);
|
||||
transposition(word);
|
||||
alteration(word);
|
||||
insertion(word);
|
||||
int mostfrequent = 0;
|
||||
String finalword = "";
|
||||
for(String temp : suggestion){
|
||||
if(dictionary.words.contains(temp)){
|
||||
int frequency = dictionary.find(temp).getValue();
|
||||
if(mostfrequent < frequency){
|
||||
finalword = temp;
|
||||
mostfrequent = frequency;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(finalword != "")
|
||||
{
|
||||
return finalword;
|
||||
}
|
||||
Set<String> suggestion1 = new TreeSet<String>();
|
||||
suggestion1.addAll(suggestion);
|
||||
suggestion.clear();
|
||||
// edit distance 2
|
||||
int mostfrequent1 = 0;
|
||||
String finalword1 = "";
|
||||
for(String temp : suggestion1){
|
||||
deletion(temp);
|
||||
transposition(temp);
|
||||
alteration(temp);
|
||||
insertion(temp);
|
||||
for(String temp1 : suggestion){
|
||||
if(dictionary.words.contains(temp1)){
|
||||
int frequency = dictionary.find(temp1).getValue();
|
||||
if(mostfrequent1 < frequency){
|
||||
finalword1 = temp1;
|
||||
mostfrequent1 = frequency;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(finalword1 != ""){
|
||||
return finalword1;
|
||||
}
|
||||
throw new NoSimilarWordFoundException();
|
||||
}
|
||||
|
||||
public void deletion(String word){
|
||||
for(int i = 0; i < word.length(); i++){
|
||||
suggestion.add(word.substring(0, i) + word.substring(i+1, word.length()));
|
||||
}
|
||||
}
|
||||
public void transposition(String word){
|
||||
for(int i = 0; i < word.length()-1; i++){
|
||||
StringBuilder sb = new StringBuilder(word);
|
||||
char letter = sb.charAt(i);
|
||||
sb.deleteCharAt(i);
|
||||
sb.insert(i+1, letter);
|
||||
suggestion.add(sb.toString());
|
||||
}
|
||||
}
|
||||
public void alteration(String word){
|
||||
for(int i = 0; i < word.length(); i++){
|
||||
for(char letter = 'a'; letter <= 'z'; letter++){
|
||||
StringBuilder sb = new StringBuilder(word);
|
||||
sb.setCharAt(i, letter);
|
||||
if(!sb.toString().equals(word)){
|
||||
suggestion.add(sb.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void insertion(String word){
|
||||
for(int i = 0; i < word.length()+1; i++){
|
||||
for(char letter = 'a'; letter <= 'z'; letter++){
|
||||
StringBuilder sb = new StringBuilder(word);
|
||||
sb.insert(i, letter);
|
||||
suggestion.add(sb.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package spell;
|
||||
|
||||
/**
|
||||
* Your trie class should implement the Trie interface
|
||||
*/
|
||||
public interface Trie {
|
||||
|
||||
/**
|
||||
* Adds the specified word to the trie (if necessary) and increments the word's frequency count
|
||||
*
|
||||
* @param word The word being added to the trie
|
||||
*/
|
||||
public void add(String word);
|
||||
|
||||
/**
|
||||
* Searches the trie for the specified word
|
||||
*
|
||||
* @param word The word being searched for
|
||||
*
|
||||
* @return A reference to the trie node that represents the word,
|
||||
* or null if the word is not in the trie
|
||||
*/
|
||||
public Node find(String word);
|
||||
|
||||
/**
|
||||
* Returns the number of unique words in the trie
|
||||
*
|
||||
* @return The number of unique words in the trie
|
||||
*/
|
||||
public int getWordCount();
|
||||
|
||||
/**
|
||||
* Returns the number of nodes in the trie
|
||||
*
|
||||
* @return The number of nodes in the trie
|
||||
*/
|
||||
public int getNodeCount();
|
||||
|
||||
/**
|
||||
* The toString specification is as follows:
|
||||
* For each word, in alphabetical order:
|
||||
* <word> <count>\n
|
||||
*/
|
||||
@Override
|
||||
public String toString();
|
||||
|
||||
@Override
|
||||
public int hashCode();
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o);
|
||||
|
||||
/**
|
||||
* Your trie node class should implement the Trie.Node interface
|
||||
*/
|
||||
public interface Node {
|
||||
|
||||
/**
|
||||
* Returns the frequency count for the word represented by the node
|
||||
*
|
||||
* @return The frequency count for the word represented by the node
|
||||
*/
|
||||
public int getValue();
|
||||
}
|
||||
|
||||
/*
|
||||
* EXAMPLE:
|
||||
*
|
||||
* public class Words implements Trie {
|
||||
*
|
||||
* public void add(String word) { ... }
|
||||
*
|
||||
* public Trie.Node find(String word) { ... }
|
||||
*
|
||||
* public int getWordCount() { ... }
|
||||
*
|
||||
* public int getNodeCount() { ... }
|
||||
*
|
||||
* @Override
|
||||
* public String toString() { ... }
|
||||
*
|
||||
* @Override
|
||||
* public int hashCode() { ... }
|
||||
*
|
||||
* @Override
|
||||
* public boolean equals(Object o) { ... }
|
||||
*
|
||||
* }
|
||||
*
|
||||
* public class WordNode implements Trie.Node {
|
||||
*
|
||||
* public int getValue() { ... }
|
||||
* }
|
||||
*
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package spell;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class TrieImpl implements Trie{
|
||||
|
||||
Node root = new Node();
|
||||
int wordcount = 0;
|
||||
int nodecount = 1;
|
||||
Set<String> words = new TreeSet<String>();
|
||||
|
||||
public TrieImpl(){
|
||||
|
||||
}
|
||||
|
||||
public void add(String word) {
|
||||
word = word.toLowerCase();
|
||||
Node tracker = root;
|
||||
|
||||
for(int i = 0; i < word.length(); i++){
|
||||
int position = word.charAt(i) - 'a';
|
||||
String temp = word.substring(0, i+1);
|
||||
|
||||
if(nodecount == 1){
|
||||
Node newnode = new Node();
|
||||
newnode.setName(temp);
|
||||
root.nodeArray[position] = newnode;
|
||||
tracker = newnode;
|
||||
nodecount++;
|
||||
}
|
||||
else{
|
||||
if(!temp.equals(word))
|
||||
{
|
||||
if(tracker.nodeArray[position] == null){
|
||||
Node newnode = new Node();
|
||||
newnode.setName(temp);
|
||||
tracker.nodeArray[position] = newnode;
|
||||
tracker = newnode;
|
||||
nodecount++;
|
||||
}
|
||||
else{
|
||||
tracker = tracker.nodeArray[position];
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(tracker.nodeArray[position] == null){
|
||||
Node newnode = new Node();
|
||||
newnode.setName(temp);
|
||||
tracker.nodeArray[position] = newnode;
|
||||
tracker = newnode;
|
||||
nodecount++;
|
||||
wordcount++;
|
||||
tracker.frequency++;
|
||||
words.add(temp);
|
||||
}
|
||||
else{
|
||||
tracker = tracker.nodeArray[position];
|
||||
tracker.frequency++;
|
||||
words.add(temp);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Trie.Node find(String word) {
|
||||
Node tracker = root;
|
||||
|
||||
for(int i = 0; i < word.length(); i++){
|
||||
int position = word.charAt(i) - 'a';
|
||||
String temp = word.substring(0, i+1);
|
||||
|
||||
if(!temp.equals(word)){
|
||||
if(tracker.nodeArray[position] == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else{
|
||||
tracker = tracker.nodeArray[position];
|
||||
}
|
||||
}
|
||||
else{
|
||||
tracker = tracker.nodeArray[position];
|
||||
if(tracker.getValue() == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return tracker;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getWordCount() {
|
||||
return wordcount;
|
||||
}
|
||||
|
||||
public int getNodeCount() {
|
||||
return nodecount;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
String output = "";
|
||||
Iterator<String> iterator = words.iterator();
|
||||
while(iterator.hasNext()) {
|
||||
String setElement = iterator.next();
|
||||
output = output + setElement + " " + Integer.toString(find(setElement).getValue()) + "\n";
|
||||
}
|
||||
return output;
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int hash = 1;
|
||||
hash = hash*17 + wordcount;
|
||||
hash = hash*31 + nodecount;
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if(o == null){
|
||||
return false;
|
||||
}
|
||||
if(this.toString().equals(o.toString())){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public class Node implements Trie.Node{
|
||||
int frequency = 0;
|
||||
String name = null;
|
||||
Node[] nodeArray = new Node[26];
|
||||
public Node(){
|
||||
}
|
||||
public void setName(String temp){
|
||||
this.name = temp;
|
||||
}
|
||||
public int getValue() {
|
||||
return frequency;
|
||||
}
|
||||
}
|
||||
}
|
||||
+127101
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,93 @@
|
||||
package hangman;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class EvilHangman implements EvilHangmanGame {
|
||||
Set<String> startingSet = new TreeSet<String>();
|
||||
Map<String, Set<String>> partition = new TreeMap<String, Set<String>>();
|
||||
int length;
|
||||
String key;
|
||||
public EvilHangman(){
|
||||
length = 0;
|
||||
key = "";
|
||||
}
|
||||
public int getstartingSetsize(){
|
||||
return startingSet.size();
|
||||
}
|
||||
public String getWord(){
|
||||
return startingSet.iterator().next();
|
||||
}
|
||||
public void setLength(int length){
|
||||
this.length = length;
|
||||
}
|
||||
public String getKey(){
|
||||
return key;
|
||||
}
|
||||
public void startGame(File dictionary, int wordLength){
|
||||
try {
|
||||
startingSet.clear();
|
||||
setLength(wordLength);
|
||||
for(int i = 0; i < length; i++){
|
||||
key = key + "-";
|
||||
}
|
||||
Scanner scanner = new Scanner(dictionary);
|
||||
while(scanner.hasNext()){
|
||||
String word = scanner.next();
|
||||
if(word.length() == wordLength){
|
||||
startingSet.add(word);
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public Set<String> makeGuess(char guess){
|
||||
partition.clear();
|
||||
for(String word: startingSet){
|
||||
String currentkey = keyGen(word,guess);
|
||||
if(partition.containsKey(currentkey)){
|
||||
partition.get(currentkey).add(word);
|
||||
}
|
||||
else{
|
||||
Set<String> newset = new TreeSet<String>();
|
||||
newset.add(word);
|
||||
partition.put(currentkey, newset);
|
||||
}
|
||||
}
|
||||
Set<String> curSet = new TreeSet<String>();
|
||||
curSet = getcurSet(guess);
|
||||
startingSet = curSet;
|
||||
return curSet;
|
||||
}
|
||||
public Set<String> getcurSet(char guess){
|
||||
String mostFrequentKey = getKey();
|
||||
Set<String> mostFrequentSet = new TreeSet<String>();
|
||||
for(Map.Entry<String, Set<String>> entry: partition.entrySet()){
|
||||
if(entry.getValue().size() > mostFrequentSet.size()){
|
||||
mostFrequentSet = entry.getValue();
|
||||
mostFrequentKey = entry.getKey();
|
||||
}
|
||||
}
|
||||
key = mostFrequentKey;
|
||||
return mostFrequentSet;
|
||||
}
|
||||
public String keyGen(String word, char guess){
|
||||
StringBuilder sb = new StringBuilder(length);
|
||||
for(int i = 0; i < length; i++){
|
||||
if(word.charAt(i) == guess){
|
||||
sb.append(guess);
|
||||
}
|
||||
else{
|
||||
sb.append(key.charAt(i));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package hangman;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Set;
|
||||
|
||||
public interface EvilHangmanGame {
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static class GuessAlreadyMadeException extends Exception {
|
||||
}
|
||||
/**
|
||||
* Starts a new game of evil hangman using words from <code>dictionary</code>
|
||||
* with length <code>wordLength</code>
|
||||
*
|
||||
* @param dictionary Dictionary of words to use for the game
|
||||
* @param wordLength Number of characters in the word to guess
|
||||
*/
|
||||
public void startGame(File dictionary, int wordLength);
|
||||
/**
|
||||
* Make a guess in the current game.
|
||||
*
|
||||
* @param guess The character being guessed
|
||||
* @return The set of strings that satisfy all the guesses made so far
|
||||
* in the game, including the guess made in this call. The game could claim
|
||||
* that any of these words had been the secret word for the whole game.
|
||||
*
|
||||
* @throws GuessAlreadyMadeException If the character <code>guess</code>
|
||||
* has already been guessed in this game.
|
||||
*/
|
||||
public Set<String> makeGuess(char guess) throws GuessAlreadyMadeException;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package hangman;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String fileName = args[0];
|
||||
int wordLength = Integer.parseInt(args[1]);
|
||||
int guesses = Integer.parseInt(args[2]);
|
||||
EvilHangman hangman = new EvilHangman();
|
||||
File dic = new File(fileName);
|
||||
hangman.startGame(dic, wordLength);
|
||||
String key = "";
|
||||
for(int i = 0; i < wordLength; i++){
|
||||
key = key+"-";
|
||||
}
|
||||
Set<String> alreadyProcess = new TreeSet<String>();
|
||||
while(guesses > 0){
|
||||
String guessedLetters = "";
|
||||
for(String letter: alreadyProcess){
|
||||
guessedLetters = guessedLetters + " " + letter;
|
||||
}
|
||||
System.out.println("You have " + guesses + " left");
|
||||
System.out.println("Used letters:" + guessedLetters);
|
||||
System.out.println("Word: " + key);
|
||||
System.out.print("Enter guess: ");
|
||||
Scanner scan = new Scanner(System.in);
|
||||
boolean keepGoing = true;
|
||||
char guess = 0;
|
||||
while(keepGoing){
|
||||
String input = scan.next().toLowerCase();
|
||||
if(!input.matches("[a-z]")){
|
||||
System.out.println("Invald input");
|
||||
}
|
||||
else if(alreadyProcess.contains(input)){
|
||||
System.out.println("You already used that letter");
|
||||
}
|
||||
else{
|
||||
keepGoing = false;
|
||||
alreadyProcess.add(input);
|
||||
guess = input.charAt(0);
|
||||
}
|
||||
}
|
||||
hangman.makeGuess(guess);
|
||||
key = hangman.getKey();
|
||||
int count = 0;
|
||||
for(int i = 0; i < key.length(); i++){
|
||||
if(key.charAt(i) == guess){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if(count == 0){
|
||||
guesses--;
|
||||
if(guesses == 0){
|
||||
System.out.println("You lose!");
|
||||
System.out.println("The word was: " + hangman.getWord());
|
||||
}
|
||||
else{
|
||||
System.out.println("Sorry, there is no " + guess + "\'s\n");
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(!key.contains("-")){
|
||||
System.out.println("You Win!");
|
||||
System.out.println("The word was: " + key);
|
||||
guesses = 0;
|
||||
}
|
||||
else{
|
||||
System.out.println("Yes, there is " + count + " " + guess + "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import java.io.IOException;
|
||||
|
||||
public class image {
|
||||
public static imagemap inverse(imagemap curPixMap) {
|
||||
imagemap pixelMap = curPixMap.copyAll();
|
||||
pixelMap.getEveryOne(new pkpix() {
|
||||
public void handle(imagemap pixelMap, pixel p, int r, int c) {
|
||||
pixelMap.set(r, c, p.invert());
|
||||
}
|
||||
});
|
||||
return pixelMap;
|
||||
}
|
||||
|
||||
public static imagemap blur(imagemap curPixMap, final int radius) {
|
||||
imagemap pixelMap = curPixMap.copyAll();
|
||||
pixelMap.getEveryOne(new pkpix() {
|
||||
public void handle(imagemap pixelMap, pixel p, int r, int c) {
|
||||
pixelMap.set(r, c, pixel.average(pixelMap.get(r, c, c + radius - 1)));
|
||||
}
|
||||
});
|
||||
return pixelMap;
|
||||
}
|
||||
|
||||
public static imagemap emboss(final imagemap curPixMap) {
|
||||
imagemap pixelMap = curPixMap.copyAll();
|
||||
pixelMap.getEveryOne(new pkpix() {
|
||||
public void handle(imagemap pixelMap, pixel p, int r, int c) {
|
||||
if (r == 0 || c == 0) {
|
||||
pixelMap.set(r, c, new pixel(128));
|
||||
return;
|
||||
}
|
||||
pixel embossed = p.emboss(curPixMap.get(r-1, c-1));
|
||||
pixelMap.set(r, c, embossed);
|
||||
}
|
||||
});
|
||||
return pixelMap;
|
||||
}
|
||||
|
||||
public static imagemap grayscale(imagemap curPixMap) {
|
||||
imagemap pixelMap = curPixMap.copyAll();
|
||||
pixelMap.getEveryOne(new pkpix() {
|
||||
public void handle(imagemap pixelMap, pixel p, int r, int c) {
|
||||
pixelMap.set(r, c, p.grayscale());
|
||||
}
|
||||
});
|
||||
return pixelMap;
|
||||
}
|
||||
|
||||
public enum Command {
|
||||
GRAYSCALE, INVERT, EMBOSS, MOTIONBLUR;
|
||||
}
|
||||
|
||||
public static void printUsage() {
|
||||
System.out.println("USAGE: java ImageEditor in-file out-file (grayscale|invert|emboss|motionblur motion-blur-length)");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String in, out;
|
||||
Command command;
|
||||
int blurRadius = 0;
|
||||
try {
|
||||
in = args[0];
|
||||
out = args[1];
|
||||
command = Command.valueOf(args[2].toUpperCase());
|
||||
if (command.equals(Command.MOTIONBLUR)) {
|
||||
blurRadius = Integer.parseInt(args[3]);
|
||||
if (blurRadius <= 0)
|
||||
printUsage();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
printUsage();
|
||||
return;
|
||||
}
|
||||
imagemap pixelMap;
|
||||
try {
|
||||
pixelMap = new imagemap(in);
|
||||
} catch (Exception e) {
|
||||
printUsage();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
switch (command) {
|
||||
case GRAYSCALE:
|
||||
pixelMap = grayscale(pixelMap);
|
||||
break;
|
||||
case INVERT:
|
||||
pixelMap = inverse(pixelMap);
|
||||
break;
|
||||
case EMBOSS:
|
||||
pixelMap = emboss(pixelMap);
|
||||
break;
|
||||
case MOTIONBLUR:
|
||||
pixelMap = blur(pixelMap, blurRadius);
|
||||
break;
|
||||
default:
|
||||
printUsage();
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
printError("Unexpected error: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
pixelMap.fileWritter(out);
|
||||
} catch (IOException e) {
|
||||
printError("Error writing to file '" + out + "'");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public static void printError(String err) {
|
||||
System.out.println(err);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PushbackReader;
|
||||
|
||||
public class imagemap {
|
||||
public int width;
|
||||
public int height;
|
||||
public int maxColorValue = 255;
|
||||
public pixel[][] pixels;
|
||||
public PushbackReader stream;
|
||||
|
||||
public imagemap(int width, int height, pixel[][] pixels) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.pixels = pixels;
|
||||
}
|
||||
|
||||
public imagemap(String path) throws Exception {
|
||||
try {
|
||||
stream = new PushbackReader(new FileReader(path));
|
||||
} catch (Exception e) {
|
||||
throw new Exception("File not found.");
|
||||
}
|
||||
try {
|
||||
readHeader();
|
||||
pixels = new pixel[height][width];
|
||||
readPixels();
|
||||
} catch (Exception e) {
|
||||
throw new Exception("File format not recognized.");
|
||||
}
|
||||
}
|
||||
|
||||
public imagemap set(int r, int c, pixel p) {
|
||||
pixels[r][c] = p;
|
||||
return this;
|
||||
}
|
||||
|
||||
public pixel get(int r, int c) {
|
||||
return pixels[r][c];
|
||||
}
|
||||
|
||||
|
||||
private imagemap readHeader() throws Exception {
|
||||
return
|
||||
uniqueNum()
|
||||
.getDims()
|
||||
.getMaxColor();
|
||||
}
|
||||
|
||||
public pixel[] get(int r, int c1, int c2) {
|
||||
if (c2 >= pixels[r].length) c2 = pixels[r].length - 1;
|
||||
pixel[] curPixels = new pixel[c2 - c1 + 1];
|
||||
for (int i = 0; c1 <= c2; i++, c1++) {
|
||||
curPixels[i] = get(r, c1);
|
||||
}
|
||||
return curPixels;
|
||||
}
|
||||
|
||||
private imagemap uniqueNum() throws Exception {
|
||||
invalChar();
|
||||
if (!readWord().equals("P3")) {
|
||||
throw new Exception("File format not recognized.");
|
||||
}
|
||||
invalChar();
|
||||
return this;
|
||||
}
|
||||
|
||||
private imagemap readPixels() throws NumberFormatException, IOException {
|
||||
for (int r = 0; r < height; r++) {
|
||||
for (int c = 0; c < width; c++) {
|
||||
int red = Integer.parseInt(readWord());
|
||||
invalChar();
|
||||
int green = Integer.parseInt(readWord());
|
||||
invalChar();
|
||||
int blue = Integer.parseInt(readWord());
|
||||
invalChar();
|
||||
pixels[r][c] = new pixel(red, green, blue);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private imagemap getDims() throws NumberFormatException, IOException {
|
||||
width = Integer.parseInt(readWord());
|
||||
invalChar();
|
||||
height = Integer.parseInt(readWord());
|
||||
invalChar();
|
||||
return this;
|
||||
}
|
||||
|
||||
private imagemap getMaxColor() throws NumberFormatException, IOException {
|
||||
maxColorValue = Integer.parseInt(readWord());
|
||||
invalChar();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
private String readWord() throws IOException {
|
||||
StringBuffer word = new StringBuffer();
|
||||
int c;
|
||||
while (isChar(c = stream.read())) {
|
||||
word.append((char) c);
|
||||
}
|
||||
stream.unread(c);
|
||||
return word.toString();
|
||||
}
|
||||
|
||||
private imagemap invalChar() throws IOException {
|
||||
int c;
|
||||
while (!isChar(c = stream.read())) {
|
||||
if (c == '#') {
|
||||
while ((char) (c = stream.read()) != '\n' && c > -1);
|
||||
stream.unread(c);
|
||||
}
|
||||
}
|
||||
stream.unread(c);
|
||||
return this;
|
||||
}
|
||||
|
||||
public imagemap copyAll() {
|
||||
pixel[][] curPixels = new pixel[pixels.length][];
|
||||
for (int i = 0; i < pixels.length; i++)
|
||||
curPixels[i] = pixels[i].clone();
|
||||
return new imagemap(width, height, curPixels);
|
||||
}
|
||||
|
||||
private boolean isChar(char c) {
|
||||
return (c != '#' && c != '\n' && c != '\r' && c != '\t' && c != ' ');
|
||||
}
|
||||
|
||||
public String headerString() {
|
||||
return "P3\n" + width + " " + height + "\n" + maxColorValue + "\n";
|
||||
}
|
||||
|
||||
private boolean isChar(int c) {
|
||||
return isChar((char) c);
|
||||
}
|
||||
|
||||
public imagemap getEveryOne(pkpix cb) {
|
||||
for (int r = 0; r < pixels.length; r++) {
|
||||
for (int c = 0; c < pixels[r].length; c++) {
|
||||
cb.handle(this, pixels[r][c], r, c);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public imagemap fileWritter(String path) throws IOException {
|
||||
FileWriter w = new FileWriter(path);
|
||||
String header = "P3\n" + width + " " + height + "\n" + maxColorValue + "\n";
|
||||
w.write(header);
|
||||
for (pixel[] row : pixels) {
|
||||
for (pixel p : row) {
|
||||
w.write(p.toString());
|
||||
}
|
||||
w.write('\n');
|
||||
}
|
||||
w.close();
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
public class pixel {
|
||||
public int red;
|
||||
public int green;
|
||||
public int blue;
|
||||
|
||||
public pixel(int r, int g, int b) {
|
||||
red = r;
|
||||
green = g;
|
||||
blue = b;
|
||||
}
|
||||
|
||||
public pixel(int v) {
|
||||
red = v;
|
||||
green = v;
|
||||
blue = v;
|
||||
}
|
||||
|
||||
public pixel invert() {
|
||||
return invert(255);
|
||||
}
|
||||
|
||||
public pixel invert(int max) {
|
||||
return new pixel(max-red, max-green, max-blue);
|
||||
}
|
||||
|
||||
public pixel grayscale() {
|
||||
int v = (red + green + blue) / 3;
|
||||
return new pixel(v);
|
||||
}
|
||||
|
||||
public pixel emboss(pixel p) {
|
||||
int v = 128 + absMax(
|
||||
red - p.red,
|
||||
green - p.green,
|
||||
blue - p.blue
|
||||
);
|
||||
if (v > 255) v = 255;
|
||||
else if (v < 0) v = 0;
|
||||
return new pixel(v);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return red + " " + green + " " + blue + " ";
|
||||
}
|
||||
|
||||
public static pixel average(pixel...pixels) {
|
||||
int r = 0,
|
||||
g = 0,
|
||||
b = 0;
|
||||
for (pixel p : pixels) {
|
||||
r += p.red;
|
||||
g += p.green;
|
||||
b += p.blue;
|
||||
}
|
||||
r = r / pixels.length;
|
||||
g = g / pixels.length;
|
||||
b = b / pixels.length;
|
||||
return new pixel(r, g, b);
|
||||
}
|
||||
|
||||
public static int absMax(int...vals) {
|
||||
int max = 0;
|
||||
for (int v : vals) {
|
||||
if (Math.abs(v) > Math.abs(max)) {
|
||||
max = v;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
public interface pkpix {
|
||||
void handle(imagemap pixelMap, pixel p, int row, int col);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ImageEditor {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
String infile;
|
||||
String outfile;
|
||||
String operation;
|
||||
int blurLength = 0;
|
||||
try{
|
||||
infile = args[0];
|
||||
outfile = args[1];
|
||||
operation = args[2];
|
||||
}
|
||||
catch(ArrayIndexOutOfBoundsException e) {
|
||||
System.out.println("USAGE: java ImageEditor in-file out-file (grayscale|invert|emboss|motionblur motion-blur-length");
|
||||
return;
|
||||
}
|
||||
try{
|
||||
if(operation.equals("motionblur")) {
|
||||
blurLength = Integer.parseInt(args[3]);
|
||||
if(blurLength < 0){
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if(args.length > 3) {
|
||||
throw new ArrayIndexOutOfBoundsException();
|
||||
}
|
||||
else if(!operation.equals("invert") && !operation.equals("grayscale") && !operation.equals("emboss")){
|
||||
System.out.println("USAGE: java ImageEditor in-file out-file (grayscale|invert|emboss|motionblur motion-blur-length");
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch(ArrayIndexOutOfBoundsException e) {
|
||||
System.out.println("USAGE: java ImageEditor in-file out-file (grayscale|invert|emboss|motionblur motion-blur-length");
|
||||
return;
|
||||
}
|
||||
File srcFile = new File(args[0]);
|
||||
File destFile = new File(args[1]);
|
||||
Picture picture = new Picture(srcFile, destFile, operation, blurLength);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Scanner;
|
||||
|
||||
import java.lang.*;
|
||||
|
||||
public class Picture {
|
||||
|
||||
private File srcFile;
|
||||
private File destFile;
|
||||
private String operation;
|
||||
private int blurLength;
|
||||
private String head;
|
||||
private int width;
|
||||
private int height;
|
||||
private int maximum;
|
||||
private Pixel[][] result;
|
||||
|
||||
public Picture(File srcFile, File destFile, String operation, int blurLength) throws FileNotFoundException {
|
||||
this.srcFile = srcFile;
|
||||
this.destFile = destFile;
|
||||
this.operation = operation;
|
||||
this.blurLength = blurLength;
|
||||
this.makeImage(srcFile);
|
||||
this.makeFile(destFile);
|
||||
|
||||
}
|
||||
public void setTable(Pixel[][] temp){
|
||||
result = temp;
|
||||
}
|
||||
public void setHead(String temp){
|
||||
head = temp;
|
||||
}
|
||||
public void setWidth(int temp){
|
||||
width = temp;
|
||||
}
|
||||
public void setHeight(int temp){
|
||||
height = temp;
|
||||
}
|
||||
public void setMaximum(int temp){
|
||||
maximum = temp;
|
||||
}
|
||||
|
||||
public void makeImage(File srcFile) throws FileNotFoundException {
|
||||
Scanner scanner = new Scanner(srcFile);
|
||||
scanner.useDelimiter("(\\s+)(#[^\\n]*\\n)?(\\s*)|(#[^\\n]*\\n)(\\s+)|(#[^\\n]*\\n)");
|
||||
|
||||
String head = scanner.next();
|
||||
int width = scanner.nextInt();
|
||||
int height = scanner.nextInt();
|
||||
int maximum = scanner.nextInt();
|
||||
setHead(head);
|
||||
setWidth(width);
|
||||
setHeight(height);
|
||||
setMaximum(maximum);
|
||||
|
||||
Pixel[][] table = new Pixel[height][width];
|
||||
|
||||
for(int i = 0; i < height; i++){
|
||||
for(int j = 0; j < width; j++){
|
||||
int red = scanner.nextInt();
|
||||
int green = scanner.nextInt();
|
||||
int blue = scanner.nextInt();
|
||||
|
||||
Pixel pixel = new Pixel(red, green, blue);
|
||||
table[i][j] = pixel;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if(operation.equals("invert")){
|
||||
invert(table);
|
||||
}
|
||||
else if(operation.equals("grayscale")){
|
||||
grayscale(table);
|
||||
}
|
||||
else if(operation.equals("emboss")){
|
||||
emboss(table);
|
||||
}
|
||||
else if(operation.equals("motionblur")){
|
||||
motionblur(table);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void invert(Pixel[][] temp){
|
||||
Pixel[][] table = temp;
|
||||
for(int i = 0; i < height; i++){
|
||||
for(int j = 0; j < width; j++){
|
||||
Pixel newpixel = new Pixel(Math.abs(maximum-table[i][j].getRed()), Math.abs(maximum-table[i][j].getGreen()), Math.abs(maximum-table[i][j].getBlue()));
|
||||
table[i][j] = newpixel;
|
||||
}
|
||||
}
|
||||
setTable(table);
|
||||
|
||||
}
|
||||
public void grayscale(Pixel[][] temp){
|
||||
Pixel[][] table = temp;
|
||||
for(int i = 0; i < height; i++){
|
||||
for(int j = 0; j < width; j++){
|
||||
int average = (table[i][j].getRed() + table[i][j].getGreen() + table[i][j].getBlue())/3;
|
||||
Pixel newpixel = new Pixel(average, average, average);
|
||||
table[i][j] = newpixel;
|
||||
}
|
||||
}
|
||||
setTable(table);
|
||||
|
||||
}
|
||||
public void emboss(Pixel[][] temp){
|
||||
Pixel[][] table = temp;
|
||||
for(int i = 0; i < height; i++){
|
||||
for(int j = 0; j < width; j++){
|
||||
|
||||
if((height-i-1) != 0 && (width-j-1) != 0){
|
||||
|
||||
int redDiff = table[height-i-1][width-j-1].getRed()-table[height-i-2][width-j-2].getRed();
|
||||
int greenDiff = table[height-i-1][width-j-1].getGreen()-table[height-i-2][width-j-2].getGreen();
|
||||
int blueDiff = table[height-i-1][width-j-1].getBlue()-table[height-i-2][width-j-2].getBlue();
|
||||
int maxDiff = Math.max(Math.abs(redDiff), Math.max(Math.abs(greenDiff), Math.abs(blueDiff)));
|
||||
int finalValue = 0;
|
||||
if(maxDiff == Math.abs(redDiff)){
|
||||
finalValue = redDiff + 128;
|
||||
}
|
||||
else if(maxDiff == Math.abs(greenDiff)){
|
||||
finalValue = greenDiff + 128;
|
||||
}
|
||||
else if(maxDiff == Math.abs(blueDiff)){
|
||||
finalValue = blueDiff + 128;
|
||||
}
|
||||
|
||||
if(finalValue < 0){
|
||||
finalValue = 0;
|
||||
}
|
||||
else if(finalValue > 255){
|
||||
finalValue = 255;
|
||||
}
|
||||
|
||||
Pixel newpixel = new Pixel(finalValue, finalValue, finalValue);
|
||||
table[height-i-1][width-j-1] = newpixel;
|
||||
}
|
||||
else{
|
||||
Pixel newpixel = new Pixel(128, 128, 128);
|
||||
table[height-i-1][width-j-1] = newpixel;
|
||||
}
|
||||
}
|
||||
}
|
||||
setTable(table);
|
||||
|
||||
}
|
||||
public void motionblur(Pixel[][] temp){
|
||||
Pixel[][] table = temp;
|
||||
for(int i = 0; i < height; i++){
|
||||
for(int j = 0; j < width; j++){
|
||||
int red = 0;
|
||||
int green = 0;
|
||||
int blue = 0;
|
||||
boolean edge = false;
|
||||
for(int k = 0; k < blurLength; k++){
|
||||
if(j+k < width){
|
||||
red += table[i][j+k].getRed();
|
||||
green += table[i][j+k].getGreen();
|
||||
blue += table[i][j+k].getBlue();
|
||||
}
|
||||
else{
|
||||
Pixel newpixel = new Pixel(red/k, green/k, blue/k);
|
||||
table[i][j] = newpixel;
|
||||
edge = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!edge){
|
||||
Pixel newpixel = new Pixel(red/blurLength, green/blurLength, blue/blurLength);
|
||||
table[i][j] = newpixel;
|
||||
}
|
||||
}
|
||||
}
|
||||
setTable(table);
|
||||
|
||||
}
|
||||
|
||||
public void makeFile(File destFile) throws FileNotFoundException{
|
||||
PrintWriter writer = new PrintWriter(destFile);
|
||||
writer.println(head);
|
||||
writer.print(width + " " + height + "\n");
|
||||
writer.println(maximum);
|
||||
for(int i = 0; i < height; i ++){
|
||||
for(int j = 0; j < width; j++){
|
||||
writer.println(result[i][j].getRed());
|
||||
writer.println(result[i][j].getGreen());
|
||||
writer.println(result[i][j].getBlue());
|
||||
}
|
||||
}
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
public class Pixel {
|
||||
|
||||
private int red;
|
||||
private int green;
|
||||
private int blue;
|
||||
|
||||
public Pixel(int red, int green, int blue) {
|
||||
this.red = red;
|
||||
this.green = green;
|
||||
this.blue = blue;
|
||||
}
|
||||
|
||||
public int getRed(){
|
||||
return red;
|
||||
}
|
||||
public int getGreen(){
|
||||
return green;
|
||||
}
|
||||
public int getBlue(){
|
||||
return blue;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package listem;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface Grep {
|
||||
|
||||
/**
|
||||
* Find lines that match a given pattern in files whose names match another
|
||||
* pattern
|
||||
*
|
||||
* @param directory The base directory to look at files from
|
||||
* @param fileSelectionPattern Pattern for selecting file names
|
||||
* @param substringSelectionPattern Pattern to search for in lines of a file
|
||||
* @param recursive Recursively search through directories
|
||||
* @return A Map containing files that had at least one match found inside them.
|
||||
* Each file is mapped to a list of strings which are the exact strings from
|
||||
* the file where the <code>substringSelectionPattern</code> was found.
|
||||
*/
|
||||
public Map<File, List<String>> grep(File directory, String fileSelectionPattern,
|
||||
String substringSelectionPattern, boolean recursive);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package listem;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class GrepImpl extends superClass implements Grep {
|
||||
|
||||
public GrepImpl(){
|
||||
}
|
||||
public Map<File, List<String>> grep(File directory, String fileSelectionPattern,
|
||||
String substringSelectionPattern, boolean recursive){
|
||||
Map<File, List<String>> mymap = new HashMap<File, List<String>>();
|
||||
ArrayList<File> myfiles = new ArrayList();
|
||||
myfiles = super.initialsearch(directory, fileSelectionPattern, recursive);
|
||||
|
||||
for(int i = 0; i < myfiles.size(); i++){
|
||||
List<String> word = process(myfiles.get(i), substringSelectionPattern);
|
||||
if(word.size() > 0){
|
||||
mymap.put(myfiles.get(i), word);
|
||||
}
|
||||
}
|
||||
return mymap;
|
||||
}
|
||||
|
||||
public List<String> process(File temp, String substringSelection){
|
||||
List<String> word = new ArrayList();
|
||||
Pattern p = Pattern.compile(substringSelection);
|
||||
try {
|
||||
Scanner scanner = new Scanner(temp);
|
||||
while(scanner.hasNextLine()){
|
||||
String line = scanner.nextLine();
|
||||
Matcher m = p.matcher(line);
|
||||
if(m.find()){
|
||||
word.add(line);
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return word;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package listem;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
public interface LineCounter {
|
||||
|
||||
/**
|
||||
* Count the number of lines in files whose names match a given pattern.
|
||||
*
|
||||
* @param directory The base directory to look at files from
|
||||
* @param fileSelectionPattern Pattern for selecting file names
|
||||
* @param recursive Recursively search through directories
|
||||
* @return A Map containing files whose lines were counted. Each file is mapped
|
||||
* to an integer which is the number of lines counted in the file.
|
||||
*/
|
||||
public Map<File, Integer> countLines(File directory, String fileSelectionPattern,
|
||||
boolean recursive);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package listem;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class LineCounterImpl extends superClass implements LineCounter {
|
||||
|
||||
public LineCounterImpl(){
|
||||
}
|
||||
public Map<File, Integer> countLines(File directory, String fileSelectionPattern,
|
||||
boolean recursive){
|
||||
Map<File, Integer> mymap = new HashMap<File, Integer>();
|
||||
ArrayList<File> myfiles = new ArrayList();
|
||||
myfiles = super.initialsearch(directory, fileSelectionPattern, recursive);
|
||||
for(int i = 0; i < myfiles.size(); i++){
|
||||
Integer count = process(myfiles.get(i));
|
||||
if(count > 0){
|
||||
mymap.put(myfiles.get(i), count);
|
||||
}
|
||||
}
|
||||
return mymap;
|
||||
}
|
||||
public Integer process(File temp){
|
||||
Integer count = 0;
|
||||
try {
|
||||
Scanner scanner = new Scanner(temp);
|
||||
while(scanner.hasNextLine()){
|
||||
String line = scanner.nextLine();
|
||||
count ++;
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package listem;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public abstract class superClass {
|
||||
|
||||
private ArrayList<File> myfiles = new ArrayList();
|
||||
superClass(){
|
||||
myfiles.clear();
|
||||
}
|
||||
|
||||
protected ArrayList<File> initialsearch(File directory, String fileSelectionPattern, boolean recursive) {
|
||||
myfiles.clear();
|
||||
myfiles = search(directory, fileSelectionPattern, recursive);
|
||||
return myfiles;
|
||||
}
|
||||
|
||||
protected ArrayList<File> search(File directory, String fileSelectionPattern, boolean recursive){
|
||||
Pattern p = Pattern.compile(fileSelectionPattern);
|
||||
File[] files = directory.listFiles();
|
||||
if(files != null){
|
||||
for(int i = 0; i < files.length; i++){
|
||||
Matcher m = p.matcher(files[i].getName());
|
||||
if(files[i].isDirectory() && recursive){
|
||||
search(files[i], fileSelectionPattern, recursive);
|
||||
}
|
||||
else if(files[i].isFile() && m.matches()){
|
||||
myfiles.add(files[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return myfiles;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user