从小就有一个梦想,将来开发机器人,说白了,就是人工智能。现在看来,可喜的是,做的是软件这行,还有点可行性,可惜的是,做的工作与人工智能不搭嘎,工作又太累,天天加班不说,大老远跑回家,就没那么多时间去思考,学习人工智能了。徘徊犹豫了几个月,一直没有决心去写代码,这不,终于决定继续小时候的梦想,开始了用java实现人工智能的第一步了,那就是用java来分析语义,让我们从头开始。
我的第一个Milestone很简单,用户输入一些语句作为java的知识源,然后程序自动分析推导出其他的事实。Come up some examples:
~X(Man(X)>Die(X))
Man(
Socrates)
这是两个事实,程序应该要给出Die(
Socrates),那第一阶段就算完成。
~代表任意,这主要是任意的符号不好打,就用波浪线代替了。->用>来代替。存在用@, 否用!, 交用&,或用|,等价就用=
要完成第一阶段得分两步,第一部是分析语句,第二部分是根据规则来进行推导。这第一部分已经完成了。实现的思路:本来是要参考编译器的,后来想想借鉴一下即可,采用了递归的方法。还需要测试,如果有bug,还请批评指正。
如果您也对于人工智能有兴趣,或者想合作开发一些赚钱的项目,java或者iphone的都可以,欢迎和我联系,我的QQ627423443
第一部分代码如下:
/** *//**
*
*/
package src.analyser;
import java.util.ArrayList;
import java.util.List;
import utils.StringUtils;
/** *//**
* @author ken
*
*/
public class BasicAnalyser {
private List<String> predicationList = new ArrayList<String>();
public boolean isSentence(String exp) {
exp = exp.trim();
if (StringUtils.isEmpty(exp)) {
return false;
}
exp = StringUtils.trimBracket(exp);
if (isAtomicSentence(exp)) {
return true;
}
if (exp.charAt(0) == '!') {
return isSentence(exp.substring(1));
} else if (exp.charAt(0) == '~' || exp.charAt(0) == '@') {
// means any or exist, must followed by VAR
String tmp = "";
int i = 0;
for (i = 1; i < exp.length(); i++) {
if ((exp.charAt(i) >= 'A' && exp.charAt(i) <= 'Z') || exp.charAt(i) == '_') {
tmp = tmp + exp.charAt(i);
} else {
break;
}
}
if (i >= exp.length()) {
// means ~X
return false;
} else {
return isSentence(exp.substring(i));
}
} else {
// means S1 op S2
StringBuffer lastSentence = new StringBuffer();
int leftBracketNum = 0;
int rightBracketNum = 0;
for (int i = 0; i < exp.length(); i++) {
if (isOperator(exp.charAt(i))) {
if (leftBracketNum == rightBracketNum) {
if (i >= exp.length()) {
return false;
} else {
if (isSentence(lastSentence.toString()) && isSentence(exp.substring(i + 1))) {
return true;
} else {
return false;
}
}
}
} else {
if (exp.charAt(i) == '(') {
leftBracketNum++;
} else if (exp.charAt(i) == ')') {
rightBracketNum++;
}
lastSentence = lastSentence.append(exp.charAt(i));
}
}
}
return false;
}
private boolean isOperator(char x) {
return x == '|' || x == '&' || x == '>' || x == '=';
}
public boolean isVar(String expression) {
expression.trim();
expression = StringUtils.trimBracket(expression);
if (StringUtils.isEmpty(expression)) {
return false;
}
if (expression.charAt(0) >= 'A' && expression.charAt(0) <= 'Z') {
for (int i = 1; i < expression.length(); i++) {
if ((expression.charAt(i) >= 'a' && expression.charAt(0) <= 'z') || (expression.charAt(i) >= 'A' && expression.charAt(i) <= 'Z')
|| expression.charAt(i) == '_') {
// do nothing
} else {
return false;
}
}
return true;
}
return false;
}
public boolean isConstant(String expression) {
expression.trim();
expression = StringUtils.trimBracket(expression);
if (StringUtils.isEmpty(expression)) {
return false;
}
if (expression.charAt(0) >= 'a' && expression.charAt(0) <= 'z') {
for (int i = 1; i < expression.length(); i++) {
if ((expression.charAt(i) >= 'a' && expression.charAt(0) <= 'z') || (expression.charAt(i) >= 'A' && expression.charAt(i) <= 'Z')
|| expression.charAt(i) == '_') {
// do nothing
} else {
return false;
}
}
return true;
}
return false;
}
public boolean isLikeFormFunc(String expression, boolean upperCase) {
if (StringUtils.isEmpty(expression)) {
return false;
}
char startLetterFrom = upperCase ? 'A' : 'a';
char startLetterTo = upperCase ? 'Z' : 'z';
expression.trim();
expression = StringUtils.trimBracket(expression);
expression = expression.replaceAll(" ", "");
if (expression.charAt(0) >= startLetterFrom && expression.charAt(0) <= startLetterTo) {
int leftBracket = 0;
int rightBracket = 0;
StringBuffer lastString = new StringBuffer();
for (int i = 0; i < expression.length(); i++) {
if (leftBracket == 0) {
if (!((expression.charAt(i) >= 'A' && expression.charAt(i) <= 'Z')
|| (expression.charAt(i) >= 'a' && expression.charAt(i) <= 'z') || expression.charAt(i) == '_' || expression.charAt(i) == '(')) {
return false;// illegal
}
}
if (expression.charAt(i) == '(') {
leftBracket++;
if (leftBracket > 1) {
lastString.append(expression.charAt(i));
}
} else if (expression.charAt(i) == ')') {
if (i == expression.length() - 1) {
// last one
if (!isVar(lastString.toString()) && !isConstant(lastString.toString()) && !isFunction(lastString.toString())) {
return false;
} else {
return true;
}
}
rightBracket++;
lastString.append(expression.charAt(i));
} else if (expression.charAt(i) == ',') {
if (leftBracket - rightBracket == 1) {
if (!isVar(lastString.toString()) && !isConstant(lastString.toString()) && !isFunction(lastString.toString())) {
return false;
}
lastString = new StringBuffer();
} else {
lastString.append(expression.charAt(i));
}
} else {
if (leftBracket != 0) {
lastString.append(expression.charAt(i));
}
}
}
}
return false;
}
public boolean isAtomicSentence(String expression) {
// Friends(father_of(david),mother_of(peter))
boolean isJustification = isLikeFormFunc(expression, true);
if (isJustification) {
predicationList.add(expression);
}
return isJustification;
}
public boolean isFunction(String expression) {
return isLikeFormFunc(expression, false);
}
public static void main(String[] args) {
BasicAnalyser x = new BasicAnalyser();
String m = "Test(test(t),a,b)&Fuck(st,m)>Monday(today)";
System.out.println(x.isSentence(m));
for (int i = 0; i < x.getPredicationList().size(); i++) {
System.out.println(x.getPredicationList().get(i));
}
}
public List<String> getPredicationList() {
return predicationList;
}
public void setPredicationList(List<String> predicationList) {
this.predicationList = predicationList;
}
}