辞書を片手に~PatternResponderの作成(5)
それでは、
・loadDictionary()とloadDictionaryResponse()の重複処理の解消
をやってみる。
まずは、
public static Pattern[] loadDictionary(FileReader reader) {
String[] patternStrings = RandomResponder.loadDictionary(reader);
Pattern[] patterns = new Pattern[patternStrings.length];
for (int index=0;index < patterns.length; index++) {
patterns[index] = Pattern.compile(patternStrings[index].split("\t")[0]);
}
return patterns;
}
下線部分を最初に取得してしまうように変更する。
public static Pattern[] loadDictionary(FileReader reader) {
String[] loadStrings = RandomResponder.loadDictionary(reader);
String[] patternStrings = new String[loadStrings.length];
for (int index=0;index < loadStrings.length; index++) {
patternStrings[index] = loadStrings[index].split("\t")[0];
}
Pattern[] patterns = new Pattern[patternStrings.length];
for (int index=0;index < patterns.length; index++) {
patterns[index] = Pattern.compile(patternStrings[index]);
}
return patterns;
}
テスト実行。OK、壊していない。
loadDictionaryResponse()の方も、使用しているローカル変数をloadDictionary()に合わせる。
public static String[] loadDictionaryResponse(FileReader reader) {
String[] loadStrings = RandomResponder.loadDictionary(reader);
String[] responses = new String[loadStrings.length];
for (int index=0;index < loadStrings.length; index++) {
responses[index] = loadStrings[index].split("\t")[1];
}
return responses;
}
テスト実行してOK。ほとんど一緒にはなったが共通メソッドに抜き出すにはまだ微妙である。
試しに、Eclipseのメソッド抽出をかけてみる。
public static String[] loadDictionaryResponse(FileReader reader) {
String[] loadStrings = RandomResponder.loadDictionary(reader);
String[] responses = splitPatternAndResponse(loadStrings);
return responses;
}
/**
* @param loadStrings
* @return
*/
private static String[] splitPatternAndResponse(String[] loadStrings) {
String[] responses = new String[loadStrings.length];
for (int index=0;index < loadStrings.length; index++) {
responses[index] = loadStrings[index].split("\t")[1];
}
return responses;
}
split("\t")[1]の1を引数にするとうまく共通化できそうだ。
public static Pattern[] loadDictionary(FileReader reader) {
String[] loadStrings = RandomResponder.loadDictionary(reader);
String[] patternStrings = splitPatternAndResponse(loadStrings, 0);
Pattern[] patterns = new Pattern[patternStrings.length];
for (int index=0;index < patterns.length; index++) {
patterns[index] = Pattern.compile(patternStrings[index]);
}
return patterns;
}
public static String[] loadDictionaryResponse(FileReader reader) {
String[] loadStrings = RandomResponder.loadDictionary(reader);
String[] responses = splitPatternAndResponse(loadStrings, 1);
return responses;
}
/**
* @param loadStrings
* @param num TODO
* @return
*/
private static String[] splitPatternAndResponse(String[] loadStrings, int num) {
String[] responses = new String[loadStrings.length];
for (int index=0;index < loadStrings.length; index++) {
responses[index] = loadStrings[index].split("\t")[num];
}
return responses;
}
いちいち書いてはいないが、1ステップずつテストでGreenを確認している。
最後に、いくつかのローカル変数をインライン化する。
public static Pattern[] loadDictionary(FileReader reader) {
String[] patternStrings = splitPatternAndResponse(RandomResponder.loadDictionary(reader), 0);
Pattern[] patterns = new Pattern[patternStrings.length];
for (int index=0;index < patterns.length; index++) {
patterns[index] = Pattern.compile(patternStrings[index]);
}
return patterns;
}
public static String[] loadDictionaryResponse(FileReader reader) {
return splitPatternAndResponse(RandomResponder.loadDictionary(reader), 1);
}
/**
* @param loadStrings
* @param num TODO
* @return
*/
private static String[] splitPatternAndResponse(String[] loadStrings, int num) {
String[] responses = new String[loadStrings.length];
for (int index=0;index < loadStrings.length; index++) {
responses[index] = loadStrings[index].split("\t")[num];
}
return responses;
}
RandomResponder.loadDictionary(reader)を2回呼び出しいるところが気になるが、これはsplitPatternAndResponse()の中に移しても2回呼ぶことには変わり無いので、次のToDoを追加しておいておく。
ToDoリスト
・loadDictionary()とloadDictionaryResponse()の重複処理の解消
・loadDictionary()とloadDictionaryResponse()で、同じファイル読み込みを繰り返している(これはファイル読み込みの結果を保持しないと解決できない?)
・loadDictionary()とloadDictionaryResponse()の重複処理の解消
をやってみる。
まずは、
public static Pattern[] loadDictionary(FileReader reader) {
String[] patternStrings = RandomResponder.loadDictionary(reader);
Pattern[] patterns = new Pattern[patternStrings.length];
for (int index=0;index < patterns.length; index++) {
patterns[index] = Pattern.compile(patternStrings[index].split("\t")[0]);
}
return patterns;
}
下線部分を最初に取得してしまうように変更する。
public static Pattern[] loadDictionary(FileReader reader) {
String[] loadStrings = RandomResponder.loadDictionary(reader);
String[] patternStrings = new String[loadStrings.length];
for (int index=0;index < loadStrings.length; index++) {
patternStrings[index] = loadStrings[index].split("\t")[0];
}
Pattern[] patterns = new Pattern[patternStrings.length];
for (int index=0;index < patterns.length; index++) {
patterns[index] = Pattern.compile(patternStrings[index]);
}
return patterns;
}
テスト実行。OK、壊していない。
loadDictionaryResponse()の方も、使用しているローカル変数をloadDictionary()に合わせる。
public static String[] loadDictionaryResponse(FileReader reader) {
String[] loadStrings = RandomResponder.loadDictionary(reader);
String[] responses = new String[loadStrings.length];
for (int index=0;index < loadStrings.length; index++) {
responses[index] = loadStrings[index].split("\t")[1];
}
return responses;
}
テスト実行してOK。ほとんど一緒にはなったが共通メソッドに抜き出すにはまだ微妙である。
試しに、Eclipseのメソッド抽出をかけてみる。
public static String[] loadDictionaryResponse(FileReader reader) {
String[] loadStrings = RandomResponder.loadDictionary(reader);
String[] responses = splitPatternAndResponse(loadStrings);
return responses;
}
/**
* @param loadStrings
* @return
*/
private static String[] splitPatternAndResponse(String[] loadStrings) {
String[] responses = new String[loadStrings.length];
for (int index=0;index < loadStrings.length; index++) {
responses[index] = loadStrings[index].split("\t")[1];
}
return responses;
}
split("\t")[1]の1を引数にするとうまく共通化できそうだ。
public static Pattern[] loadDictionary(FileReader reader) {
String[] loadStrings = RandomResponder.loadDictionary(reader);
String[] patternStrings = splitPatternAndResponse(loadStrings, 0);
Pattern[] patterns = new Pattern[patternStrings.length];
for (int index=0;index < patterns.length; index++) {
patterns[index] = Pattern.compile(patternStrings[index]);
}
return patterns;
}
public static String[] loadDictionaryResponse(FileReader reader) {
String[] loadStrings = RandomResponder.loadDictionary(reader);
String[] responses = splitPatternAndResponse(loadStrings, 1);
return responses;
}
/**
* @param loadStrings
* @param num TODO
* @return
*/
private static String[] splitPatternAndResponse(String[] loadStrings, int num) {
String[] responses = new String[loadStrings.length];
for (int index=0;index < loadStrings.length; index++) {
responses[index] = loadStrings[index].split("\t")[num];
}
return responses;
}
いちいち書いてはいないが、1ステップずつテストでGreenを確認している。
最後に、いくつかのローカル変数をインライン化する。
public static Pattern[] loadDictionary(FileReader reader) {
String[] patternStrings = splitPatternAndResponse(RandomResponder.loadDictionary(reader), 0);
Pattern[] patterns = new Pattern[patternStrings.length];
for (int index=0;index < patterns.length; index++) {
patterns[index] = Pattern.compile(patternStrings[index]);
}
return patterns;
}
public static String[] loadDictionaryResponse(FileReader reader) {
return splitPatternAndResponse(RandomResponder.loadDictionary(reader), 1);
}
/**
* @param loadStrings
* @param num TODO
* @return
*/
private static String[] splitPatternAndResponse(String[] loadStrings, int num) {
String[] responses = new String[loadStrings.length];
for (int index=0;index < loadStrings.length; index++) {
responses[index] = loadStrings[index].split("\t")[num];
}
return responses;
}
RandomResponder.loadDictionary(reader)を2回呼び出しいるところが気になるが、これはsplitPatternAndResponse()の中に移しても2回呼ぶことには変わり無いので、次のToDoを追加しておいておく。
ToDoリスト
・loadDictionary()とloadDictionaryResponse()の重複処理の解消
・loadDictionary()とloadDictionaryResponse()で、同じファイル読み込みを繰り返している(これはファイル読み込みの結果を保持しないと解決できない?)