Commit e120eec0 by Hut

cleanup

parent c8bfdb8d
...@@ -6,12 +6,12 @@ ...@@ -6,12 +6,12 @@
<groupId>passive.directory</groupId> <groupId>passive.directory</groupId>
<artifactId>markov</artifactId> <artifactId>markov</artifactId>
<version>0.4</version> <version>0.5</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.9</java.version> <java.version>1.8</java.version>
<junit.jupiter.version>5.0.3</junit.jupiter.version> <junit.jupiter.version>5.0.3</junit.jupiter.version>
<junit.platform.version>1.0.3</junit.platform.version> <junit.platform.version>1.0.3</junit.platform.version>
</properties> </properties>
...@@ -109,7 +109,7 @@ ...@@ -109,7 +109,7 @@
<configuration> <configuration>
<archive> <archive>
<manifest> <manifest>
<mainClass>markov.Mail</mainClass> <mainClass>markov.stuff.Mail</mainClass>
</manifest> </manifest>
</archive> </archive>
<descriptorRefs> <descriptorRefs>
......
package markov; package markov.stuff;
import com.google.common.base.Stopwatch; import com.google.common.base.Stopwatch;
import directory.passive.huffman.ByteHuffmanCodeBuilder; import directory.passive.huffman.ByteHuffmanCodeBuilder;
import directory.passive.huffman.HuffmanCode; import directory.passive.huffman.HuffmanCode;
import markov.Builder;
import markov.Data;
import markov.Sentence;
import markov.Shortener;
import markov.huffman.HuffmanStore; import markov.huffman.HuffmanStore;
import markov.shortener.ShortenerByteHUffmanTrainerImpl; import markov.shortener.ShortenerByteHUffmanTrainerImpl;
import markov.shortener.ShortenerByteHuffmanImpl; import markov.shortener.ShortenerByteHuffmanImpl;
import markov.shortener.ShortenerIntHuffmanImpl; import markov.shortener.ShortenerIntHuffmanImpl;
import markov.shortener.ShortenerSimpleImpl; import markov.shortener.ShortenerSimpleImpl;
import markov.stuff.CountMap;
import markov.stuff.Inspector;
import markov.stuff.SimpleCountMap;
import markov.stuff.Utils;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
...@@ -43,7 +43,7 @@ public class Mail { ...@@ -43,7 +43,7 @@ public class Mail {
public static void main(String[] args) public static void main(String[] args)
throws IOException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException { throws IOException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
System.in.read(); System.in.read();
new Mail().timeCreation(); new Mail().createHuffmanMaps();
} }
private void timeCreation() { private void timeCreation() {
......
package markov; package markov.stuff;
import markov.stuff.Utils; import markov.Builder;
import markov.Data;
import markov.Renderer;
import markov.Sentence;
import java.util.stream.Stream; import java.util.stream.Stream;
...@@ -9,7 +12,6 @@ public class Main { ...@@ -9,7 +12,6 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
int prefixLength = 2; int prefixLength = 2;
// Data data = new Parser_first_version(prefixLength).parse(input);
Data data = Utils.parse(Stream.of(input.replace("Kai☺UWE empfiehlt ", "").split("\n")), prefixLength); Data data = Utils.parse(Stream.of(input.replace("Kai☺UWE empfiehlt ", "").split("\n")), prefixLength);
Builder b = new Builder(prefixLength, data); Builder b = new Builder(prefixLength, data);
...@@ -20,7 +22,6 @@ public class Main { ...@@ -20,7 +22,6 @@ public class Main {
System.out.println(r2.render(sentence)); System.out.println(r2.render(sentence));
} }
// Collection<Map.Entry<Prefix, Decission>> a = b.average(); // Collection<Map.Entry<Prefix, Decission>> a = b.average();
// System.out.println("average: " + r.render(a)); // System.out.println("average: " + r.render(a));
// for (Map.Entry<Prefix, Decission> e : a) { // for (Map.Entry<Prefix, Decission> e : a) {
......
...@@ -10,7 +10,6 @@ import javax.mail.Session; ...@@ -10,7 +10,6 @@ import javax.mail.Session;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
...@@ -46,26 +45,16 @@ public class Utils { ...@@ -46,26 +45,16 @@ public class Utils {
public static Data parse(Stream<String> input, int prefixLength) { public static Data parse(Stream<String> input, int prefixLength) {
Collection<Integer> collectionDummy = new ArrayList<>(); Collection<Integer> collectionDummy = new ArrayList<>();
collectionDummy.add(prefixLength); collectionDummy.add(prefixLength);
return new Parser().parse(new Tokenizer().tokenize(input), collectionDummy).values() return new Parser().parse(new Tokenizer().tokenize(input), collectionDummy)
.stream().findFirst().get(); .values()
.stream()
.findFirst()
.get();
} }
public static File getFile(String location) { public static Map<Integer, Data> createDataMap(int maxPrefix) {
File file; return createDataMap(maxPrefix,
if (location.startsWith("classpath:")) { getFile(System.getProperty("mail.folder", "C:\\Users\\admin\\Desktop\\emails")));
URL url = Utils.class.getClassLoader().getResource(location.replace("classpath:", ""));
try {
file = new File(url.toURI());
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
} else {
file = new File(location);
}
if (!file.exists() || !file.canRead() || !file.canWrite()) {
throw new IllegalArgumentException(String.format("could not not use file: %s", file.getName()));
}
return file;
} }
public static void maybePrintPercentages(final int i, final int max) { public static void maybePrintPercentages(final int i, final int max) {
...@@ -92,34 +81,20 @@ public class Utils { ...@@ -92,34 +81,20 @@ public class Utils {
} }
} }
public static Map<Integer, Data> deserializeDataMap() throws IOException,
ClassNotFoundException {
System.out.println("reading map");
Map<Integer, Data> dataMap = (Map<Integer, Data>) new ObjectInputStream(
Files.newInputStream(new File(
"C:\\Users\\admin\\git\\markovdisplay\\target\\classes\\data").toPath()))
.readObject();
System.out.println("read map");
return dataMap;
}
public static Map<Integer, Data> createDataMap(int maxPrefix) {
return createDataMap(maxPrefix, new File("C:\\Users\\admin\\Desktop\\emails"));
}
public static Map<Integer, Data> createDataMap(int maxPrefix, File parent) { public static Map<Integer, Data> createDataMap(int maxPrefix, File parent) {
System.out.println("generating data map..."); System.out.println("generating data map...");
Stream<String> mails = Arrays.stream(parent.listFiles()).limit(500).map(File::toPath).map Stream<String> mails = Arrays.stream(parent.listFiles()).limit(500).map(File::toPath).map(
(path -> { path -> {
try { try {
MimeMessage m = new MimeMessage( MimeMessage m =
Session.getDefaultInstance(new Properties()), Files.newInputStream(path)); new MimeMessage(Session.getDefaultInstance(new Properties()),
return m; Files.newInputStream(path));
} catch (IOException | MessagingException e) { return m;
throw new RuntimeException(e); } catch (IOException | MessagingException e) {
} throw new RuntimeException(e);
}).filter(mimeMessage -> { }
}).filter(mimeMessage -> {
try { try {
return mimeMessage.getContentType().contains("text/plain"); return mimeMessage.getContentType().contains("text/plain");
} catch (MessagingException e) { } catch (MessagingException e) {
...@@ -136,9 +111,28 @@ public class Utils { ...@@ -136,9 +111,28 @@ public class Utils {
Tokenizer tokenizer = new Tokenizer(); Tokenizer tokenizer = new Tokenizer();
Stream<Stream<Token>> tokens = tokenizer.tokenize(mails); Stream<Stream<Token>> tokens = tokenizer.tokenize(mails);
Map<Integer, Data> ret = parser.parse(tokens, IntStream.range(1, maxPrefix + 1) Map<Integer, Data> ret = parser.parse(tokens, IntStream.range(1, maxPrefix + 1)
.boxed() .boxed()
.collect(Collectors.toList())); .collect(Collectors.toList()));
System.out.println("generated data map!"); System.out.println("generated data map!");
return ret; return ret;
} }
public static File getFile(String location) {
File file;
if (location.startsWith("classpath:")) {
URL url = Utils.class.getClassLoader().getResource(location.replace("classpath:", ""));
try {
file = new File(url.toURI());
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
} else {
file = new File(location);
}
if (!file.exists() || !file.canRead() || !file.canWrite()) {
throw new IllegalArgumentException(String.format("could not not use file: %s",
file.getName()));
}
return file;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment