Concept:OCRDocument/Java

From BITPlan Wiki
Jump to navigation Jump to search

java code

@// This is a rythm template
@// the args are the standard wikiTask arguments
@import org.sidif.triple.TripleQuery
@import org.sidif.triple.Triple
@import com.alibaba.fastjson.JSON
@args() {
  String title 
  String logo
  org.sidif.wiki.WikiTask wikiTask
  org.sidif.triple.TripleStore tripleStore
}


@def static {

  /**
   * Base class
   */
  static abstract class TopicBase {
    // each Topic has a pageid - for non subobject thats the pagename
    public String pageid;

    /**
     * get a WikiSon version of the given name value
     * 
     * @param name
     * @param value
     * @return - the string representation
     */
    public String toWikiSon(String name, String value) {
      String result = "<!-- " + name + " is null-->\n";
      if (value != null)
        result = "|" + name + "=" + value + "\n";
      return result;
    }

    /**
     * get the SiDIF representation of the given property
     * 
     * @param name - the name of the property
     * @param value - the value of the property
     * @param type - the type of the property
     * @return - the SiDIF Sting representation of the property
     */
    public static String propertySiDIF(String name, String value, String type) {
      // default is a comment line which can be filled by uncommenting
      String result = String.format("# is is %s of it\n",name);;
      // if the value is not empty
      if ((value != null) && (!("".equals(value.trim())))) {
        // do we need to quote the result?
        String quote = "";
        // this depends on the Type
        if ("Text".equals(type)) {
          quote = "\"";
        }
        // create a SIDIF Property line like
        // "John" is lastname of it
        // convert double quotes to single quotes - FIXME - should we escape instead?
        value=value.replace("\"","'");
        result = String.format("%s%s%s is %s of it\n",quote,value,quote,name);
      }
      // return the SiDIF property line
      return result;
    }

    /**
     * get me as a String
     * 
     * @param name
     * @param value
     * @return
     */
    public static String propertySiDIF(String name, String value) {
      String result = propertySiDIF(name, value, "Text");
      return result;
    }

    /**
     * check if the given boolean String value is true
     * 
     * @param value
     * @return true if the value is not null and has true/TRUE as it's string
     *         content
     */
    public boolean isTrue(String value) {
      boolean result = false;
      if (value != null && value.toLowerCase().equals("true")) {
        result = true;
      }
      return result;
    }

    /**
     * initialize
     */
    public void init(TripleQuery query) {
    }
  } // TopicBase
 /**
  * OCRDocument
  * An OCRDocument is a document that has been scanned and for which optical character recognition has been performed
  */
  public static class OCRDocument extends TopicBase {
  
    public String id;
    public String name;
    public String nextpage;
    public String previouspage;
    public String page;
    public String pages;
    public String pdfname;

    public String getId() { return id; }
    public void setId(String pId) { id=pId; }
    public String getName() { return name; }
    public void setName(String pName) { name=pName; }
    public String getNextpage() { return nextpage; }
    public void setNextpage(String pNextpage) { nextpage=pNextpage; }
    public String getPreviouspage() { return previouspage; }
    public void setPreviouspage(String pPreviouspage) { previouspage=pPreviouspage; }
    public String getPage() { return page; }
    public void setPage(String pPage) { page=pPage; }
    public String getPages() { return pages; }
    public void setPages(String pPages) { pages=pPages; }
    public String getPdfname() { return pdfname; }
    public void setPdfname(String pPdfname) { pdfname=pPdfname; }
    /**
     * convert this OCRDocument to a JSON string
     */
    public String toJson() { return JSON.toJSONString(this); }

    /**
     * convert this OCRDocument to a WikiSon string
     * @return the WikiSon representation of this OCRDocument
     */
    public String toWikiSon() {
      String wikison= "{{OCRDocument\n";
      wikison+=toWikiSon("id",id);
      wikison+=toWikiSon("name",name);
      wikison+=toWikiSon("nextpage",nextpage);
      wikison+=toWikiSon("previouspage",previouspage);
      wikison+=toWikiSon("page",page);
      wikison+=toWikiSon("pages",pages);
      wikison+=toWikiSon("pdfname",pdfname);
      wikison+="}}\n";
      return wikison;
    }

    /**
     * convert this OCRDocument to a SiDIF string
     * @return the SiDIF representation of this OCRDocument
     */
    public String toSiDIF() {
      String siDIF = String.format("%s isA OCRDocument\n",this.pageid);
      siDIF+=propertySiDIF("id",id,"Types/Text");
      siDIF+=propertySiDIF("name",name,"Types/Text");
      siDIF+=propertySiDIF("nextpage",nextpage,"Types/Number");
      siDIF+=propertySiDIF("previouspage",previouspage,"Types/Number");
      siDIF+=propertySiDIF("page",page,"Types/Number");
      siDIF+=propertySiDIF("pages",pages,"Types/Number");
      siDIF+=propertySiDIF("pdfname",pdfname,"Types/Text");
      return siDIF;
    }
 
    /**  
     * get the pageid for this topic
     */
    public String getPageid() { return pageid; };

    /**
     * default constructor for OCRDocument
     */
    public OCRDocument() {}

    /**
     * construct a OCRDocument from the given Triple
     * @param query - the TripleQuery to get the triples from
     * @param pOCRDocumentTriple - the triple to construct me from
     */
    public OCRDocument(TripleQuery query,Triple pOCRDocumentTriple) {
      this(query,pOCRDocumentTriple.getSubject().toString());
    } // constructor

    /**
     * construct a OCRDocument from the given pageId
     * @param query - the TripleQuery to get the triples from
     * @param pageid - pageid
     */
    public OCRDocument(TripleQuery query,String pageid) {
      this.pageid=pageid;
      Triple idTriple=query.selectSingle(pageid,"id",null);
      if (idTriple==null)
        idTriple=query.selectSingle(pageid,"Property:OCRDocument_id",null);
      if (idTriple!=null) 
        id=idTriple.getObject().toString();
      Triple nameTriple=query.selectSingle(pageid,"name",null);
      if (nameTriple==null)
        nameTriple=query.selectSingle(pageid,"Property:OCRDocument_name",null);
      if (nameTriple!=null) 
        name=nameTriple.getObject().toString();
      Triple nextpageTriple=query.selectSingle(pageid,"nextpage",null);
      if (nextpageTriple==null)
        nextpageTriple=query.selectSingle(pageid,"Property:OCRDocument_nextpage",null);
      if (nextpageTriple!=null) 
        nextpage=nextpageTriple.getObject().toString();
      Triple previouspageTriple=query.selectSingle(pageid,"previouspage",null);
      if (previouspageTriple==null)
        previouspageTriple=query.selectSingle(pageid,"Property:OCRDocument_previouspage",null);
      if (previouspageTriple!=null) 
        previouspage=previouspageTriple.getObject().toString();
      Triple pageTriple=query.selectSingle(pageid,"page",null);
      if (pageTriple==null)
        pageTriple=query.selectSingle(pageid,"Property:OCRDocument_page",null);
      if (pageTriple!=null) 
        page=pageTriple.getObject().toString();
      Triple pagesTriple=query.selectSingle(pageid,"pages",null);
      if (pagesTriple==null)
        pagesTriple=query.selectSingle(pageid,"Property:OCRDocument_pages",null);
      if (pagesTriple!=null) 
        pages=pagesTriple.getObject().toString();
      Triple pdfnameTriple=query.selectSingle(pageid,"pdfname",null);
      if (pdfnameTriple==null)
        pdfnameTriple=query.selectSingle(pageid,"Property:OCRDocument_pdfname",null);
      if (pdfnameTriple!=null) 
        pdfname=pdfnameTriple.getObject().toString();
      init(query);
    } // constructor for OCRDocument
    
    // >>>{user defined topic code}{OCRDocument}{OCRDocument}
    // <<<{user defined topic code}{OCRDocument}{OCRDocument}
  } // class OCRDocument
  /**
   * Manager for OCRDocument
   */
  public static class OCRDocumentManager extends TopicBase {
 
    public String topicName="OCRDocument";
    public transient List<OCRDocument> mOCRDocuments=new ArrayList<OCRDocument>();
    public transient Map<String,OCRDocument> mOCRDocumentMap=new LinkedHashMap<String,OCRDocument>();

    /**
     * get my OCRDocuments
     */
    public List<OCRDocument> getOCRDocuments() {
      List<OCRDocument> result=this.mOCRDocuments;
      return result;
    }

    /**
     *  add a new OCRDocument 
     */
    public OCRDocument add(OCRDocument pOCRDocument) {
      mOCRDocuments.add(pOCRDocument);
      mOCRDocumentMap.put(pOCRDocument.getPageid(),pOCRDocument);
      return pOCRDocument;
    }

    /**
     *  add a new OCRDocument from the given triple
     */
    public OCRDocument add(TripleQuery query,Triple pOCRDocumentTriple) {
      OCRDocument lOCRDocument=new OCRDocument(query,pOCRDocumentTriple);
      add(lOCRDocument);
      return lOCRDocument;
    }

    // reinitialize my mOCRDocument map
    public void reinit() {
      mOCRDocumentMap.clear();
      for (OCRDocument lOCRDocument:mOCRDocuments) {
        mOCRDocumentMap.put(lOCRDocument.getPageid(),lOCRDocument);
      }
    }

    // convert this manager to json format 
    public String toJson() { return JSON.toJSONString(this); }
    
    // get a new manager from the given json string
    public static OCRDocumentManager fromJson(String json) {
      OCRDocumentManager result=JSON.parseObject(json, OCRDocumentManager.class);
      result.reinit();
      return result;
    }

    // default constructor for OCRDocument Manager
    public OCRDocumentManager() {}

    // add OCRDocuments from the given query
    public void addOCRDocuments(TripleQuery pOCRDocumentQuery,TripleQuery query) {
      if (pOCRDocumentQuery!=null) {
        for (Triple lOCRDocumentTriple:pOCRDocumentQuery.getTriples()) {
          add(query,lOCRDocumentTriple);
        }
      }
    }

    // construct me from the given triple Query query
    public OCRDocumentManager(TripleQuery query) {
      // first query the SiDIF bases triplestore
      TripleQuery lOCRDocumentQuery=query.query(null,"isA","OCRDocument");
      addOCRDocuments(lOCRDocumentQuery,query);
      // then the SMW triplestore
      lOCRDocumentQuery=query.query(null,"Property:IsA","OCRDocument");
      addOCRDocuments(lOCRDocumentQuery,query);
      init(query);
    } // constructor for OCRDocument Manager
    
    // >>>{user defined topicmanager code}{OCRDocument}{OCRDocument}
    // <<<{user defined topicmanager code}{OCRDocument}{OCRDocument}
  } // class OCRDocument Manager

}
Showing below 0 pages.

Retrieved from "https://wiki.bitplan.com/index.php?title=Concept:OCRDocument/Java&oldid=14766#smw-result"