在Java中按字母排序而不是按字典排序
对于我正在进行的研究,我需要比较照片识别软件的效率。这些软件依次比较图片,确定它们是否包含相同的功能。因此,我需要软件以相同的方式对图片进行分类(1、2、3、10、100…),以便测试它们是否显示相同的结果 我希望软件能按顺序呈现照片1、照片2、照片10,而不是照片1、照片10、照片2 我使用的软件之一是按以下方式对图像名称进行排序: 1, 10, 100, 2, 3... 我试图更改Java源代码中的设置,但我不知道这个语言,尽管我在互联网上看到了这些,但我不知道要修改什么才能不出错 您可以在下面找到源代码 提前非常感谢您的帮助在Java中按字母排序而不是按字典排序,java,arrays,alphabetical,lexicographic,Java,Arrays,Alphabetical,Lexicographic,对于我正在进行的研究,我需要比较照片识别软件的效率。这些软件依次比较图片,确定它们是否包含相同的功能。因此,我需要软件以相同的方式对图片进行分类(1、2、3、10、100…),以便测试它们是否显示相同的结果 我希望软件能按顺序呈现照片1、照片2、照片10,而不是照片1、照片10、照片2 我使用的软件之一是按以下方式对图像名称进行排序: 1, 10, 100, 2, 3... 我试图更改Java源代码中的设置,但我不知道这个语言,尽管我在互联网上看到了这些,但我不知道要修改什么才能不出错 您可以在
public class SortResults {
private static class NameMap {
private final SortedMap<String, Integer> stringToInt = new TreeMap<String, Integer>();
private final List<String> intToString = new ArrayList<String>();
private short[] sortPositions = null;
public short getSortPosition(int index) {
if (sortPositions == null) {
List<Integer> indices = new ArrayList<Integer>(stringToInt
.values());
sortPositions = new short[indices.size()];
for (short i = 0; i < indices.size(); i++) {
sortPositions[indices.get(i)] = i;
}
}
return (short) (int) sortPositions[index];
}
public String lookup(int index) {
return intToString.get(index);
}
public short lookup(String name) {
Integer index = stringToInt.get(name);
if (index == null) {
index = intToString.size();
if (index > Short.MAX_VALUE) {
throw new RuntimeException("Too many names");
}
intToString.add(name);
stringToInt.put(name, index);
}
return index.shortValue();
}
}
private static final int NUM_SCORES = MatchResult.NUM_SCORES;
private static final int SCORE_SCALE_FACTOR = 10000;
public static String multiSort(final File unsortedFile) throws IOException {
final PathParts sortPath = new PathParts(unsortedFile);
final String basename = sortPath.getBasename();
final StringBuilder sortNames = new StringBuilder();
for (int i = 0; i < MatchResult.NUM_SCORES; i++) {
sortPath.setBasename(basename + "-" + MatchResult.SCORE_NAMES[i]);
sortNames.append(i == 0 ? "" : "\n").append(
" .../" + sortPath.getName());
final PrintStream sortStream = new PrintStream(sortPath.getFile());
sortByScore(unsortedFile, sortStream, i);
}
return sortNames.toString();
}
private static void sortByScore(final File unsortedFile,
final PrintStream sortStream, final int sortScoreIndex)
throws IOException {
int numDataLines = 0;
for (int pass = 0; pass < 2; pass++) {
final BufferedReader reader = new BufferedReader(new FileReader(
unsortedFile));
final List<String> headerLines = new ArrayList<String>();
reader.mark(1);
while (reader.read() == '#') {
reader.reset();
headerLines.add(reader.readLine());
reader.mark(1);
}
reader.reset();
if (headerLines.size() < 1) {
throw new RuntimeException("Scores file lacks header");
}
final String fieldLabelsLine = headerLines
.get(headerLines.size() - 1);
if (pass == 0) {
// First pass
if (!fieldLabelsLine.trim().equals(
MatchResult.getFieldLabelsLine())) {
throw new RuntimeException("Invalid field-labels line: "
+ fieldLabelsLine);
}
while (reader.readLine() != null) {
numDataLines++;
}
} else {
// Second pass (pass == 1)
// Copy header lines to sorted file
for (String headerLine : headerLines) {
sortStream.println(headerLine);
}
// Sort and write out data lines
sortFileSecondPassDataLines(numDataLines, reader, sortStream,
sortScoreIndex);
}
reader.close();
}
sortStream.close();
}
private static void sortFileSecondPassDataLines(final int numDataLines,
final BufferedReader reader, PrintStream sortStream,
final int sortScoreIndex) throws IOException {
// Read the data lines into a bunch of arrays.
final NameMap nameMap = new NameMap();
final short[][] nameIndices = new short[2][numDataLines];
final short[][] shortScores = new short[NUM_SCORES][numDataLines];
final String[] imageNames = new String[2];
final float[] scores = new float[NUM_SCORES];
for (int lineNum = 0; lineNum < numDataLines; lineNum++) {
final String line = reader.readLine();
MatchResult.parseLine(2 + lineNum, line, imageNames, scores);
nameIndices[0][lineNum] = nameMap.lookup(imageNames[0]);
nameIndices[1][lineNum] = nameMap.lookup(imageNames[1]);
for (int i = 0; i < scores.length; i++) {
shortScores[i][lineNum] = (short) Math.round(SCORE_SCALE_FACTOR
* scores[i]);
}
}
// Compute the line ordering based on alphabetic ordering of names.
// The line-order array entries have name-position indices in their
// upper four bytes, and line numbers in their lower four bytes.
final long lineAlphaOrder[] = new long[numDataLines];
for (int lineNum = 0; lineNum < lineAlphaOrder.length; lineNum++) {
long alphaPos0 = nameMap.getSortPosition(nameIndices[0][lineNum]);
long alphaPos1 = nameMap.getSortPosition(nameIndices[1][lineNum]);
lineAlphaOrder[lineNum] = (alphaPos0 << 48) | (alphaPos1 << 32)
| lineNum;
}
Arrays.sort(lineAlphaOrder);
// Compute output-file offsets for the different possible score values.
final int[] scoreCounts = new int[SCORE_SCALE_FACTOR + 1];
final int[] scoreOffsets = new int[SCORE_SCALE_FACTOR + 1];
Arrays.fill(scoreCounts, 0);
Arrays.fill(scoreOffsets, 0);
for (int i = 0; i < numDataLines; i++) {
++scoreCounts[shortScores[sortScoreIndex][i]];
}
int cumOffset = 0;
for (int i = SCORE_SCALE_FACTOR; i >= 0; i--) {
scoreOffsets[i] = cumOffset;
cumOffset += scoreCounts[i];
}
if (cumOffset != numDataLines) {
throw new RuntimeException("cumOffset = " + cumOffset
+ " but numDataLines = " + numDataLines);
}
// Compute ordering of original line numbers that results from sorting
// first by score, and then by alphabetical order of the names
final int[] sortOrderLineNums = new int[numDataLines];
Arrays.fill(sortOrderLineNums, -1);
for (long longLineNum : lineAlphaOrder) {
final int lineNum = (int) longLineNum;
final short scoreOfInterest = shortScores[sortScoreIndex][lineNum];
final int offset = scoreOffsets[scoreOfInterest]++;
if (sortOrderLineNums[offset] != -1) {
throw new RuntimeException("Corrupted sort array");
}
sortOrderLineNums[offset] = lineNum;
}
// Write out the data in sorted order.
for (int lineNum : sortOrderLineNums) {
imageNames[0] = nameMap.lookup(nameIndices[0][lineNum]);
imageNames[1] = nameMap.lookup(nameIndices[1][lineNum]);
for (int i = 0; i < scores.length; i++) {
scores[i] = shortScores[i][lineNum]
/ (float) SCORE_SCALE_FACTOR;
}
MatchResult.printLine(sortStream, imageNames, scores);
}
}
}
公共类排序结果{
私有静态类名称映射{
私有最终分类映射stringToInt=新树映射();
私有最终列表intToString=new ArrayList();
private short[]sortPositions=null;
公共短getSortPosition(int索引){
if(sortPositions==null){
列表索引=新的ArrayList(stringToInt
.values());
sortPositions=新的短[index.size()];
对于(短i=0;i短最大值){
抛出新的RuntimeException(“名称太多”);
}
intToString.add(名称);
stringToInt.put(名称、索引);
}
返回index.shortValue();
}
}
私有静态最终int NUM_分数=MatchResult.NUM_分数;
私人静态最终整数分数\量表\因子=10000;
公共静态字符串multiSort(最终文件unsortedFile)引发IOException{
最终路径部件排序路径=新路径部件(未排序文件);
最后一个字符串basename=sortPath.getBasename();
最终StringBuilder sortNames=新StringBuilder();
对于(int i=0;i