⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nlp.java

📁 经典人工智能源程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// mwa.products.db.NLP.java
//
// Copyright 1997, Mark Watson.
//                                           

package nlbean;

public class NLP implements java.io.Serializable {

    public NLP(DBInfo dbi) {
      dbinfo=dbi;
    }

    final private int AND=1;
    final private int OR=2;

    public void parse(String s) {

      // parse a new sentence:
      currentWords = Util.parseStrings(s);
      if (currentWords==null) {
        System.out.println("Error parsing: " + s);
        return;
      }

      // Check for a REFINED QUERY that builds on the
      // last query (in which case, we do not want to
      // blank-out the slots from the last parse.
      //
      // Support:
      //   Mode 0:
      //         new query
      //
      //   Mode 1:
      //         and <column name>   -- adds a display column name
      //
      //   Mode 2:
      //         and <condition>     -- adds a SQL WHERE condition
      //

      int mode = 0; // 0->new query
      if (currentWords[0].equalsIgnoreCase("and") ||
          currentWords[0].equalsIgnoreCase("add"))
      {
         if (currentWords.length < 3)   mode=1;
         else                           mode=2;
      }


      System.out.println("parse(" + s + "): number of words in sentence=" + currentWords.length);
      currentWordIndex=0;

      if (mode==0) {
        tableName=null;
        time_after=null;
        time_before=null;
        num_temp_col_names = 0;
        currentAction=NO_OP;
        displayColumnName = "*";
        searchColumnName = null;
        searchString="*";
        searchColumnName2 = null;
        searchString2=null;
        searchColumnName3 = null;
        searchString3=null;
      }  else if (mode==1) {
        System.out.println("processing 'add/and <column name>'");
        currentWordIndex++;
        String cname = eatColumnName(null);
        if (cname != null) {
          displayColumnName = displayColumnName + ", " + cname;
          return;
        }
      } else if (mode==2) {
         System.out.println("processing 'and/add <condition>'");
         currentWordIndex++;
         String cname1 = eatColumnName(null);
         if (cname1 != null) {
            System.out.println("   cname1=" + cname1);
            // look for a condition:
            if (eatWord(equals_is)) {
              System.out.println("  equals_is matched **");
              if (currentWordIndex < currentWords.length) {
                searchColumnName2 = cname1;
                searchString2 = currentWords[currentWordIndex];
                return;
             }
           }
         }
         return;
      }


      if (eatWord(show) == false) return;
      eatWord(noise1); // optional; also check for column names
      displayColumnName = eatColumnName(null);

      // check for more column names of the form:
      //   <cn>, <cn>, and <cn>
      // NOTE: "," chars are already removed.

      eatWord(and);
      String temp = eatColumnName(null);
      if (temp!=null) {
        displayColumnName = displayColumnName + ", " + temp;
        eatWord(and);
        temp = eatColumnName(null);
        if (temp!=null) {
            displayColumnName = displayColumnName + ", " + temp;
        }
      }

      if (displayColumnName==null) displayColumnName="*";
      eatWord(where);   // WHERE
      searchColumnName = eatColumnName(null);  // displayColumnName);
      System.out.println("searchColumnName=" + searchColumnName);
      currentAction=LIST;
      eatWord(is); // skip 'is'

      quantity=-999.0f;
      compareMode=NONE;

      if (eatWord(less)) {
        eatWord(than);  // skip 'than'
        String quan = currentWords[currentWordIndex];
        try {
           Float f = new Float(quan);
           quantity = f.floatValue();
           compareMode=LESS;
           currentWordIndex++;
           System.out.println("less than " + quantity);
        } catch (Exception e) { }
      }

      if (eatWord(more)) {
        eatWord(than);  // skip 'than'
        String quan = currentWords[currentWordIndex];
        try {
           Float f = new Float(quan);
           quantity = f.floatValue();
           compareMode=MORE;
           currentWordIndex++;
           System.out.println("more than " + quantity);
        } catch (Exception e) { }
      }

      if (eatWord(after)) {
         if (currentWords.length > currentWordIndex+2) {
            String test = currentWords[currentWordIndex] + " " +
                          currentWords[currentWordIndex+1] + " " +
                          currentWords[currentWordIndex+2];
            time_after = new SmartDate(test);
            if (time_after.valid()==false)  time_after=null;
            else currentWordIndex+=3;
         }
         if (time_after==null & currentWords.length > currentWordIndex+1) {
            String test = currentWords[currentWordIndex] + " " +
                          currentWords[currentWordIndex+1];
            time_after = new SmartDate(test);
            if (time_after.valid()==false)  time_after=null;
            else currentWordIndex+=2;
         }
         if (time_after==null & currentWords.length > currentWordIndex) {
            String test = currentWords[currentWordIndex];
            time_after = new SmartDate(test);
            if (time_after.valid()==false)  time_after=null;
            else currentWordIndex+=1;
         }
      }
      if (time_after!=null) {
        System.out.println("parsed 'after' time OK:");
        System.out.println("  year:  " + time_after.getYear());
        System.out.println("  month: " + time_after.getMonth());
        System.out.println("  day:   " + time_after.getDayOfMonth());
      }



      if (eatWord(before)) {
         if (currentWords.length > currentWordIndex+2) {
            String test = currentWords[currentWordIndex] + " " +
                          currentWords[currentWordIndex+1] + " " +
                          currentWords[currentWordIndex+2];
            time_before = new SmartDate(test);
            if (time_before.valid()==false)  time_before=null;
            else currentWordIndex+=3;
         }
         if (time_before==null & currentWords.length > currentWordIndex+1) {
            String test = currentWords[currentWordIndex] + " " +
                          currentWords[currentWordIndex+1];
            time_before = new SmartDate(test);
            if (time_before.valid()==false)  time_before=null;
            else currentWordIndex+=2;
         }
         if (time_before==null & currentWords.length > currentWordIndex) {
            String test = currentWords[currentWordIndex];
            time_before = new SmartDate(test);
            if (time_before.valid()==false)  time_before=null;
            else currentWordIndex+=1;
         }
      }
      if (time_before!=null) {
        System.out.println("parsed 'before' time OK:");
        System.out.println("  year:  " + time_before.getYear());
        System.out.println("  month: " + time_before.getMonth());
        System.out.println("  day:   " + time_before.getDayOfMonth());
      }



      conditionMode = 0;

      if (searchColumnName==null) return;
      if (eatWord(and)) {   // check for AND condition
         System.out.println("processing 'and/add <condition>'");
         String cname1 = eatColumnName(null);
         if (cname1 != null) {
            System.out.println("   cname1=" + cname1);
            // look for a condition:
            if (eatWord(equals_is)) {
              System.out.println("  equals_is matched **");
              if (currentWordIndex < currentWords.length) {
                searchColumnName2 = cname1;
                searchString2 = currentWords[currentWordIndex];
                conditionMode = AND;
             }
           }
         }
      }
      if (eatWord(or)) {   // check for OR condition
         System.out.println("processing 'and/add <condition>'");
         String cname1 = eatColumnName(null);
         if (cname1 != null) {
            System.out.println("   cname1=" + cname1);
            // look for a condition:
            if (eatWord(equals_is)) {
              System.out.println("  equals_is matched **");
              if (currentWordIndex < currentWords.length) {
                searchColumnName2 = cname1;
                searchString2 = currentWords[currentWordIndex];
                conditionMode = OR;
             }
           }
         }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -