Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    lỗi lạ khi đưa file vào biến

    em gà java lắm, các bác giúp em với
    thuật toán này so khớp với các mẫu "pattern" mình nhập vào với nội dung file (save ở định dạng unicode)
    các bác tạo 1 file text ở desktop chạy thử & nhập mẫu để so với nó
    in file ra thì được nhưng đến dòng đưa vào biến s thì bị lỗi
    nếu ko đưa file vào mà gán giá trị sẵn cho s thì chạy bthg` =.=
    Mã:
    package ahofix;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Ahofix {
        static final int ALPHABET_SIZE = 26;
    
      Ahofix.Node[] nodes;
      int nodeCount;
      
    //Bước 2.Xây dựng hàm Goto Function
      
      public static class Node {
        int parent;
        char charFromParent;
        int suffLink = -1;
        int[] children = new int[ALPHABET_SIZE];
        int[] transitions = new int[ALPHABET_SIZE];
        boolean leaf;
    
        {
          Arrays.fill(children, -1);
          Arrays.fill(transitions, -1);
        }
      }
      //Bước 3. Xây dựng hàm Failure Function
         public Ahofix(int maxNodes) {
        nodes = new Ahofix.Node[maxNodes];
        // create root
        nodes[0] = new Ahofix.Node();
        nodes[0].suffLink = 0;
        nodes[0].parent = -1;
        nodeCount = 1;
      }
    //Bước 4. Xây dựng hàm Output Function
      public void addString(String s) {
        int cur = 0;
        for (char ch : s.toCharArray()) {
          int c = ch - 'a';
          if (nodes[cur].children[c] == -1) {
            nodes[nodeCount] = new Ahofix.Node();
            nodes[nodeCount].parent = cur;
            nodes[nodeCount].charFromParent = ch;
            nodes[cur].children[c] = nodeCount++;
          }
          cur = nodes[cur].children[c];
        }
        nodes[cur].leaf = true;
      }
    //Bước 5. Thực hiện quá trình Transition
      public int suffLink(int nodeIndex) {
        Ahofix.Node node = nodes[nodeIndex];
        if (node.suffLink == -1)
          node.suffLink = node.parent == 0 ? 0 : transition(suffLink(node.parent), node.charFromParent);
        return node.suffLink;
      }
    
      public int transition(int nodeIndex, char ch) {
        int c = ch - 'a';
        Ahofix.Node node = nodes[nodeIndex];
        if (node.transitions[c] == -1)
          node.transitions[c] = node.children[c] != -1 ? node.children[c] : (nodeIndex == 0 ? 0 : transition(suffLink(nodeIndex), ch));
        return node.transitions[c];
      }
      //B6
         public  String ReadFile(String pathFile) {
            try
            {
        //Tạo một con trỏ để connect đến pathFile
        FileInputStream fileInPutStream = new FileInputStream(pathFile.trim());
        //Đọc file với Unicode
        Reader reader = new java.io.InputStreamReader(fileInPutStream, "Unicode");
        //Đọc từng byte
        BufferedReader buffReader = new BufferedReader(reader);
        //Tạo 1 stringBuilder để lấy từng dòng (line) của file
        StringBuilder stringBuilder = new StringBuilder();
        String line = null;
        while ((line = buffReader.readLine()) != null)
            {
            //stringBuilder cho phép cộng dồn "append"
            stringBuilder.append(line + "
    ");
            }
         reader.close();
            return stringBuilder.toString();
            }
        catch(IOException io)
        {
     System.out.println("File not found ! " + pathFile);
     }
     return "";
     }
      
    public void Process()
    {
      Ahofix ahoCorasick = new Ahofix(1000);
       System.out.println("enter number of patterns:"); 
      Scanner nhap = new Scanner(System.in);
        int n=nhap.nextInt();
        String q , pattern,s,pathFile;
      for(int i=1;i<=n;i++)
      {
          System.out.println("enter pattern number "+i+" :");   
          q=nhap.next();     
          ahoCorasick.addString(q);
      } 
      
      System.out.println("enter pathfile:");
      pathFile=nhap.next();
      
      s=ahoCorasick.ReadFile(pathFile);
      //s=nhap.next();
      System.out.println(s);
      int node = 0;
        for (char ch :s.toCharArray()) {
          node = ahoCorasick.transition(node, ch);
        }
        if(ahoCorasick.nodes[node].leaf==true) System.out.println("Match !");
        else System.out.println("Not match !");
    }
     public static void main(String[] args)  {
     Ahofix ahoCorasick = new Ahofix(1000);
         ahoCorasick.Process();}  
    //C:\\Users\\Dell\\Desktop\\file.txt
    }

    run:
    enter number of patterns:
    1
    enter pattern number 1 :
    bc
    enter pathfile:
    C:\\Users\\Dell\\Desktop\\file.txt
    abc

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -87
    at ahofix.Ahofix.transition(Ahofix.java:69)
    at ahofix.Ahofix.Process(Ahofix.java:123)
    at ahofix.Ahofix.main(Ahofix.java:130)
    Java Result: 1

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Hi bạn !
    Exception này là do mảng của bạn (do index không đúng vị trí trong mảng).

    Thanks,
    Nhan.

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •