Skip to content

Commit

Permalink
Merge pull request #101 from Daxunyu/master
Browse files Browse the repository at this point in the history
Update WordCloudAutoFillTest.java
  • Loading branch information
kennycason committed Jun 14, 2021
2 parents 9f43f99 + 00b2f52 commit a9b89d4
Showing 1 changed file with 116 additions and 5 deletions.
121 changes: 116 additions & 5 deletions kumo-core/src/test/java/com/kennycason/kumo/WordCloudAutoFillTest.java
Expand Up @@ -18,13 +18,27 @@
import java.util.*;
import java.util.List;

/**
* This is part of javadoc is makred by Jiaxiang Li
*
*/
public class WordCloudAutoFillTest {

// 我们要有四个测试样例,分别对应单词过少,自定义填充, 默认填充, 以及直接处理字符串链表

/**
* we have four test cases, and they are being applied to the test the
* issue of word too few, autofill, default fill, and directly dealing
* with the string lists
*
*/

private static final String INPUT_PATH ="backgrounds/whale.png";
private static final String DEFAULT_FONT = "Impact";
private static final String DEFAULT_IMAGE_TYPE = "png";

/** This method is for testing the picture of "output_test/a_whale_word_too_few.png"
* @throws IOException
*/

@Test
public void whaleImgWordTooFewTest() throws IOException {
Expand Down Expand Up @@ -55,7 +69,13 @@ public void whaleImgWordTooFewTest() throws IOException {
wordCloud.build(wordFrequencies);
wordCloud.writeToFile("output_test/a_whale_word_too_few.png");
}


/**
* This method is for when there are no word after be filtered with
* the Custom autofill test
* @throws IOException
*/

@Test
public void whaleImgNoWordAfterFilterWithCustomAutoFillTest() throws IOException {
final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
Expand Down Expand Up @@ -85,12 +105,22 @@ public void whaleImgNoWordAfterFilterWithCustomAutoFillTest() throws IOException
wordCloud.build(wordFrequencies);
wordCloud.writeToFile("output_test/a_whale_no_word_with_dararara.png");
}

/**
* This method is for when there are no word after be filtered with
* the default autofill test
* @throws IOException
*
*/
//CS304 Issue Link:
//https://github.com/kennycason/kumo/issues/93

@Test
public void whaleImgNoWordAfterFilterWithDefaultAutoFillTest() throws IOException {
final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
frequencyAnalyzer.setWordFrequenciesToReturn(600);
frequencyAnalyzer.setMinWordLength(5);
//change 5->6
frequencyAnalyzer.setMinWordLength(6);
frequencyAnalyzer.setStopWords(loadStopWords());
final int width = 1500;
final int height = 1000;
Expand All @@ -115,7 +145,14 @@ public void whaleImgNoWordAfterFilterWithDefaultAutoFillTest() throws IOExceptio
wordCloud.build(wordFrequencies);
wordCloud.writeToFile("output_test/a_whale_no_word_with_default_autofill.png");
}


/**
* This method is for the image of whale when manually add more duplicated
* words
* @throws IOException
*/
//CS304 Issue Link:
//https://github.com/kennycason/kumo/issues/93

@Test
public void whaleImgWithListOfStringTest() throws IOException{
Expand All @@ -128,6 +165,8 @@ public void whaleImgWithListOfStringTest() throws IOException{
final List<String> texts = new ArrayList<>();
texts.add("hello");
texts.add("world");
//my added
texts.add("hello world");

final List<WordFrequency> wordFrequencies = frequencyAnalyzer.load(texts, true);
final Dimension dimension = new Dimension(width, height);
Expand All @@ -149,7 +188,12 @@ public void whaleImgWithListOfStringTest() throws IOException{
wordCloud.build(wordFrequencies);
wordCloud.writeToFile("output_test/a_whale_with_string_list.png");
}


/**
* This method is for loading the stop words
* @return
*/

private static Set<String> loadStopWords() {
try {
final List<String> lines = IOUtils.readLines(getInputStream("text/stop_words.txt"));
Expand All @@ -165,3 +209,70 @@ private static InputStream getInputStream(final String path) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}
}

//CS304 (manually written) issue link:
//https://github.com/kennycason/kumo/issues/93

@Test
public void wordtoosmall() throws IOException{
final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
frequencyAnalyzer.setWordFrequenciesToReturn(500);
frequencyAnalyzer.setMinWordLength(6);
frequencyAnalyzer.setStopWords(loadStopWords());
final int width = 500;
final int height = 900;
final List<WordFrequency> wordFrequencies = frequencyAnalyzer.load(getInputStream("text/hello_world.txt"), true);
final Dimension dimension = new Dimension(width, height);
final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
wordCloud.setPadding(1);
wordCloud.setBackgroundColor(Color.WHITE);
InputStream inputStream = null;
try {
inputStream = ImageProcessor.readImage(INPUT_PATH, width, height, DEFAULT_IMAGE_TYPE);
wordCloud.setBackground(new PixelBoundaryBackground(inputStream));
} finally {
inputStream.close();
}
wordCloud.setKumoFont(new KumoFont(DEFAULT_FONT, FontWeight.PLAIN));
wordCloud.setColorPalette(new ColorPalette(new Color(0x4055F1), new Color(0x408DF1), new Color(0x40AAF1), new Color(0x40C5F1), new Color(0x40D3F1), new Color(0x000000)));
wordCloud.setFontScalar(new SqrtFontScalar(7, 35));
wordCloud.build(wordFrequencies);
wordCloud.writeToFile("output_test/a_whale_word_too_few.png");
}

//CS304 (manually written) issue link:
//https://github.com/kennycason/kumo/issues/93
@Test
public void duplicatesmall() throws IOException{
final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
frequencyAnalyzer.setWordFrequenciesToReturn(600);
frequencyAnalyzer.setMinWordLength(5);
frequencyAnalyzer.setStopWords(loadStopWords());
final int width = 300;
final int height = 400;
final List<String> texts = new ArrayList<>();
//could manually add more here
texts.add("hellohellohello");
texts.add("worldworldworld");

final List<WordFrequency> wordFrequencies = frequencyAnalyzer.load(texts, true);
final Dimension dimension = new Dimension(width, height);
final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
wordCloud.setPadding(1);
wordCloud.setBackgroundColor(Color.WHITE);
InputStream inputStream = null;
try {
inputStream = ImageProcessor.readImage(INPUT_PATH, width, height, DEFAULT_IMAGE_TYPE);

wordCloud.setBackground(new PixelBoundaryBackground(inputStream));

} finally {
inputStream.close();
}
wordCloud.setKumoFont(new KumoFont(DEFAULT_FONT, FontWeight.PLAIN));
wordCloud.setColorPalette(new ColorPalette(new Color(0x4055F1), new Color(0x408DF1), new Color(0x40AAF1), new Color(0x40C5F1), new Color(0x40D3F1), new Color(0x000000)));
wordCloud.setFontScalar(new SqrtFontScalar(8, 30));
wordCloud.build(wordFrequencies);
wordCloud.writeToFile("output_test/a_whale_with_string_list.png");
}
}

0 comments on commit a9b89d4

Please sign in to comment.