Skip to content

Commit

Permalink
Changed comments, minor edits
Browse files Browse the repository at this point in the history
  • Loading branch information
tink-expo committed Dec 1, 2019
1 parent 17cb36e commit ec7f2d4
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,6 +43,9 @@ public class SsidPwPick {
private final TextBlockGraphComponent ssidTagComponent;
private final List<String> pwCandidateListFromTag;

// Single SsidPwPick object can deal only with single text detection result (textBlockList).

// Constructor of SsidPw for extractSsidPw
public SsidPwPick(List<TextBlock> textBlockList, List<WifiData> wifiDataList) {
this.textBlockList = textBlockList;
rotationNormalize(this.textBlockList);
Expand All @@ -51,6 +59,7 @@ public SsidPwPick(List<TextBlock> textBlockList, List<WifiData> wifiDataList) {
this.wifiDataList = wifiDataList;
}

// Constructor of SsidPw for extractPw
public SsidPwPick(List<TextBlock> textBlockList) {
this.textBlockList = textBlockList;
rotationNormalize(this.textBlockList);
Expand All @@ -59,11 +68,13 @@ public SsidPwPick(List<TextBlock> 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()) {
Expand All @@ -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;
Expand Down Expand Up @@ -181,6 +195,10 @@ private List<String> 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;
Expand Down Expand Up @@ -260,10 +278,6 @@ private List<TextBlockGraphComponent> buildTextBlockGraphComponentList() {
textBlockGraphComponentList.add(new TextBlockGraphComponent(textBlockGraphComponent));
}

// for (TextBlockGraphComponent c : textBlockGraphComponentList) {
//// System.out.println(c.getConcatenatedDescription());
//// }

return textBlockGraphComponentList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> fileList = new ArrayList<>();
for (File file : dir.listFiles()) {
String fileName = file.getName();
if (fileName.endsWith(".save")) {
fileList.add(file);
}
}
fileList.sort(new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
return getIntPrefix(file1.getName()) - getIntPrefix(file2.getName());
}
});
List<File> fileList = getSortedSavedFiles(saveDir);

float scoreSum = 0f;
int count = 0;
Expand All @@ -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<TextBlock> textBlockList = TextDetect.getTextBlockListFromSaved(file.getPath());
// System.out.printf("[ %s ]\n", file.getName());
List<TextBlock> textBlockList = TextDetectExperimental.getTextBlockListFromSaved(file.getPath());
scoreSum += evaluate(
textBlockList, groundTruthSsid, groundTruthPw,
wifiListSize, numConfusingWifis);
Expand All @@ -99,14 +85,42 @@ public int compare(File file1, File file2) {
return scoreSum / count;
}

static void printJsonAll(String saveDir) {
List<File> fileList = getSortedSavedFiles(saveDir);
for (File file : fileList) {
System.out.printf("[ %s ]\n", file.getName());
System.out.println(TextDetectExperimental.getJsonFromSaved(file.getPath()));
}
}

private static List<File> getSortedSavedFiles(String saveDir) {
File dir = new File(saveDir);
File[] files = dir.listFiles();
List<File> fileList = new ArrayList<>();
for (File file : dir.listFiles()) {
String fileName = file.getName();
if (fileName.endsWith(".save")) {
fileList.add(file);
}
}
fileList.sort(new Comparator<File>() {
@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());
String groundTruthSsid = groundTruthSsidMap.get(prefix);
String groundTruthPw = groundTruthPwMap.get(prefix);

if (groundTruthSsid != null || groundTruthPw != null) {
List<TextBlock> textBlockList = TextDetect.getTextBlockListFromSaved(savePath);
List<TextBlock> textBlockList = TextDetectExperimental.getTextBlockListFromSaved(savePath);

return evaluate(textBlockList, groundTruthSsid, groundTruthPw,
wifiListSize, numConfusingWifis);
}
Expand All @@ -116,6 +130,10 @@ public float evaluate(String savePath,
private float evaluate(List<TextBlock> 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);
Expand Down Expand Up @@ -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));
}
Expand All @@ -172,7 +190,7 @@ private float evaluatePw(String groundTruthPw, List<TextBlock> 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));
}
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.ArrayList;
import java.util.List;

class TextDetect {
class TextDetectExperimental {

static List<TextBlock> getTextBlockListFromSaved(String savePath) {
try {
Expand Down

0 comments on commit ec7f2d4

Please sign in to comment.