From ec7f2d426fb5319958adcc1543e01e7afabc6e50 Mon Sep 17 00:00:00 2001 From: tink-expo Date: Sun, 1 Dec 2019 23:27:52 +0900 Subject: [PATCH] Changed comments, minor edits --- .../hsh0908y/auto_wifi/SsidPwPick.java | 26 ++++++-- .../hsh0908y/auto_wifi/utils/Algorithm.java | 2 +- .../hsh0908y/auto_wifi/Evaluation.java | 62 ++++++++++++------- .../auto_wifi/SsidPwPickEvaluateTest.java | 8 ++- ...etect.java => TextDetectExperimental.java} | 2 +- 5 files changed, 68 insertions(+), 32 deletions(-) rename android/app/src/test/java/com/example/hsh0908y/auto_wifi/{TextDetect.java => TextDetectExperimental.java} (99%) diff --git a/android/app/src/main/java/com/example/hsh0908y/auto_wifi/SsidPwPick.java b/android/app/src/main/java/com/example/hsh0908y/auto_wifi/SsidPwPick.java index d9daee0..132c3ef 100644 --- a/android/app/src/main/java/com/example/hsh0908y/auto_wifi/SsidPwPick.java +++ b/android/app/src/main/java/com/example/hsh0908y/auto_wifi/SsidPwPick.java @@ -22,10 +22,15 @@ import static com.example.hsh0908y.auto_wifi.utils.TextBlockGeometry.isOrderY; import static com.example.hsh0908y.auto_wifi.utils.TextBlockGeometry.rotationNormalize; +// This class was initially designed to return untried SSID and Password whenever the extract method +// is called. However, we changed the design of the application to try extraction and connection only +// once. So our evaluation metrics is only for the first result that the extract method of this class +// returns. + public class SsidPwPick { private final String[] SSID_TAGS = {"아이디", "id"}; - private final String[] PW_TAGS = {"pw", "password", "비밀번호", "비번", "ps", "패스워드", "p/w", "p.w", "pu"}; + private final String[] PW_TAGS = {"pw", "password", "비밀번호", "비번", "ps", "패스워드", "p/w", "p.w"}; private final String[] WIFI_TAGS = {"wifi", "wi-fi", "와이파이", "와이-파이"}; private final int NUM_TRY_PW_PER_COMPONENT = 2; @@ -38,6 +43,9 @@ public class SsidPwPick { private final TextBlockGraphComponent ssidTagComponent; private final List pwCandidateListFromTag; + // Single SsidPwPick object can deal only with single text detection result (textBlockList). + + // Constructor of SsidPw for extractSsidPw public SsidPwPick(List textBlockList, List wifiDataList) { this.textBlockList = textBlockList; rotationNormalize(this.textBlockList); @@ -51,6 +59,7 @@ public SsidPwPick(List textBlockList, List wifiDataList) { this.wifiDataList = wifiDataList; } + // Constructor of SsidPw for extractPw public SsidPwPick(List textBlockList) { this.textBlockList = textBlockList; rotationNormalize(this.textBlockList); @@ -59,11 +68,13 @@ public SsidPwPick(List textBlockList) { ssidTriedList = new ArrayList<>(); pwCandidateQueue = new LinkedList<>(); - ssidTagComponent = getSsidTagComponent(); + ssidTagComponent = null; pwCandidateListFromTag = getPwCandidateListFromTag(true); this.wifiDataList = null; } + // Can be called multiple times and returns untried pair of SSID, Password each time it is called. + // When all of the candidates are returned, public SsidPw extractSsidPw() { if (wifiDataList == null || wifiDataList.isEmpty() || textBlockGraphComponentList.isEmpty()) { @@ -80,6 +91,9 @@ public SsidPw extractSsidPw() { int maximumScore = 0; for (WifiData wifiData : wifiDataList) { for (TextBlockGraphComponent component : textBlockGraphComponentList) { + // Not reflected in current implementation, but ComputeScoreSsid() can penaltize the + // component if it exist in ssidTriedList, in order to avoid same SSID to be chosen + // whenever this method is called. int score = ComputeScoreSsid(wifiData, component); if (score > maximumScore) { maximumScore = score; @@ -181,6 +195,10 @@ private List getPwCandidateListFromTag(boolean regardWifiTag) { return new ArrayList<>(); } + // There could be cases where PW Tag component is not found - Example : Word PW is detected + // as other letters. + // For this case, we try to infer from the position information of SSID Tag componet, if it + // exists. int selectedIndex = -1; if (ssidTagComponent != null) { float minDistance = Float.MAX_VALUE; @@ -260,10 +278,6 @@ private List buildTextBlockGraphComponentList() { textBlockGraphComponentList.add(new TextBlockGraphComponent(textBlockGraphComponent)); } -// for (TextBlockGraphComponent c : textBlockGraphComponentList) { -//// System.out.println(c.getConcatenatedDescription()); -//// } - return textBlockGraphComponentList; } diff --git a/android/app/src/main/java/com/example/hsh0908y/auto_wifi/utils/Algorithm.java b/android/app/src/main/java/com/example/hsh0908y/auto_wifi/utils/Algorithm.java index c508921..46f2bc5 100644 --- a/android/app/src/main/java/com/example/hsh0908y/auto_wifi/utils/Algorithm.java +++ b/android/app/src/main/java/com/example/hsh0908y/auto_wifi/utils/Algorithm.java @@ -84,7 +84,7 @@ public static int getLengthOfLongestCommonSubstring(String s1, String s2) { return maximum; } - public static int getEditDistance(String s1, String s2) { + public static int getCustomizedEditDistance(String s1, String s2) { int l1 = s1.length(); int l2 = s2.length(); int[][] dp = new int[l1 + 1][l2 + 1]; diff --git a/android/app/src/test/java/com/example/hsh0908y/auto_wifi/Evaluation.java b/android/app/src/test/java/com/example/hsh0908y/auto_wifi/Evaluation.java index 62dc864..0cae58b 100644 --- a/android/app/src/test/java/com/example/hsh0908y/auto_wifi/Evaluation.java +++ b/android/app/src/test/java/com/example/hsh0908y/auto_wifi/Evaluation.java @@ -65,21 +65,7 @@ public boolean LoadGroundTruth(String csvPath) { } public float evaluateAll(String saveDir, int wifiListSize, int numConfusingWifis) { - File dir = new File(saveDir); - File[] files = dir.listFiles(); - List fileList = new ArrayList<>(); - for (File file : dir.listFiles()) { - String fileName = file.getName(); - if (fileName.endsWith(".save")) { - fileList.add(file); - } - } - fileList.sort(new Comparator() { - @Override - public int compare(File file1, File file2) { - return getIntPrefix(file1.getName()) - getIntPrefix(file2.getName()); - } - }); + List fileList = getSortedSavedFiles(saveDir); float scoreSum = 0f; int count = 0; @@ -88,8 +74,8 @@ public int compare(File file1, File file2) { String groundTruthSsid = groundTruthSsidMap.get(prefix); String groundTruthPw = groundTruthPwMap.get(prefix); if (groundTruthSsid != null || groundTruthPw != null) { - System.out.printf("[ %s ]\n", file.getName()); - List textBlockList = TextDetect.getTextBlockListFromSaved(file.getPath()); + // System.out.printf("[ %s ]\n", file.getName()); + List textBlockList = TextDetectExperimental.getTextBlockListFromSaved(file.getPath()); scoreSum += evaluate( textBlockList, groundTruthSsid, groundTruthPw, wifiListSize, numConfusingWifis); @@ -99,6 +85,33 @@ public int compare(File file1, File file2) { return scoreSum / count; } + static void printJsonAll(String saveDir) { + List fileList = getSortedSavedFiles(saveDir); + for (File file : fileList) { + System.out.printf("[ %s ]\n", file.getName()); + System.out.println(TextDetectExperimental.getJsonFromSaved(file.getPath())); + } + } + + private static List getSortedSavedFiles(String saveDir) { + File dir = new File(saveDir); + File[] files = dir.listFiles(); + List fileList = new ArrayList<>(); + for (File file : dir.listFiles()) { + String fileName = file.getName(); + if (fileName.endsWith(".save")) { + fileList.add(file); + } + } + fileList.sort(new Comparator() { + @Override + public int compare(File file1, File file2) { + return getIntPrefix(file1.getName()) - getIntPrefix(file2.getName()); + } + }); + return fileList; + } + public float evaluate(String savePath, int wifiListSize, int numConfusingWifis) { int prefix = getIntPrefix(Paths.get(savePath).getFileName().toString()); @@ -106,7 +119,8 @@ public float evaluate(String savePath, String groundTruthPw = groundTruthPwMap.get(prefix); if (groundTruthSsid != null || groundTruthPw != null) { - List textBlockList = TextDetect.getTextBlockListFromSaved(savePath); + List textBlockList = TextDetectExperimental.getTextBlockListFromSaved(savePath); + return evaluate(textBlockList, groundTruthSsid, groundTruthPw, wifiListSize, numConfusingWifis); } @@ -116,6 +130,10 @@ public float evaluate(String savePath, private float evaluate(List textBlockList, String groundTruthSsid, String groundTruthPw, int wifiListSize, int numConfusingWifis) { +// for (TextBlock b : textBlockList) { +// System.out.print(" {" + b.getDescription() + "} "); +// } + if (groundTruthSsid != null) { return evaluateSsidPw(wifiListSize, numConfusingWifis, groundTruthSsid, groundTruthPw, textBlockList); @@ -161,8 +179,8 @@ private float evaluateSsidPw( score = ssidScore * 0.5f + getStringScore(ssidPw.pw, groundTruthPw) * 0.5f; } - System.out.printf("%s / %s\n%s / %s\n\n", - groundTruthSsid, ssidPw.ssid, groundTruthPw, ssidPw.pw); +// System.out.printf("%s / %s\n%s / %s\n\n", +// groundTruthSsid, ssidPw.ssid, groundTruthPw, ssidPw.pw); return Math.min(1f, Math.max(0f, score)); } @@ -172,7 +190,7 @@ private float evaluatePw(String groundTruthPw, List textBlockList) { String pw = pickTask.extractPw(); float score = getStringScore(pw, groundTruthPw); - System.out.printf("%s / %s\n\n", groundTruthPw, pw); + // System.out.printf("%s / %s\n\n", groundTruthPw, pw); return Math.min(1f, Math.max(0f, score)); } @@ -185,7 +203,7 @@ private static float getStringScore(String answer, String groundTruth) { return 1.0f; } return 0.5f * (1 - - (float) Algorithm.getEditDistance(groundTruth, answer) / groundTruth.length()); + (float) Algorithm.getCustomizedEditDistance(groundTruth, answer) / groundTruth.length()); } private static String generateRandomString(int length) { diff --git a/android/app/src/test/java/com/example/hsh0908y/auto_wifi/SsidPwPickEvaluateTest.java b/android/app/src/test/java/com/example/hsh0908y/auto_wifi/SsidPwPickEvaluateTest.java index 150f62f..186a67d 100644 --- a/android/app/src/test/java/com/example/hsh0908y/auto_wifi/SsidPwPickEvaluateTest.java +++ b/android/app/src/test/java/com/example/hsh0908y/auto_wifi/SsidPwPickEvaluateTest.java @@ -11,10 +11,14 @@ */ public class SsidPwPickEvaluateTest { + // NOTE: Since we didn't upload the sampledata to the Version control system, + // this test can't be run if you have cloned the repository from github. + + static final String testDataDir = Paths.get( + System.getProperty("user.dir"), "sampledata").toString(); + @Test public void printEvaluation() { - String testDataDir = - Paths.get(System.getProperty("user.dir"), "sampledata").toString(); String saveDir = Paths.get(testDataDir, "output-test").toString(); Evaluation evaluation = new Evaluation(); evaluation.LoadGroundTruth(Paths.get(testDataDir, "ground_truth.csv").toString()); diff --git a/android/app/src/test/java/com/example/hsh0908y/auto_wifi/TextDetect.java b/android/app/src/test/java/com/example/hsh0908y/auto_wifi/TextDetectExperimental.java similarity index 99% rename from android/app/src/test/java/com/example/hsh0908y/auto_wifi/TextDetect.java rename to android/app/src/test/java/com/example/hsh0908y/auto_wifi/TextDetectExperimental.java index 210ac7f..dff83fa 100644 --- a/android/app/src/test/java/com/example/hsh0908y/auto_wifi/TextDetect.java +++ b/android/app/src/test/java/com/example/hsh0908y/auto_wifi/TextDetectExperimental.java @@ -15,7 +15,7 @@ import java.util.ArrayList; import java.util.List; -class TextDetect { +class TextDetectExperimental { static List getTextBlockListFromSaved(String savePath) { try {