|
@@ -1,8 +1,12 @@
|
|
|
package a;
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
+import java.io.FileReader;
|
|
|
import java.io.InputStream;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
@@ -10,6 +14,10 @@ import java.util.Random;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
+import org.ansj.domain.Result;
|
|
|
+import org.ansj.domain.Term;
|
|
|
+import org.ansj.splitWord.analysis.ToAnalysis;
|
|
|
+
|
|
|
import com.persagy.ibms.data.sdk.util.Constant;
|
|
|
import com.persagy.ibms.data.sdk.util.ImageUtil;
|
|
|
|
|
@@ -47,7 +55,7 @@ public class A {
|
|
|
InputStream is = new FileInputStream(choose);
|
|
|
BufferedImage bi = ImageIO.read(is);
|
|
|
is.close();
|
|
|
- List<String> strList = ImageUtil.toStringList(bi, true);
|
|
|
+ List<String> strList = ImageUtil.toStringList(bi, false);
|
|
|
for (String str : strList) {
|
|
|
System.out.println(str);
|
|
|
// log.error(str);
|
|
@@ -60,24 +68,131 @@ public class A {
|
|
|
}
|
|
|
|
|
|
public static void print(String status) {
|
|
|
- log.warn("compute result: " + status);
|
|
|
File file = new File("img" + Constant.getSeperator() + status);
|
|
|
if (file.exists() && file.isDirectory()) {
|
|
|
printOne(file, true, -1);
|
|
|
}
|
|
|
+ log.warn("compute result: " + status);
|
|
|
}
|
|
|
|
|
|
- public static void print_joke(String status) {
|
|
|
- File file = new File("img" + Constant.getSeperator() + status + Constant.getSeperator() + "joke");
|
|
|
- if (file.exists() && file.isDirectory()) {
|
|
|
- for (int i = 0; i < 60; i++) {
|
|
|
+ // static String[] fuhao1 = { ",", ".", "?", ":", ";", "'", "\"", "(", ")", "!", "、" };
|
|
|
+ // static String[] fuhao2 = { ",", "。", "?", ":", ";", "‘", "’", "“", "”", "(", ")", "!" };
|
|
|
+ static String[] fuhao1 = { ",", ".", ":", ";", "'", "\"", "(", ")", "、" };
|
|
|
+ static String[] fuhao2 = { ",", "。", ":", ";", "‘", "’", "“", "”", "(", ")" };
|
|
|
+
|
|
|
+ public static void chinese_split(String title, List<String> lineList) {
|
|
|
+ Result result = ToAnalysis.parse(title);
|
|
|
+ List<Term> termList = result.getTerms();
|
|
|
+ List<String> tmpList = new ArrayList<String>();
|
|
|
+ for (Term Term : termList) {
|
|
|
+ String name = Term.getName();
|
|
|
+ boolean isfuhao = false;
|
|
|
+ if (!isfuhao) {
|
|
|
+ for (String fuhao : fuhao1) {
|
|
|
+ if (fuhao.equals(name)) {
|
|
|
+ isfuhao = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!isfuhao) {
|
|
|
+ for (String fuhao : fuhao2) {
|
|
|
+ if (fuhao.equals(name)) {
|
|
|
+ isfuhao = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!isfuhao) {
|
|
|
+ int index = 0;
|
|
|
+ while (index < name.length()) {
|
|
|
+ int next_index;
|
|
|
+ if (name.length() == index + 3) {
|
|
|
+ next_index = index + 3;
|
|
|
+ } else {
|
|
|
+ next_index = index + 2 < name.length() ? index + 2 : name.length();
|
|
|
+ }
|
|
|
+ tmpList.add(name.substring(index, next_index));
|
|
|
+ index = next_index;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int i = 0; i < tmpList.size(); i++) {
|
|
|
+ String curr = tmpList.get(i);
|
|
|
+ if (i < tmpList.size() - 1) {
|
|
|
+ String next = tmpList.get(i + 1);
|
|
|
+ if (curr.length() + next.length() <= 2) {
|
|
|
+ lineList.add(curr + next);
|
|
|
+ i++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ lineList.add(curr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void print_text(String status, String filename, boolean all, boolean sleep) {
|
|
|
+ File file = new File("img" + Constant.getSeperator() + filename);
|
|
|
+ if (file.exists() && file.isFile()) {
|
|
|
+ List<String> lineList = new ArrayList<String>();
|
|
|
+ try {
|
|
|
+ BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
|
+ String temp;
|
|
|
+ while ((temp = reader.readLine()) != null) {
|
|
|
+ if (temp.length() > 0) {
|
|
|
+ lineList.add(temp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ reader.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> wordList = new ArrayList<String>();
|
|
|
+ if (all) {
|
|
|
+ for (String line : lineList) {
|
|
|
+ chinese_split(line, wordList);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Random rand = new Random();
|
|
|
+ int index = rand.nextInt(lineList.size());
|
|
|
+ chinese_split(lineList.get(index), wordList);
|
|
|
+ }
|
|
|
+ boolean isTextBlack = true;
|
|
|
+ for (String word : wordList) {
|
|
|
+ if (sleep) {
|
|
|
+ try {
|
|
|
+ Thread.sleep(1000L);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ int fontSize = word.length() == 1 ? 108 : (word.length() == 2 ? 64 : 48);
|
|
|
try {
|
|
|
- Thread.sleep(1000L);
|
|
|
- } catch (InterruptedException e) {
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ ImageUtil.drawChinese(word, false, fontSize, isTextBlack, os);
|
|
|
+ os.close();
|
|
|
+ InputStream is = new ByteArrayInputStream(os.toByteArray());
|
|
|
+ BufferedImage bi = ImageIO.read(is);
|
|
|
+ is.close();
|
|
|
+ List<String> strList = ImageUtil.toStringList(bi);
|
|
|
+ for (String str : strList) {
|
|
|
+ System.out.println(str);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
}
|
|
|
- log.warn("compute result: " + status);
|
|
|
- printOne(file, false, i);
|
|
|
+ log.warn(status);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ A.print_text("good", "joke.txt", false, false);
|
|
|
+ A.print_text("good", "joke.txt", false, false);
|
|
|
+ List<String> wordList = new ArrayList<String>();
|
|
|
+ A.chinese_split("我叫李太白,我是一个诗人,我生活在唐朝", wordList);
|
|
|
+ for (String word : wordList) {
|
|
|
+ System.out.println(word);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|