2009年06月23日

javascriptで英単語抽出


javascriptで簡単な英単語抽出をやってみました。 別に特別な事はしてないんだけど、なんとなく作ったので一応ブログにUP。
◆ソース

<script type="text/javascript">
function word_hash(str)
{
var word_container = [];
var sp = str.split(/[ ,.;:`']/);

for(var i = 0;i < sp.length;i++)
{
if(word_container[sp[i]] == undefined)
{
word_container[sp[i]] = 1;
}else{
word_container[sp[i]] += 1;
}
}

return word_container;
}

function word_select()
{
word_container = word_hash($("#word").text());
var tmp = '';
for(var k in word_container)
{
tmp += k+':'+word_container[k]+'<br>';
}
$('#hash_result').html(tmp);
}
</script>
・単語抽出元<br>
<div id="word">Either the well was very deep, or she fell very slowly, for she had plenty of time as she went down to look about her and to wonder what was going to happen next. First, she tried to look down and make out what she was coming to, but it was too dark to see anything; then she looked at the sides of the well, and noticed that they were filled with cupboards and book-shelves; here and there she saw maps and pictures hung upon pegs. She took down a jar from one of the shelves as she passed; it was labelled `ORANGE MARMALADE', but to her great disappointment it was empty: she did not like to drop the jar for fear of killing somebody, so managed to put it into one of the cupboards as she fell past it. </div>
<input type="button" onclick="word_select();" value="単語抽出"><br>
・単語(単語:出現数)<br>
<div id="hash_result"></div>

◆実行
 ・単語抽出元
 
Either the well was very deep, or she fell very slowly, for she had plenty of time as she went down to look about her and to wonder what was going to happen next. First, she tried to look down and make out what she was coming to, but it was too dark to see anything; then she looked at the sides of the well, and noticed that they were filled with cupboards and book-shelves; here and there she saw maps and pictures hung upon pegs. She took down a jar from one of the shelves as she passed; it was labelled `ORANGE MARMALADE', but to her great disappointment it was empty: she did not like to drop the jar for fear of killing somebody, so managed to put it into one of the cupboards as she fell past it.


 ・単語(単語:出現数)
 




posted by purigen at 01:36| Comment(0) | TrackBack(0) | javascript | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。

この記事へのトラックバック
×

この広告は180日以上新しい記事の投稿がないブログに表示されております。