Skip to content

Instantly share code, notes, and snippets.

@deltam
Created October 2, 2010 16:06
Show Gist options
  • Save deltam/607759 to your computer and use it in GitHub Desktop.
Save deltam/607759 to your computer and use it in GitHub Desktop.
<html>
<title>10分で書くベイジアンフィルタ</title>
<script type="text/javascript">
<!--//
function run() {
var hash = splitWord(form.classA.value);
var str = "";
var sum = 0;
for (key in hash) {
str += key + ": " + hash[key] + ";¥n";
sum += hash[key];
}
form.out.value = str + "sum = " + sum;
}
function splitWord(str) {
var ret = [];
var pat = /[一-龠]+|[ぁ-ん]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+/;
while (true) {
var m = str.match(pat);
if (m != null && m.length > 0 && m[0].length > 0) {
str = str.substring(RegExp.lastIndex);
if (ret[m[0]] != undefined)
ret[m[0]]++;
else
ret[m[0]] = 1;
}
else
break;
}
return ret;
}
//-->
</script>
<body>
<h1>10分で書くベイジアンフィルタ</h1>
<form name="form">
Class A:<textarea name="classA" rows="10" cols="40"></textarea>
Class B:<textarea name="classB" rows="10" cols="40"></textarea><br />
<input value="ベイズ判定" type="button" onclick="run()" /><br />
out:<br />
<textarea name="out" rows="10" cols="40"></textarea><br />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment