code: public static List<TypedSpan> createSpanList(String[] toks, String[] tags) {
  
      // initialize with the list maximum size
      List<TypedSpan> phrases = new ArrayList<TypedSpan>(toks.length); 
      String startTag = "";
      int startIndex = 0;
      boolean foundPhrase = false;
  
      for (int ci = 0, cn = tags.length; ci < cn; ci++) {
        String pred = tags[ci];
        if(!tags[ci].startsWith("B-") && !tags[ci].startsWith("I-")) {
          pred = "O";
        }
        if (pred.startsWith("B-")
            || (!pred.equals("I-" + startTag) && !pred.equals("O"))) { // start
          if (foundPhrase) { // handle the last
            phrases.add(new TypedSpan(startIndex, ci, startTag));
          }
          startIndex = ci;
          startTag = pred.substring(2);
          foundPhrase = true;
        } else if (pred.equals("I-" + startTag)) { // middle
          // do nothing
        } else if (foundPhrase) {// end
          phrases.add(new TypedSpan(startIndex, ci, startTag));
          foundPhrase = false;
          startTag = "";
        }
      }
      if (foundPhrase) { // leftover
        phrases.add(new TypedSpan(startIndex, tags.length, startTag));
      }
  
      return phrases;
    }
docs: this is from opennlp