@Test public void shouldExtractKey(){ List<String> chords = new ArrayList<>(); chords.add("D4MIN7"); chords.add("D4MIN"); chords.add("EMIN"); chords.add("DbMIN7"); String actual = keyExtractor.extract(chords); String expected = "c"; assertThat(actual).isEqualToIgnoringCase(expected); }
public String extract(List<String> chords){ List<String> potentialKeys = Arrays.asList("a","a#","b", "c","c#", "d","d#", "e", "f","f#", "g", "g#"); Map<String, Integer> keyScore = new HashMap<>(); for (String key : potentialKeys) { keyScore.put(key, 0); } for(String key: potentialKeys){ List<HiveChord> chordsInKey = getChordsInKey(key); for(String chord: chords){ HiveChord hiveChord = new HiveChord(chord); for (HiveChord hChordInKey : chordsInKey) { if(hiveChord.getChordName().equalsIgnoreCase(hChordInKey.getChordName())){ keyScore.put(key, keyScore.get(key)+1); } } } } System.out.println(keyScore); String keyWithHighestScore = ""; int currentScore = -1; for(String key: potentialKeys){ if(keyScore.get(key) > currentScore){ keyWithHighestScore = key; currentScore = keyScore.get(key); } } List<String> highestScores = new ArrayList<>(); for(String key: potentialKeys){ if(keyScore.get(key) == currentScore){ highestScores.add(key); } } if(highestScores.size() == 1){ return highestScores.get(0); } for (String key : highestScores) { if(chords.get(0).equalsIgnoreCase(key+"maj") || chords.get(0).equalsIgnoreCase(key + "maj7")){ return key; } } return keyWithHighestScore; }
KeyExtractor { public String extract(List<String> chords){ List<String> potentialKeys = Arrays.asList("a","a#","b", "c","c#", "d","d#", "e", "f","f#", "g", "g#"); Map<String, Integer> keyScore = new HashMap<>(); for (String key : potentialKeys) { keyScore.put(key, 0); } for(String key: potentialKeys){ List<HiveChord> chordsInKey = getChordsInKey(key); for(String chord: chords){ HiveChord hiveChord = new HiveChord(chord); for (HiveChord hChordInKey : chordsInKey) { if(hiveChord.getChordName().equalsIgnoreCase(hChordInKey.getChordName())){ keyScore.put(key, keyScore.get(key)+1); } } } } System.out.println(keyScore); String keyWithHighestScore = ""; int currentScore = -1; for(String key: potentialKeys){ if(keyScore.get(key) > currentScore){ keyWithHighestScore = key; currentScore = keyScore.get(key); } } List<String> highestScores = new ArrayList<>(); for(String key: potentialKeys){ if(keyScore.get(key) == currentScore){ highestScores.add(key); } } if(highestScores.size() == 1){ return highestScores.get(0); } for (String key : highestScores) { if(chords.get(0).equalsIgnoreCase(key+"maj") || chords.get(0).equalsIgnoreCase(key + "maj7")){ return key; } } return keyWithHighestScore; } }
KeyExtractor { public String extract(List<String> chords){ List<String> potentialKeys = Arrays.asList("a","a#","b", "c","c#", "d","d#", "e", "f","f#", "g", "g#"); Map<String, Integer> keyScore = new HashMap<>(); for (String key : potentialKeys) { keyScore.put(key, 0); } for(String key: potentialKeys){ List<HiveChord> chordsInKey = getChordsInKey(key); for(String chord: chords){ HiveChord hiveChord = new HiveChord(chord); for (HiveChord hChordInKey : chordsInKey) { if(hiveChord.getChordName().equalsIgnoreCase(hChordInKey.getChordName())){ keyScore.put(key, keyScore.get(key)+1); } } } } System.out.println(keyScore); String keyWithHighestScore = ""; int currentScore = -1; for(String key: potentialKeys){ if(keyScore.get(key) > currentScore){ keyWithHighestScore = key; currentScore = keyScore.get(key); } } List<String> highestScores = new ArrayList<>(); for(String key: potentialKeys){ if(keyScore.get(key) == currentScore){ highestScores.add(key); } } if(highestScores.size() == 1){ return highestScores.get(0); } for (String key : highestScores) { if(chords.get(0).equalsIgnoreCase(key+"maj") || chords.get(0).equalsIgnoreCase(key + "maj7")){ return key; } } return keyWithHighestScore; }  }
KeyExtractor { public String extract(List<String> chords){ List<String> potentialKeys = Arrays.asList("a","a#","b", "c","c#", "d","d#", "e", "f","f#", "g", "g#"); Map<String, Integer> keyScore = new HashMap<>(); for (String key : potentialKeys) { keyScore.put(key, 0); } for(String key: potentialKeys){ List<HiveChord> chordsInKey = getChordsInKey(key); for(String chord: chords){ HiveChord hiveChord = new HiveChord(chord); for (HiveChord hChordInKey : chordsInKey) { if(hiveChord.getChordName().equalsIgnoreCase(hChordInKey.getChordName())){ keyScore.put(key, keyScore.get(key)+1); } } } } System.out.println(keyScore); String keyWithHighestScore = ""; int currentScore = -1; for(String key: potentialKeys){ if(keyScore.get(key) > currentScore){ keyWithHighestScore = key; currentScore = keyScore.get(key); } } List<String> highestScores = new ArrayList<>(); for(String key: potentialKeys){ if(keyScore.get(key) == currentScore){ highestScores.add(key); } } if(highestScores.size() == 1){ return highestScores.get(0); } for (String key : highestScores) { if(chords.get(0).equalsIgnoreCase(key+"maj") || chords.get(0).equalsIgnoreCase(key + "maj7")){ return key; } } return keyWithHighestScore; }  String extract(List<String> chords); List<String> getNotesInKey(String key); List<HiveChord> getChordsInKey(String key); }
KeyExtractor { public String extract(List<String> chords){ List<String> potentialKeys = Arrays.asList("a","a#","b", "c","c#", "d","d#", "e", "f","f#", "g", "g#"); Map<String, Integer> keyScore = new HashMap<>(); for (String key : potentialKeys) { keyScore.put(key, 0); } for(String key: potentialKeys){ List<HiveChord> chordsInKey = getChordsInKey(key); for(String chord: chords){ HiveChord hiveChord = new HiveChord(chord); for (HiveChord hChordInKey : chordsInKey) { if(hiveChord.getChordName().equalsIgnoreCase(hChordInKey.getChordName())){ keyScore.put(key, keyScore.get(key)+1); } } } } System.out.println(keyScore); String keyWithHighestScore = ""; int currentScore = -1; for(String key: potentialKeys){ if(keyScore.get(key) > currentScore){ keyWithHighestScore = key; currentScore = keyScore.get(key); } } List<String> highestScores = new ArrayList<>(); for(String key: potentialKeys){ if(keyScore.get(key) == currentScore){ highestScores.add(key); } } if(highestScores.size() == 1){ return highestScores.get(0); } for (String key : highestScores) { if(chords.get(0).equalsIgnoreCase(key+"maj") || chords.get(0).equalsIgnoreCase(key + "maj7")){ return key; } } return keyWithHighestScore; }  String extract(List<String> chords); List<String> getNotesInKey(String key); List<HiveChord> getChordsInKey(String key);  }