Exemplo n.º 1
0
 /**
  * count the keyword in one line string.
  *
  * @param content
  * @return the count
  */
 private int countLineKMP(String content) {
   int count = 0;
   if (content != null) {
     int index = kmp.indexOf(content, keyWord);
     while (index != -1) {
       count++;
       index = kmp.indexOf(content, keyWord, index + 6);
     }
   }
   return count;
 }
Exemplo n.º 2
0
  // test client
  public static void main(String[] args) {
    String pat = args[0];
    String txt = args[1];
    char[] pattern = pat.toCharArray();
    char[] text = txt.toCharArray();

    KMP kmp1 = new KMP(pat);
    int offset1 = kmp1.search(txt);

    KMP kmp2 = new KMP(pattern, 256);
    int offset2 = kmp2.search(text);

    // print results
    StdOut.println("text:    " + txt);

    StdOut.print("pattern: ");
    for (int i = 0; i < offset1; i++) StdOut.print(" ");
    StdOut.println(pat);

    StdOut.print("pattern: ");
    for (int i = 0; i < offset2; i++) StdOut.print(" ");
    StdOut.println(pat);
  }
Exemplo n.º 3
0
  public static void match_tweet() {
    /* menentukan kategori setiap tweet */
    // untuk setiap tweet
    for (int i = 0; i < TweetResultList.size(); i++) {
      boolean match = false;
      int result = -1;

      // untuk kategori 1
      // untuk setiap keyword
      int j = 0;
      int category1Size = sportList.size();
      while ((j < category1Size) && (!match)) {
        String pattern = sportList.get(j);
        if (type.equals("KMP")) {
          KMP A = new KMP(pattern);
          result = A.SearchOn(TweetResultList.get(i).getContent());
        } else if (type.equals("BM")) {
          BoyMoore B = new BoyMoore(pattern);
          result = B.SearchOn(TweetResultList.get(i).getContent());
        }
        if (result != -1) {
          match = true;
          String output =
              "[sport][user]@"
                  + TweetResultList.get(i).getUsername()
                  + ":[/user][tweet]"
                  + TweetResultList.get(i).getContent()
                  + "[/tweet][link]"
                  + TweetResultList.get(i).getLink()
                  + "[/link][/catend]";
          System.out.println(output);
        }
        j++;
      }

      // kategori2
      j = 0;
      int category2Size = hiburanList.size();
      while ((j < category2Size) && (!match)) {
        String pattern = hiburanList.get(j);
        if (type.equals("KMP")) {
          KMP A = new KMP(pattern);
          result = A.SearchOn(TweetResultList.get(i).getContent());
        } else if (type.equals("BM")) {
          BoyMoore B = new BoyMoore(pattern);
          result = B.SearchOn(TweetResultList.get(i).getContent());
        }
        if (result != -1) {
          match = true;
          String output =
              "[hiburan][user]@"
                  + TweetResultList.get(i).getUsername()
                  + ":[/user][tweet]"
                  + TweetResultList.get(i).getContent()
                  + "[/tweet][link]"
                  + TweetResultList.get(i).getLink()
                  + "[/link][/catend]";
          System.out.println(output);
        }
        j++;
      }

      // kategori3
      j = 0;
      int category3Size = teknologiList.size();
      while ((j < category3Size) && (!match)) {
        String pattern = teknologiList.get(j);
        if (type.equals("KMP")) {
          KMP A = new KMP(pattern);
          result = A.SearchOn(TweetResultList.get(i).getContent());
        } else if (type.equals("BM")) {
          BoyMoore B = new BoyMoore(pattern);
          result = B.SearchOn(TweetResultList.get(i).getContent());
        }
        if (result != -1) {
          match = true;
          String output =
              "[teknologi][user]@"
                  + TweetResultList.get(i).getUsername()
                  + ":[/user][tweet]"
                  + TweetResultList.get(i).getContent()
                  + "[/tweet][link]"
                  + TweetResultList.get(i).getLink()
                  + "[/link][/catend]";
          System.out.println(output);
        }
        j++;
      }

      // kategori4
      j = 0;
      int category4Size = healthBeautyList.size();
      while ((j < category4Size) && (!match)) {
        String pattern = healthBeautyList.get(j);
        if (type.equals("KMP")) {
          KMP A = new KMP(pattern);
          result = A.SearchOn(TweetResultList.get(i).getContent());
        } else if (type.equals("BM")) {
          BoyMoore B = new BoyMoore(pattern);
          result = B.SearchOn(TweetResultList.get(i).getContent());
        }
        if (result != -1) {
          match = true;
          String output =
              "[healthbeauty][user]@"
                  + TweetResultList.get(i).getUsername()
                  + ":[/user][tweet]"
                  + TweetResultList.get(i).getContent()
                  + "[/tweet][link]"
                  + TweetResultList.get(i).getLink()
                  + "[/link][/catend]";
          System.out.println(output);
        }
        j++;
      }

      if (!(match)) {
        String output =
            "[unknown][user]@"
                + TweetResultList.get(i).getUsername()
                + ":[/user][tweet]"
                + TweetResultList.get(i).getContent()
                + "[/tweet][link]"
                + TweetResultList.get(i).getLink()
                + "[/link][/catend]";
        System.out.println(output);
      }
    }
  }