Skip to content

Commit

Permalink
Merge pull request #74 from lingfengsan/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
lingfengsan authored Jan 25, 2018
2 parents 8461fa1 + 0711181 commit 772665f
Show file tree
Hide file tree
Showing 15 changed files with 481 additions and 215 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ target
/src/resource/adb/
.DS_Store
/hero.properties
/src/main/java/utils/TestSomething.java
/src/main/resources/chromedriver.exe
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@

1. 将手机点击到直播界面(在这里我们先打开一张图片);
2. 用Adb 工具获取当前手机截图
3. 用tessOCR进行图像识别,提取文字;
3. 用OCR进行图像识别,提取文字;
4. 将文字中的问题和答案提取出来;
5. 使用百度搜索并打开网页,然后统计搜索得到结果数量
6. 计算pmi
7. 选择pmi值最高的为答案。
5. 使用搜索工具并打开网页
6. 计算问题和答案的相似度,此处调用百度的Nlp接口或者采用PMI的方式
> 该公式的依据来自于维基百科:
https://en.wikipedia.org/wiki/Pointwise_mutual_information
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
+ 2018-01-25
+ 增加百度短文本识别API,欢迎尝试[短文本相似度](https://console.bce.baidu.com/ai/#/ai/nlp/overview/index),请去该链接申请该服务
+ 2018-01-24
+ 不在支持命令行模式,请运行MainGUI
+ 2018-01-22
+ 增加了本地保存配置信息
+ 建议使用百度OCR+搜狗,正确率更高。
Expand Down
64 changes: 0 additions & 64 deletions src/main/java/Main.java

This file was deleted.

119 changes: 75 additions & 44 deletions src/main/java/MainGUI.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gui.BaiDuOCRSettingDialog;
import gui.BaiduNlpSettingDialog;
import ocr.OCR;
import ocr.impl.OCRFactory;
import org.apache.log4j.Logger;
Expand All @@ -10,7 +11,6 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Properties;

/**
* Created by lingfengsan on 2018/1/16.
Expand All @@ -25,24 +25,23 @@ public class MainGUI {
private static ButtonGroup ocrSelectionButton = new ButtonGroup();
private static ButtonGroup searchSelectionButton = new ButtonGroup();
private static ButtonGroup patternSelectionButton = new ButtonGroup();
private static int ocrSelection = 1;
private static ButtonGroup similaritySelectionButton = new ButtonGroup();
private static int patternSelection = 1;
private static int searchSelection = 1;
private static JTextArea resultTextArea;
private static JFrame frame = new JFrame("答题助手");
private static final CommonPattern COMMON_PATTERN = new CommonPattern();
private static Properties heroProperties = new Properties();

public static void main(String[] args) throws IOException {
// Setting the width and height of frame
frame.setSize(500, 800);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
try{
loadConfig();
Utils.loadConfig();
}catch (Exception e){
initConfig();
Utils.initialConfig();
}
loadConfig();
Utils.loadConfig();
// 创建面板
JPanel panel = new JPanel();
frame.add(panel);
Expand All @@ -53,6 +52,7 @@ public static void main(String[] args) throws IOException {
addSearchSelection(panel);
addSetFinishButton(panel);
addRunButton(panel);
addSimilaritySelection(panel);
addResultTextArea(panel);
addPatternSelection(panel);
// 设置界面可见
Expand All @@ -64,7 +64,7 @@ private static void addAdbPath(JPanel panel) {
JLabel adbPathLabel = new JLabel("adb路径:");
adbPathLabel.setBounds(10, 20, 100, 25);
panel.add(adbPathLabel);
adbPathText = new JTextField(heroProperties.getProperty("ADB_PATH"), 30);
adbPathText = new JTextField(Config.getAdbPath(), 30);
adbPathText.setBounds(100, 20, 100, 25);
panel.add(adbPathText);
}
Expand All @@ -74,12 +74,15 @@ private static void addImagePath(JPanel panel) {
JLabel imagePathLabel = new JLabel("图片存放路径:");
imagePathLabel.setBounds(10, 45, 100, 25);
panel.add(imagePathLabel);
imagePathText = new JTextField(heroProperties.getProperty("PHOTO_PATH"), 30);
imagePathText = new JTextField(Config.getPhotoPath(), 30);
imagePathText.setBounds(100, 45, 100, 25);
panel.add(imagePathText);
}

// 创建OCR选择
/**
* 创建OCR选择
* @param panel 创建OCR选择
*/
private static void addOCRSelection(JPanel panel) {
JLabel ocrSelectionLabel = new JLabel("OCR方式:");
ocrSelectionLabel.setBounds(10, 70, 100, 25);
Expand All @@ -88,26 +91,55 @@ private static void addOCRSelection(JPanel panel) {
panel.add(ocrSelectionLabel);
}

private static void addOCRSelection(JPanel panel, int X, String text, final int selection) {
private static void addOCRSelection(JPanel panel, int x, String text, final int selection) {
final JRadioButton ocrButton = new JRadioButton(text);
ocrButton.setBounds(X, 70, 165, 25);
ocrButton.setBounds(x, 70, 165, 25);
ocrSelectionButton.add(ocrButton);
panel.add(ocrButton);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ocrSelection = selection;
if (selection == 2) {
new BaiDuOCRSettingDialog(frame);
}
OCR ocr = OCR_FACTORY.getOcr(ocrSelection);
OCR ocr = OCR_FACTORY.getOcr(selection);
COMMON_PATTERN.setOcr(ocr);
}
};
ocrButton.addActionListener(listener);
}
/**
* 创建相似度选择
* @param panel 创建相似度选择
*/
private static void addSimilaritySelection(JPanel panel) {
JLabel similaritySelectionLabel = new JLabel("相似度方式:");
similaritySelectionLabel.setBounds(10, 145, 100, 25);
addSimilaritySelection(panel, 100, "PMI", 1);
addSimilaritySelection(panel, 300, "百度Nlp", 2);
panel.add(similaritySelectionLabel);
}

// 创建搜索选择
private static void addSimilaritySelection(JPanel panel, int x, String text, final int selection) {
final JRadioButton jRadioButton = new JRadioButton(text);
jRadioButton.setBounds(x, 145, 165, 25);
similaritySelectionButton.add(jRadioButton);
panel.add(jRadioButton);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (selection == 2) {
new BaiduNlpSettingDialog(frame);
}
}
};
jRadioButton.addActionListener(listener);
}

/**
*
* @param panel 创建搜索选择
*/
private static void addSearchSelection(JPanel panel) {
JLabel searchSelectionLabel = new JLabel("搜索方式:");
searchSelectionLabel.setBounds(10, 95, 100, 25);
Expand All @@ -116,9 +148,9 @@ private static void addSearchSelection(JPanel panel) {
panel.add(searchSelectionLabel);
}

private static void addSearchSelection(JPanel panel, int X, String text, final int selection) {
private static void addSearchSelection(JPanel panel, int x, String text, final int selection) {
final JRadioButton searchButton = new JRadioButton(text);
searchButton.setBounds(X, 95, 165, 25);
searchButton.setBounds(x, 95, 165, 25);
searchSelectionButton.add(searchButton);
panel.add(searchButton);
ActionListener listener = new ActionListener() {
Expand All @@ -130,7 +162,10 @@ public void actionPerformed(ActionEvent e) {
searchButton.addActionListener(listener);
}

// 创建模式选择
/**
*
* @param panel 创建模式选择
*/
private static void addPatternSelection(JPanel panel) {
JLabel patternSelectionLabel = new JLabel("游戏模式:");
patternSelectionLabel.setBounds(10, 120, 100, 25);
Expand All @@ -139,9 +174,9 @@ private static void addPatternSelection(JPanel panel) {
panel.add(patternSelectionLabel);
}

private static void addPatternSelection(JPanel panel, int X, String text, final int selection) {
private static void addPatternSelection(JPanel panel, int x, String text, final int selection) {
final JRadioButton patternButton = new JRadioButton(text);
patternButton.setBounds(X, 120, 165, 25);
patternButton.setBounds(x, 120, 165, 25);
patternSelectionButton.add(patternButton);
panel.add(patternButton);
ActionListener listener = new ActionListener() {
Expand All @@ -153,16 +188,24 @@ public void actionPerformed(ActionEvent e) {
patternButton.addActionListener(listener);
}

//增加设置完成按钮
/**
*
* @param panel 增加设置完成按钮
*/
private static void addSetFinishButton(JPanel panel) {
final JButton setFinishButton = new JButton("设置完成");
setFinishButton.setBounds(40, 145, 120, 25);
setFinishButton.setBounds(40, 170, 120, 25);
panel.add(setFinishButton);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Config.setAdbPath(adbPathText.getText());
Config.setPhotoPath(imagePathText.getText());
try {
Utils.storeConfig();
} catch (IOException e1) {
logger.error("存储配置信息失败");
}
Utils utils = new Utils(Config.getAdbPath(), Config.getPhotoPath());
COMMON_PATTERN.setUtils(utils);
COMMON_PATTERN.setPatterSelection(patternSelection);
Expand All @@ -172,10 +215,13 @@ public void actionPerformed(ActionEvent e) {
setFinishButton.addActionListener(listener);
}

//增加获取答案按钮
/**
*
* @param panel 增加获取答案按钮
*/
private static void addRunButton(JPanel panel) {
final JButton runButton = new JButton("获取答案");
runButton.setBounds(160, 145, 120, 25);
runButton.setBounds(160, 170, 120, 25);
panel.add(runButton);
ActionListener listener = new ActionListener() {
@Override
Expand All @@ -192,33 +238,18 @@ public void actionPerformed(ActionEvent e) {
runButton.addActionListener(listener);
}

// 创建图片存放路径
/**
*
* @param panel 创建图片存放路径
*/
private static void addResultTextArea(JPanel panel) {
resultTextArea = new JTextArea();
resultTextArea.setBounds(10, 170, 400, 400);
resultTextArea.setBounds(10, 195, 400, 400);
panel.add(resultTextArea);
}

private static void initConfig() throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("hero.properties", true);
heroProperties.setProperty("ADB_PATH", "D:\\adb\\adb");
heroProperties.setProperty("PHOTO_PATH", "D:\\Photo");
heroProperties.setProperty("APP_ID", "APP_ID");
heroProperties.setProperty("API_KEY", "API_KEY");
heroProperties.setProperty("SECRET_KEY", "SECRET_KEY");
heroProperties.store(fileOutputStream, "million hero properties");
fileOutputStream.close();
}

private static void loadConfig() throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream("hero.properties"));
heroProperties.load(in);
for (String key : heroProperties.stringPropertyNames()) {
System.out.println(key+":"+heroProperties.getProperty(key));
Config.set(key, heroProperties.getProperty(key));
}
in.close();
}



}
Loading

0 comments on commit 772665f

Please sign in to comment.