make unicode string print as characters

This commit is contained in:
wyq 2025-02-07 16:47:31 +08:00
parent 137db5026f
commit 27ff2934b2
5 changed files with 157 additions and 15 deletions

View File

@ -43,6 +43,8 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;

View File

@ -652,6 +652,10 @@ public class JConsole extends JScrollPane
} }
private void append(String string) { private void append(String string) {
if (string.length() > 10000) {
string = string.substring(0, 10000);
}
string = StringUtil.unicodeToString(string);
int slen = textLength(); int slen = textLength();
if (slen > this.maxLength) { if (slen > this.maxLength) {
text.setText(""); text.setText("");

View File

@ -29,6 +29,8 @@ package org.meteoinfo.console;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.Vector; import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtil { public class StringUtil {
@ -91,6 +93,26 @@ public class StringUtil {
return sb.toString(); return sb.toString();
} }
/**
* Convert unicode string to character string
*
* @param unicodeString Unicode string
* @return Character string
*/
public static String unicodeToString(String unicodeString) {
Pattern pattern = Pattern.compile("\\\\u[0-9a-fA-F]{4}");
Matcher matcher = pattern.matcher(unicodeString);
StringBuffer builder = new StringBuffer();
while (matcher.find()) {
String unicodeSequence = matcher.group();
char unicode = (char) Integer.parseInt(unicodeSequence.substring(2), 16);
matcher.appendReplacement(builder, Character.toString(unicode));
}
matcher.appendTail(builder);
return builder.toString();
}
/** /**
* Split a filename into dirName, baseName * Split a filename into dirName, baseName
* *

View File

@ -1,11 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MeteoInfo File="milconfig.xml" Type="configurefile"> <MeteoInfo File="milconfig.xml" Type="configurefile">
<Path OpenPath="D:\Working\MIScript\Jython\mis\array"> <Path OpenPath="D:\Working\MIScript\Jython\mis\io\burf">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\dataframe"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\dataconvert"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\netcdf"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\micaps"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite\FY"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite\FY"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
@ -13,22 +8,23 @@
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\plot"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\plot"/>
<RecentFolder Folder="D:\Temp\test"/> <RecentFolder Folder="D:\Temp\test"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/> <RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\stats"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\dataframe"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\netcdf"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\burf"/>
</Path> </Path>
<File> <File>
<OpenedFiles> <OpenedFiles>
<OpenedFile File="D:\Working\MIScript\Jython\mis\io\radar\radar_x_phase_2.py"/> <OpenedFile File="D:\Working\MIScript\Jython\mis\io\radar\radar_x_phase_2.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\plot\plot3_multi_color_1.py"/> <OpenedFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_cma_rsd.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\array\concatenate.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\array\concatenate_2.py"/>
</OpenedFiles> </OpenedFiles>
<RecentFiles> <RecentFiles>
<RecentFile File="D:\Working\MIScript\Jython\mis\io\radar\radar_x_phase_2.py"/> <RecentFile File="D:\Working\MIScript\Jython\mis\io\radar\radar_x_phase_2.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\plot\plot3_multi_color_1.py"/> <RecentFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_cma_rsd.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\array\concatenate.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\array\concatenate_2.py"/>
</RecentFiles> </RecentFiles>
</File> </File>
<Font> <Font>
@ -36,5 +32,5 @@
</Font> </Font>
<LookFeel DockWindowDecorated="true" LafDecorated="true" Name="FlatDarkLaf"/> <LookFeel DockWindowDecorated="true" LafDecorated="true" Name="FlatDarkLaf"/>
<Figure DoubleBuffering="true"/> <Figure DoubleBuffering="true"/>
<Startup MainFormLocation="-6,0" MainFormSize="1352,827"/> <Startup MainFormLocation="-6,0" MainFormSize="1357,849"/>
</MeteoInfo> </MeteoInfo>

View File

@ -0,0 +1,118 @@
package org.meteoinfo.math.stats.regression;
import org.apache.commons.math4.legacy.stat.regression.OLSMultipleLinearRegression;
import java.util.ArrayList;
import java.util.List;
public class StepwiseRegression {
private static final double F_THRESHOLD = 4.0; // F test threshold, can be adjusted as needed
public static void main(String[] args) {
// sample data
double[][] xData = {
{1, 2, 3, 4},
{2, 3, 4, 5},
{3, 4, 5, 6},
{4, 5, 6, 7},
{5, 6, 7, 8}
};
double[] yData = {1, 2, 3, 4, 5};
// stepwise regression
StepwiseRegression stepwiseRegression = new StepwiseRegression();
List<Integer> selectedVariables = stepwiseRegression.stepwise(xData, yData);
System.out.println("Selected Variables: " + selectedVariables);
}
public List<Integer> stepwise(double[][] xData, double[] yData) {
List<Integer> variables = new ArrayList<>();
for (int i = 0; i < xData[0].length; i++) {
variables.add(i);
}
List<Integer> selectedVariables = new ArrayList<>();
boolean changed;
do {
changed = false;
// forward selection
double bestFValue = Double.NEGATIVE_INFINITY;
int bestVariable = -1;
for (int var : variables) {
if (!selectedVariables.contains(var)) {
List<Integer> tempVars = new ArrayList<>(selectedVariables);
tempVars.add(var);
double fValue = calculateFValue(xData, yData, tempVars);
if (fValue > bestFValue) {
bestFValue = fValue;
bestVariable = var;
}
}
}
if (bestFValue > F_THRESHOLD) {
selectedVariables.add(bestVariable);
changed = true;
}
// backward remove
bestFValue = Double.NEGATIVE_INFINITY;
bestVariable = -1;
for (int var : selectedVariables) {
List<Integer> tempVars = new ArrayList<>(selectedVariables);
tempVars.remove(Integer.valueOf(var));
double fValue = calculateFValue(xData, yData, tempVars);
if (fValue > bestFValue) {
bestFValue = fValue;
bestVariable = var;
}
}
if (bestFValue > F_THRESHOLD && selectedVariables.size() > 1) {
selectedVariables.remove(Integer.valueOf(bestVariable));
changed = true;
}
} while (changed);
return selectedVariables;
}
private double calculateFValue(double[][] xData, double[] yData, List<Integer> variables) {
int n = xData.length;
int k = variables.size();
double[][] xMatrix = new double[n][k + 1];
for (int i = 0; i < n; i++) {
xMatrix[i][0] = 1; // intercept
for (int j = 0; j < k; j++) {
xMatrix[i][j + 1] = xData[i][variables.get(j)];
}
}
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.newSampleData(yData, xMatrix);
double sse = regression.calculateResidualSumOfSquares();
double sst = calculateSST(yData);
double fValue = ((sst - sse) / k) / (sse / (n - k - 1));
return fValue;
}
private double calculateSST(double[] yData) {
double mean = 0;
for (double y : yData) {
mean += y;
}
mean /= yData.length;
double sst = 0;
for (double y : yData) {
sst += Math.pow(y - mean, 2);
}
return sst;
}
}