资源简介
来自M.I.T的人工智能代码实现Java版,研究人工智能的必下
代码片段和文件信息
import java.util.*;
public class BayesianSpamChecker {
public BayesianSpamChecker(Hashtable wordProbabilities) {
this.wordProbabilities = wordProbabilities;
}
public float probabilityOfSpam(String text) throws Exception {
Vector words = Tokenizer.getTokens(text);
double probs = 1 probs_minus_one = 1;
HashSet set = new HashSet();
for (int i = 0; i < 15; i++) {
interesting[i] = -1;
wrds[i] = ““;
}
// get unique words:
for (int i = 0 size = words.size(); i < size; i++) {
set.add(words.get(i));
}
Iterator iter = set.iterator();
while (iter.hasNext()) {
String w = (String) iter.next();
float f = 0.5f;
Float ff = (Float) wordProbabilities.get(w);
if (ff != null) {
f = ff.floatValue();
}
float interest = Math.abs(f - 0.5f);
// see if this word is more interesting than on of the 15:
// first find the smallest value to replace (if it is smaller than the
// value in ‘interest‘:
float minVal = 9999;
int minIndex = 0;
for (int i = 0; i < 15; i++) {
if (minVal > interesting[i]) {
minVal = interesting[i];
minIndex = i;
}
}
//System.out.println(“w=“+w+“ minVal=“+minVal+“ minIndex=“+minIndex);
if (interest > minVal) {
interesting[minIndex] = interest;
wrds[minIndex] = w;
}
}
for (int i = 0; i < 15; i++) {
if (interesting[i] < 0) continue;
Float ff = (Float) wordProbabilities.get(wrds[i]);
if (ff != null) {
float f = ff.floatValue();
//System.out.println(“word: “ + wrds[i] + “ interest: “ + interesting[i] + “ spam prob: “ + f);
probs *= f;
probs_minus_one *= (1 - f);
}
}
//if (Math.abs(f - 0.5f) > 0.05f) {
// probs *= f;
// probs_minus_one *= (1 - f);
//}
return (float) (probs / (probs + probs_minus_one));
}
private Hashtable wordProbabilities;
float[] interesting = new float[15];
String[] wrds = new String[15];
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2005-10-31 23:09 src\
目录 0 2005-07-04 23:15 src\search\
目录 0 2005-07-04 23:15 src\search\statespace\
目录 0 2005-07-04 23:15 src\search\statespace\mazeexample\
文件 2592 2000-01-26 17:53 src\search\statespace\mazeexample\AbstractSearchEngine.java
文件 3750 2000-06-21 02:17 src\search\statespace\mazeexample\BreadthFirstSearchEngine.java
文件 1332 2000-06-21 02:33 src\search\statespace\mazeexample\DepthFirstSearchEngine.java
文件 2217 2002-01-20 01:59 src\search\statespace\mazeexample\Maze.java
文件 3184 2002-01-20 01:59 src\search\statespace\mazeexample\MazeBreadthFirstSearch.java
文件 3303 2002-01-20 01:59 src\search\statespace\mazeexample\MazeDepthFirstSearch.java
目录 0 2005-07-04 23:15 src\search\statespace\graphexample\
文件 2732 2002-01-20 02:44 src\search\statespace\graphexample\AbstractGraphSearch.java
文件 2773 2002-01-20 02:44 src\search\statespace\graphexample\AbstractGraphSearch.java~
文件 2928 2002-01-20 02:44 src\search\statespace\graphexample\BreadthFirstSearch.java
文件 2969 2002-01-20 02:44 src\search\statespace\graphexample\BreadthFirstSearch.java~
文件 2885 2002-01-20 02:44 src\search\statespace\graphexample\DepthFirstSearch.java
文件 2926 2002-01-20 02:44 src\search\statespace\graphexample\DepthFirstSearch.java~
文件 3209 2002-01-20 02:44 src\search\statespace\graphexample\GraphBreadthFirstSearch.java
文件 3342 2002-01-20 02:44 src\search\statespace\graphexample\GraphBreadthFirstSearch.java~
文件 3193 2002-01-20 02:44 src\search\statespace\graphexample\GraphDepthFirstSearch.java
文件 3326 2002-01-20 02:44 src\search\statespace\graphexample\GraphDepthFirstSearch.java~
目录 0 2005-07-04 23:15 src\search\games\
文件 272 2000-06-01 21:13 src\search\games\Chess$aMove.class
文件 7123 2000-06-01 21:13 src\search\games\Chess.class
文件 17259 2000-06-01 21:13 src\search\games\Chess.java
文件 212 2000-05-30 22:27 src\search\games\ChessMove.class
文件 84 2000-05-30 21:34 src\search\games\ChessMove.java
文件 889 2000-05-30 22:27 src\search\games\ChessPosition.class
文件 681 2000-05-30 21:38 src\search\games\ChessPosition.java
文件 2558 2000-05-30 22:27 src\search\games\GameSearch.class
文件 4802 2000-05-30 20:27 src\search\games\GameSearch.java
............此处省略147个文件信息
评论
共有 条评论