/unlpbot/src/net/sf/unlpbot/irc/base/uNLPBotHeap.java
Java | 379 lines | 288 code | 8 blank | 83 comment | 36 complexity | 8bae41d670a6cde06d1dccf15e11b097 MD5 | raw file
Possible License(s): LGPL-2.1
- /**
- *
- * Projec name:
- * unlpbot
- * Module name:
- * unlpbot
- * Package name:
- * net.sf.unlpbot.irc.base
- * File name:
- * uNLPBotHeap.java
- * Created on:
- * 3-dic-2004
- * By:
- * gni
- * Email address:
- * gni at users.sourceforge.net
- *
- * --------------------------------------------------------------------------
- *
- * This file is part of uNLPBot project hosted on Sourceforge.net
- * at URL: http://sourceforge.net/projects/unlpbot/
- * You can visit project homepage
- * at URL: http://unlpbot.sourceforge.net/
- *
- * --------------------------------------------------------------------------
- *
- * Such project is owned, as date 3-dic-2004, by:
- * Sourceforge.net User:
- * gni
- * Sourceforge.net User ID:
- * 1154253
- * Sourceforge.net User Email address:
- * gni@users.sourceforge.net
- * Sourceforge.net Member since:
- * 2004-11-07 09:35 UTC
- *
- * --------------------------------------------------------------------------
- *
- * Details about project as date 3-dic-2004:
- * Sourceforge.net Project UNIX name: unlpbot
- * Sourceforge.net Project ID: 125225
- * Sourceforge.net Registered: 2004-11-27 15:55 UTC
- *
- * Sourceforge.net Original project description:
- * uNLPBot is a chatter bot based on NLP (Natural
- * Language Processing) theory, able to parse small but
- * representative subsets of english natural language and
- * to produce english sentences compliant to english grammar
- * and related to conversation threads.
- *
- * --------------------------------------------------------------------------
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to:
- * Free Software Foundation, Inc.
- * 59 Temple Place, Suite 330
- * Boston, MA 02111-1307
- * USA
- *
- * --------------------------------------------------------------------------
- * uNLPBot: Unintelligent Natural Language Processing chatter BOT
- * Copyright (C) 2004. All rights reserved.
- * Use is subject to license terms.
- * Initial developer(s): gni.
- *
- */
- package net.sf.unlpbot.irc.base;
- import java.lang.reflect.Method;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import com.mysql.jdbc.Statement;
- import java.util.*;
- import net.sf.snowball.SnowballProgram;
- import java.util.regex.*;
- /**
- * uNLPBot: Unintelligent Natural Language Processing chatter BOT<p>
- * Copyright (C) 2004. All rights reserved.<p>
- * Use is subject to license terms.<p>
- * Initial developer(s): gni.<p>
- * @author gni at users.sourceforge.net<p>
- */
- public class uNLPBotHeap {
- private Connection con=null;
- private String language=null;
- private int id=0;
- public boolean Connect() {
- try {
- this.con = DriverManager.getConnection("jdbc:mysql://"+uNLPBotGlobal.sqlhost+"/" +uNLPBotGlobal.sqldb+"?user="+uNLPBotGlobal.sqluser+"&password="+uNLPBotGlobal.sqlpass);
- return true;
- }
- catch (SQLException e) {
- System.out.println("SQL Exception: " + e.getMessage());
- e.printStackTrace(System.out);
- return false;
- }
- }
- public boolean Disconnect() {
- try {
- this.con.close();
- return true;
- }
- catch (SQLException e) {
- System.out.println("SQL Exception: " + e.getMessage());
- e.printStackTrace(System.out);
- return false;
- }
- }
- public uNLPBotHeap(String language, int id) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- }
- catch (ClassNotFoundException e) {
- System.out.println("Exception: Cannot Load JDBC Driver");
- return;
- }
-
- this.language = language;
- this.id = id;
- }
- public void MakeWordsAndStems(String message, Connection con) {
- this.con=con;
- Pattern unpattern = Pattern.compile("[^\\p{Alnum}??????]");
- Matcher unmatcher = unpattern.matcher(message);
- String cleanedmessage=unmatcher.replaceAll(" ");
- StringTokenizer st = new StringTokenizer(cleanedmessage);
- int nt = st.countTokens();
- String[] tokens = new String[nt];
- String[] stems = new String[nt];
- boolean m = true;
- int i=0;
- Pattern pattern = Pattern.compile("[\\p{Alnum}??????]+");
- while (st.hasMoreTokens() && m) {
- tokens[i] = st.nextToken().toLowerCase();
- try {
- Class stemClass = Class.forName("net.sf.snowball.ext." +
- this.language + "Stemmer");
- SnowballProgram stemmer = (SnowballProgram) stemClass.newInstance();
- Method stemMethod = stemClass.getMethod("stem", new Class[0]);
- stemmer.setCurrent(tokens[i]);
- Object [] emptyArgs = new Object[0];
- stemMethod.invoke(stemmer, emptyArgs);
- stems[i] = stemmer.getCurrent();
- Matcher matcher = pattern.matcher(tokens[i]);
- m=m && matcher.matches();
- i=i+1;
- } catch (Exception e) {}
- }
- int uresult=0;
- int fromid=0;
- int toid=0;
-
- int startword = 0;
- int endword = 0;
- for (i=0;i<nt && m;i++) {
- if (i==0) {
- startword=1;
- }
- if (i==(nt-1)) {
- endword=1;
- }
- if (i>0 && i<(nt-1) && i!=(nt-1)) {
- startword=0;
- endword=0;
- }
- try {
- Statement ustmt = (com.mysql.jdbc.Statement) this.con.createStatement();
- uresult = ustmt.executeUpdate("INSERT INTO `words` ( `botid`,`word`,`quantity`,`start`,`end`) \n" +
- "VALUES (\n" +
- "'"+this.id+"','"+tokens[i]+"','"+1+"','"+startword+"','"+endword+"'\n" +
- ");");
- ustmt.close();
- } catch (Exception e) {
- try {
- Statement ustmt = (com.mysql.jdbc.Statement) this.con.createStatement();
- uresult = ustmt.executeUpdate("UPDATE `words`\n" +
- "SET quantity=quantity+1,start=start+"+startword+",end=end+"+endword+"\n" +
- "WHERE `word`='"+tokens[i]+"' AND `botid`='"+this.id+"'\n" +
- ";");
- ustmt.close();
- } catch (Exception ee) {uresult=0;}
- }
- }
- int startstem=0;
- int endstem=0;
- for (i=0;i<nt && m;i++) {
- if (i==0) {
- startstem=1;
- }
- if (i==(nt-1)) {
- endstem=1;
- }
- if (i>0 && i<(nt-1) && i!=(nt-1)) {
- startstem=0;
- endstem=0;
- }
- try {
- Statement ustmt = (com.mysql.jdbc.Statement) this.con.createStatement();
- uresult = ustmt.executeUpdate("INSERT INTO `stems` ( `botid`,`stem`,`quantity`,`start`,`end`) \n" +
- "VALUES (\n" +
- "'"+this.id+"','"+stems[i]+"','"+1+"','"+startstem+"','"+endstem+"'\n" +
- ");");
- ustmt.close();
- } catch (Exception e) {
- try {
- Statement ustmt = (com.mysql.jdbc.Statement) this.con.createStatement();
- uresult = ustmt.executeUpdate("UPDATE `stems`\n" +
- "SET quantity=quantity+1,start=start+"+startstem+",end=end+"+endstem+"\n" +
- "WHERE `stem`='"+stems[i]+"' AND `botid`='"+this.id+"'\n" +
- ";");
- ustmt.close();
- } catch (Exception ee) {uresult=0;}
- }
- }
- }
- public void MakeWordsAndStems() {
- try {
- if (this.con.isClosed()) {
- this.Connect();
- }
- } catch (Exception e) {}
- try {
- Statement stmt = (com.mysql.jdbc.Statement) this.con.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM botlogs WHERE action='pubmsg' AND botid='"+this.id+"'");
- while (rs.next()) {
- String message = rs.getString("content");
- this.MakeWordsAndStems(message.toLowerCase(),this.con);
- }
- stmt.close();
- } catch (Exception e) {}
- }
-
- public void MakeEdges(String message, Connection con) {
- this.con=con;
- Pattern unpattern = Pattern.compile("[^\\p{Alnum}??????]");
- Matcher unmatcher = unpattern.matcher(message);
- String cleanedmessage=unmatcher.replaceAll(" ");
- StringTokenizer st = new StringTokenizer(cleanedmessage);
- int nt = st.countTokens();
- String[] tokens = new String[nt];
- String[] stems = new String[nt];
- boolean m = true;
- int i=0;
- Pattern pattern = Pattern.compile("[\\p{Alnum}??????]+");
- while (st.hasMoreTokens() && m) {
- tokens[i] = st.nextToken().toLowerCase();
- try {
- Class stemClass = Class.forName("net.sf.snowball.ext." +
- this.language + "Stemmer");
- SnowballProgram stemmer = (SnowballProgram) stemClass.newInstance();
- Method stemMethod = stemClass.getMethod("stem", new Class[0]);
- stemmer.setCurrent(tokens[i]);
- Object [] emptyArgs = new Object[0];
- stemMethod.invoke(stemmer, emptyArgs);
- stems[i] = stemmer.getCurrent();
- Matcher matcher = pattern.matcher(tokens[i]);
- m=m && matcher.matches();
- i=i+1;
- } catch (Exception e) {}
- }
- int uresult=0;
- int fromid=0;
- int toid=0;
- for (i=0;i<nt && m;i++) {
- try {
- Statement fstmt = (com.mysql.jdbc.Statement) con.createStatement();
- ResultSet frs = fstmt.executeQuery("SELECT id FROM words WHERE word='"+tokens[i]+"' AND botid='"+this.id+"'");
- if(frs.next()) {
- fromid=frs.getInt("id");
- }
- frs.close();
- fstmt.close();
- } catch (Exception fe) {}
- if (nt>1) {
- try {
- Statement tstmt = (com.mysql.jdbc.Statement) con.createStatement();
- ResultSet trs = tstmt.executeQuery("SELECT id FROM words WHERE word='"+tokens[i+1]+"' AND botid='"+this.id+"'");
- if(trs.next()) {
- toid=trs.getInt("id");
- }
- trs.close();
- tstmt.close();
- } catch (Exception te) {}
- } else {
- toid=fromid;
- }
- try {
- Statement ustmt = (com.mysql.jdbc.Statement) this.con.createStatement();
- uresult = ustmt.executeUpdate("INSERT INTO `wedges` ( `botid`,`fromid`,`toid`,`quantity`) \n" +
- "VALUES (\n" +
- "'"+this.id+"','"+fromid+"','"+toid+"','"+1+"'\n" +
- ");");
- ustmt.close();
- } catch (Exception e) {
- try {
- Statement ustmt = (com.mysql.jdbc.Statement) this.con.createStatement();
- uresult = ustmt.executeUpdate("UPDATE `wedges`\n" +
- "SET quantity=quantity+1\n" +
- "WHERE `fromid`='"+fromid+"' AND `toid`='"+toid+"' AND `botid`='"+this.id+"'\n" +
- ";");
- ustmt.close();
- } catch (Exception ee) {uresult=0;}
- }
- }
- for (i=0;i<nt && m;i++) {
- try {
- Statement fstmt = (com.mysql.jdbc.Statement) con.createStatement();
- ResultSet frs = fstmt.executeQuery("SELECT id FROM stems WHERE stem='"+stems[i]+"'");
- if(frs.next()) {
- fromid=frs.getInt("id");
- }
- frs.close();
- fstmt.close();
- } catch (Exception fe) {}
- if (nt>1) {
- try {
- Statement tstmt = (com.mysql.jdbc.Statement) con.createStatement();
- ResultSet trs = tstmt.executeQuery("SELECT id FROM stems WHERE stem='"+stems[i+1]+"'");
- if(trs.next()) {
- toid=trs.getInt("id");
- }
- trs.close();
- tstmt.close();
- } catch (Exception te) {}
- } else {
- toid=fromid;
- }
- try {
- Statement ustmt = (com.mysql.jdbc.Statement) this.con.createStatement();
- uresult = ustmt.executeUpdate("INSERT INTO `sedges` ( `botid`,`fromid`,`toid`,`quantity`) \n" +
- "VALUES (\n" +
- "'"+this.id+"','"+fromid+"','"+toid+"','"+1+"'\n" +
- ");");
- ustmt.close();
- } catch (Exception e) {
- try {
- Statement ustmt = (com.mysql.jdbc.Statement) this.con.createStatement();
- uresult = ustmt.executeUpdate("UPDATE `sedges`\n" +
- "SET quantity=quantity+1\n" +
- "WHERE `fromid`='"+fromid+"' AND `toid`='"+toid+"' AND `botid`='"+this.id+"'\n" +
- ";");
- ustmt.close();
- } catch (Exception ee) {uresult=0;}
- }
- }
- }
- public void MakeEdges() {
- try {
- if (this.con.isClosed()) {
- this.Connect();
- }
- } catch (Exception e) {}
- try {
- Statement stmt = (com.mysql.jdbc.Statement) this.con.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM botlogs WHERE action='pubmsg' AND botid='"+this.id+"'");
- while (rs.next()) {
- String message = rs.getString("content");
- this.MakeEdges(message.toLowerCase(),this.con);
- }
- stmt.close();
- } catch (Exception e) {}
- }
- }