2010年12月24日金曜日

TwitterStreamingAPIを使って日本語だけを取り出す

以前書いたTwitterStreamingAPIを使う話は,Twitter4jを使っていなかったので,今度はTwitter4jを使ったバージョンを書いてみる.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;


public class StreamReader implements StatusListener{

   /**
    * @param args
    */
   public static void main(String[] args) {
      StreamReader streamReader = new StreamReader(User, Password);
      streamReader.start();

   }
   
   
   /**
    * ユーザ名
    */
   String userName;

   /**
    * パスワード
    */
   String password;

   /**
    * stream
    */
   private TwitterStream streamTwitter;
   
   
   
   
   public StreamReader(String userName, String password) {
      super();
      this.userName = userName;
      this.password = password;
   }

   public void start() {
      TwitterStreamFactory factory = new TwitterStreamFactory();
      streamTwitter = factory.getInstance(userName, password);
      streamTwitter.addListener(this);
      streamTwitter.sample();
   }

   @Override
   public void onStatus(Status status) {
      if(isJapanese(status.getText())){
         System.out.printf("%d\t%s\t%s\n", status.getId(), status.getUser().getScreenName(), status.getText());
         //得られたStatusに対する処理をする
      }
      
   }

   /**
    * 日本語かどうかを返す
    * @param text
    * @return 日本語ならtrue
    */
   boolean isJapanese(String text){
      Matcher m = Pattern.compile("([\\p{InHiragana}\\p{InKatakana}])").matcher(text);
      return m.find();
   }
   

   @Override
   public void onDeletionNotice(StatusDeletionNotice arg0) {
      
   }



   @Override
   public void onException(Exception arg0) {
      
   }

   @Override
   public void onTrackLimitationNotice(int arg0) {
      
   }

}
多分,これで動くはず.
おまけで日本語だけを取得するようにしておいた.

2010年12月10日金曜日

アクティブ遷移図利用促進キャンペーン中

今度,第3回知識共有コミュニティワークショップにて
アクティブ遷移図に関する発表を行います.
それに伴って,
OpenPNE3用プラグインアクティブ遷移図opActiveTransitionPlugin 利用促進キャンペーン中です.
いや,キャンペーンって行っても何するわけでもないけれど.

OpenPNE3を使っていて,興味がある管理者は今すぐ以下のURLへGo!
アクティブ遷移図作成プラグインの解説ページ
OpenPNEの活性度を可視化する、アクティブ遷移図プラグイン

あなたのOpenPNE3の利用状況をグラフィカルにお見せします.

2010年11月26日金曜日

GoogleChartAPIをリアルタイムにテストする

GoogleChartAPIは、URL指定だけで図を描画してくれる素敵なAPI。
でも、結構書くのが面倒で、ちゃんと書いたものが動くかどうかいちいち確認するのも大変。

そこで、テスト用にコードを書けば即座に図を描画してくれる以下のURLがとても便利。
http://code.google.com/intl/en-EN/apis/chart/docs/chart_playground.html

ぽじったーでも使ってます。

GAEでユーザ管理

GAEでAdministratorしか使えないページを作る場合.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService userService = UserServiceFactory.getUserService();
        String thisURL = request.getRequestURI();
        if (request.getUserPrincipal() != null && userService.isUserAdmin()) {
         String urlLogout = userService.createLogoutURL(thisURL);
         request.setAttribute("urlLogout", urlLogout);

         RequestDispatcher rd = request.getRequestDispatcher(jsp).forward(request, response);
        } else {
            response.getWriter().println("<p>
Please <a href=\"" +userService.createLoginURL(thisURL) +"\">sign in</a>.</p>");
        }

 }
これで,jsp側で
<%
String urlLogout = (String)request.getAttribute("urlLogout");
%>
・・・
<a href="<%=urlLogout%>">ログアウト</a>
としてあげればOk.

2010年11月2日火曜日

いろんな自然言語資源

自然言語関連の辞書とかコーパスが色々あると面白いなと思って,調べてみた.

電子的じゃないけど,役立ちそうな辞典.
他にもこんな辞書があるよ,ということをご存じの方がいたら,
コメント欄にお願いします.

2010年11月1日月曜日

プレゼンタイマーオンライン修正

以前作成したプレゼンタイマーオンラインが,
IEでは動かないという致命的バグがあったのですが,修正しました.
http://conference-timer.appspot.com/
これで,誰でも使えるはず!

2010年9月3日金曜日

bit.lyの短縮URLをワンクリックで作る

TwitterにURLを張るときは,http://bit.ly/などの短縮URLを使うのが普通らしいけど,
いちいちbitlyへ移動して作るのが面倒くさいので,ブックマークレットを作ってみた.
javascript:(function(){
var%20s=location.href;s.charset="UTF-8";
var%20b="http://bit.ly/?u="+encodeURIComponent(s)+"&searchButton=Shorten";
window.open(b,"bitly");})();
使ってみたい人は,以下のURLをブックマークバーにドラッグしてください.
[短縮URL]
たぶん,もっと便利なものを誰かが作っているんだろうなあと,思いつつ.