我想知道是否有适用于Java的lib,其实现包括:isTemperature()
,isPercentual()
,isDistanceUnit()
,isWeightUnit()
,isProperName()
,{{ 1}},isDate()
,isYear()
,isPhone()
还可以定义更多内容。
不仅单位符合我的兴趣,而且所有可能的分类都是数字或单词。
这将用于对文本中的单词进行分类。
答案 0 :(得分:6)
不是我知道的。但是,你仍然可以自己创建做同样事情的方法......
isTemperature()
boolean isTemperature(String check) {
char[] letters = check.toCharArray();
if (check.indexOf("°").equals((letters.length - 1)) {
return true;
}
return false;
}
isPercentual()
boolean isPercentual(String check) {
try {
double verify = Double.parseDouble(check);
} catch (NumberFormatException e) {
return false;
}
char[] numbers = check.toCharArray();
if (check.indexOf("%").equals(numbers.length) {
return true;
}
return false;
}
isDistanceUnit()
boolean isDistanceUnit(String check, boolean customary) {
String[] customaryUnits = {"mi","yd","ft","in"};
String[] metricUnits = {"mm","cm","dm","m","km"};
if (customary) {
for (int i = 0; i <= customaryUnits.length; i++) {
if (check.toLowerCase().contains(customaryUnits[i])) {
return true;
}
}
return false;
} else {
for (int i = 0; i <= metricUnits.length; i++) {
if (check.toLowerCase().contains(metricUnits[i])) {
return true;
}
}
return false;
}
}
isWeightUnit()
boolean isWeightUnit(String check, boolean customary) {
String[] customaryUnits = {"lb","oz","T"};
String[] metricUnits = {"kg"}; //I'm sorry that's all I know :(
if (customary) {
for (int i = 0; i <= customaryUnits.length; i++) {
if (check.toLowerCase().contains(customaryUnits[i])) {
return true;
}
}
return false;
} else {
for (int i = 0; i <= metricUnits.length; i++) {
if (check.toLowerCase().contains(metricUnits[i])) {
return true;
}
}
return false;
}
}
isProperName()
boolean isProperName(String check) {
char[] letters = check.toCharArray();
String[] capLetters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
for (int i = 0; i <= capLetters.length; i++) {
if (letters[0].equals(capLetters[i])) {
return true;
}
}
return false;
}
isDate()
更新:现在我可以给你这个了。不过,在我做之前请快速说明。在代码的开头添加这些行,否则此方法将无效。
import java.text.SimpleDateFormat;
import java.text.ParseException;
//--------------------------------------------------------------------------------
boolean isDate(String check) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
if (check.trim().length != dateFormat.toPattern().length) {
return false;
}
dateFormat.setLenient(false);
try {
dateFormat.parse(check.trim());
} catch (ParseException pe) {
return false;
}
return true;
}
isYear()
boolean isYear(String check) {
try {
int verify = Integer.parseInt(check);
} catch (NumberFormatException e) {
return false;
}
char[] verify2 = check.toCharArray();
if ((verify2.length).equals(4)) {
return true;
}
return false;
}
isPhone()
您还没有真正说过要检查的电话号码。我猜你想要一个这种形式(865-867-5309)。
boolean isPhone(String check) {
float firstHyphen = check.indexOf("-");
char[] numbers = check.toCharArray();
float check2 = numbers.length / 3;
if (firstHypen.equals(check2)) {
return true;
}
return false;
}
isLocation()
我道歉,我现在不能给你这个。 :(
isEmail()
boolean isEmail(String check) {
String[] emailDomains = {".com",".net",".org"};
String[] emailProviders = {"gmail","yahoo","hotmail","aol","tds","comcast","charter","peoplepc"}; //add more if you want
char[] check2 = check.toCharArray();
for (int i = 0; i <= emailProviders.length; i++) {
for (int x = 0; x <= emailDomains.length; x++) {
if ((check.indexOf(emailDomains[x]).equals(check2.length)) && (check.indexOf(emailProviders[i]).equals(check2.length - emailDomains[x].toCharArray().length)) {
return true;
}
}
}
return false;
}
如果您愿意,可以为isDistanceUnit()
和isWeightUnit()
方法添加更多单位。如果您需要更多方法或有任何疑问,请询问。 :)
答案 1 :(得分:2)
您所使用的语言中是否有相同的库?
我无法想到许多应用程序,您需要检查单个数据源中是否存在所有这些格式,但我会使用正则表达式来完成这项工作。
修改强>
如果您的数据是异构的,并且您想要弄清楚它是什么,那么您可能需要某种分类器。试试jBNC或classifier4j。
答案 2 :(得分:0)