Skip to content

Commit

Permalink
Start the new book project for issue #65.
Browse files Browse the repository at this point in the history
Also create Poem class with word sorting.
  • Loading branch information
donkirkby committed Nov 1, 2015
1 parent f48cf7f commit 0a03de5
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 0 deletions.
8 changes: 8 additions & 0 deletions book/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry combineaccessrules="false" kind="src" path="/vograbulary-core"/>
<classpathentry kind="lib" path="lib/pdfclown.jar" sourcepath="lib/pdfclown-0.1.2-sources.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions book/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>vograbulary-book</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Binary file added book/lib/pdfclown-0.1.2-javadoc.jar
Binary file not shown.
Binary file added book/lib/pdfclown-0.1.2-sources.jar
Binary file not shown.
Binary file added book/lib/pdfclown.jar
Binary file not shown.
117 changes: 117 additions & 0 deletions book/src/com/github/donkirkby/vograbulary/VograbularyBook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.github.donkirkby.vograbulary;

import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.util.Date;

import org.pdfclown.documents.Document;
import org.pdfclown.documents.Page;
import org.pdfclown.documents.contents.composition.BlockComposer;
import org.pdfclown.documents.contents.composition.PrimitiveComposer;
import org.pdfclown.documents.contents.composition.XAlignmentEnum;
import org.pdfclown.documents.contents.composition.YAlignmentEnum;
import org.pdfclown.documents.contents.fonts.StandardType1Font;
import org.pdfclown.documents.interaction.viewer.ViewerPreferences;
import org.pdfclown.documents.interchange.metadata.Information;
import org.pdfclown.files.File;
import org.pdfclown.files.SerializationModeEnum;

public class VograbularyBook {
public static void main(String[] args) {
try {
File file = new File();
Document document = file.getDocument();
populate(document);
serialize(file, "vograbulary.pdf");

System.out.println("Done.");
} catch (IOException e) {
e.printStackTrace();
}
}

private static void populate(Document document) {
Page page = new Page(document); // Instantiates the page inside the
// document context.
document.getPages().add(page); // Puts the page in the pages collection.

PrimitiveComposer composer = new PrimitiveComposer(page);
BlockComposer blockComposer = new BlockComposer(composer);

StandardType1Font titleFont = new StandardType1Font(
document,
StandardType1Font.FamilyEnum.Helvetica,
true, // bold
false); // italic
int titleFontSize = 20;
StandardType1Font textFont = new StandardType1Font(
document,
StandardType1Font.FamilyEnum.Times,
false, // bold
false); // italic
int textFontSize = 12;
StandardType1Font puzzleFont = new StandardType1Font(
document,
StandardType1Font.FamilyEnum.Courier,
false, // bold
false); // italic
int puzzleFontSize = 12;

Dimension2D pageSize = page.getSize();
Rectangle2D titleFrame = new Rectangle2D.Double(
20,
50,
pageSize.getWidth() - 90,
pageSize.getHeight() - 250
);
blockComposer.begin(titleFrame, XAlignmentEnum.Center, YAlignmentEnum.Top);
composer.setFont(titleFont, titleFontSize);
blockComposer.showText("Vograbulary");
blockComposer.end();
Rectangle2D usedSpace = blockComposer.getBoundBox();
Rectangle2D bodyFrame = new Rectangle2D.Double(
titleFrame.getX(),
usedSpace.getMaxY() + 10,
titleFrame.getWidth(),
titleFrame.getHeight() - usedSpace.getHeight());
blockComposer.begin(bodyFrame, XAlignmentEnum.Left, YAlignmentEnum.Top);
composer.setFont(textFont, textFontSize);
blockComposer.showText(
"Each puzzle starts with a poem, then the letters in each " +
"word are sorted. To solve the puzzle, find the original " +
"words and read the poem. Solutions are listed at the end.");
blockComposer.showBreak();
composer.setFont(puzzleFont, puzzleFontSize);
blockComposer.showText("ARX 23");
blockComposer.showBreak();
blockComposer.showText("ARX 25");
blockComposer.end();

composer.flush();
}

private static String serialize(File file, String filePath) throws IOException {
applyDocumentSettings(file.getDocument());

java.io.File outputFile = new java.io.File(filePath);
file.save(outputFile, SerializationModeEnum.Standard);

return outputFile.getPath();
}

private static void applyDocumentSettings(Document document) {
ViewerPreferences view = new ViewerPreferences(document);
document.setViewerPreferences(view);
view.setDisplayDocTitle(true);

Information info = document.getInformation();
info.clear();
info.setAuthor("Don Kirkby");
info.setCreationDate(new Date());
info.setCreator("Don Kirkby");
info.setTitle("Vograbulary");
info.setSubject("Word puzzles from the Vograbulary project");
info.setKeywords("word puzzles");
}
}
68 changes: 68 additions & 0 deletions core/src/com/github/donkirkby/vograbulary/poemsorting/Poem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.github.donkirkby.vograbulary.poemsorting;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Poem {
private String title;
private ArrayList<String> lines;

public static List<Poem> load(String... lines) {
return load(Arrays.asList(lines));
}

public static List<Poem> load(List<String> lines) {
List<Poem> poems = new ArrayList<Poem>();
Poem poem = null;
String title = null;
for (String line : lines) {
if (line.startsWith("#")) {
if (poem != null) {
poems.add(poem);
poem = null;
}
title = line.replaceAll("^#+\\s*(.*\\S)\\s*#+$", "$1");
}
else {
if (poem == null){
poem = new Poem();
poem.title = title;
poem.lines = new ArrayList<String>();
}
poem.lines.add(line);
}
}
poems.add(poem);
return poems;
}

public String getTitle() {
return title;
}

public List<String> getLines() {
return lines;
}

public Poem sortWords() {
Poem poem = new Poem();
poem.lines = new ArrayList<String>();
char[] buffer = new char[1000];
for (String line : lines) {
StringBuilder sortedWords = new StringBuilder();
String[] words = line.toLowerCase().split(" ");
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (i > 0) {
sortedWords.append(" ");
}
word.getChars(0, word.length(), buffer, 0);
Arrays.sort(buffer, 0, word.length());
sortedWords.append(buffer, 0, word.length());
}
poem.lines.add(sortedWords.toString());
}
return poem;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.github.donkirkby.vograbulary.poemsorting;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class PoemTest {
@Test
public void loadLines() {
List<Poem> poems = Poem.load("The first line,", "And the second.");

assertThat("poems length", poems.size(), is(1));
Poem poem = poems.get(0);
List<String> lines = poem.getLines();
assertThat("line count", lines.size(), is(2));
assertThat("second line", lines.get(1), is("And the second."));
}

@Test
public void loadTitle() {
List<Poem> poems = Poem.load(
"# A Poem #",
"The first line,",
"And the second.");

assertThat("poems length", poems.size(), is(1));
Poem poem = poems.get(0);
List<String> lines = poem.getLines();
assertThat("line count", lines.size(), is(2));
assertThat("second line", lines.get(1), is("And the second."));
assertThat("title", poem.getTitle(), is("A Poem"));
}

@Test
public void loadTwoPoems() {
List<Poem> poems = Poem.load(
"# A Poem #",
"The first line,",
"And the second.",
"# Another Poem #",
"With one line");

assertThat("poems length", poems.size(), is(2));
assertThat("first line count", poems.get(0).getLines().size(), is(2));
assertThat("first title", poems.get(0).getTitle(), is("A Poem"));
assertThat("second line count", poems.get(1).getLines().size(), is(1));
assertThat("second title", poems.get(1).getTitle(), is("Another Poem"));
}

@Test
public void sortWords() {
List<Poem> poems = Poem.load(
"# A Poem #",
"The first line,",
"And the second.");
Poem poem = poems.get(0);

Poem sorted = poem.sortWords();

assertThat("lines", sorted.getLines(), is(Arrays.asList(
"eht first ,eiln",
"adn eht .cdenos")));
assertThat("title", poem.getTitle(), is("A Poem"));
}
}
44 changes: 44 additions & 0 deletions wordpile/pinagrams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from collections import defaultdict
from nltk.corpus import brown
from nltk.corpus import gutenberg

from word_loader import WordLoader


def print_anagrams(sorted_words, all_words):
match_count = 0
valid_set = set(all_words)
matches = defaultdict(list) # {sorted_letters: [word]}
for word in sorted_words:
if word in valid_set:
sorted_letters = ''.join(sorted(word))
matching_words = matches[sorted_letters]
matching_words.append(word)
if len(matching_words) > 1:
print '\n'.join(matching_words)
print
match_count += 1
if match_count > 100:
return


def main():
loader = WordLoader()
loader.load_valid_words_from_aspell("en_GB")
loader.load_valid_words_from_aspell("en_US")
all_words = brown.words() + gutenberg.words()
sorted_words_filename = 'sorted_words.txt'
loader.write_sorted_words(all_words, sorted_words_filename)
sorted_words = loader.sorted_words
print_anagrams(sorted_words, all_words)


def test():
all_words = 'abcd efgh dabc'.split()
sorted_words = 'abcd dabc efgh'.split()
print_anagrams(sorted_words, all_words)

if __name__ == '__main__':
main()
elif __name__ == '__live_coding__':
test()

0 comments on commit 0a03de5

Please sign in to comment.