1

ドキュメント変換により、HTMLドキュメントのConcept InsightsをPDFから変換するためのクラスを作成したいと考えています。私は私のGitディレクトリのビューでEclipse IDEを使用しています。私はそれを実行すると、私は応答がありません。私はそれをきちんと保ちたいが、それぞれのメソッドが必要な情報を取得することを確認する。とにかく、これまでのコードはここにあります。Javaでドキュメント変換とコンセプト分析を使用するためのクラスを作成する

package example.servlet; 
package com.ibm.watson.developer_cloud.document_conversion.v1; 
package com.ibm.watson.developer_cloud.concept_insights.v1; 

import java.io.File; 
import java.io.IOException; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.apache.commons.io.FileUtils; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 



import com.ibm.watson.developer_cloud.document_conversion.v1.model.Answers; 
import com.ibm.watson.developer_cloud.util.CredentialUtils; 



/** 
* Servlet implementation class SimpleServlet 
*/ 
@WebServlet("/SimpleServlet") 
public class SimpleServlet extends HttpServlet { 

    ConceptInsights service = new ConceptInsights(); 
    DocumentConversion docService = new DocumentConversion(DocumentConversion.VERSION_DATE_2015_12_01); 

    service.setUsernameAndPassword("username", "password"); 

    Accounts my_accounts = service.getAccountsInfo(); 
    System.out.println(my_accounts); 
    String my_account_id = my_accounts.getAccounts().get(0).getId(); 
    System.out.println("My account ID: "+ my_account_id); 
    docService.setUsernameAndPassword("docUsername", "docPassword"); 

    @Override 
    public void init() throws ServletException { 
     super.init(); 
     String vcap = System.getProperty("VCAP_SERVICES"); 
     if (vcap == null){ 
      try { 
       vcap = FileUtils.readFileToString(new File("vcap.txt")); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

    } 

    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     response.setContentType("text/html"); 
     response.getWriter().print("Hello World!"); 
    } 

} 

public void transformDocs(File[] availableDocs){ 
    /* Files.walk(Paths.get("/home/you/Desktop")).forEach(filePath -> { 
    if (Files.isRegularFile(filePath)) { 
     Document test = new Document(my_corpus, filePath); 
test.setLabel("Test: "+filePath); 
    } 
}); 

File folder = new File("/Users/you/folder/"); 
File[] listOfFiles = folder.listFiles(); 

for (File file : listOfFiles) { 
    if (file.isFile()) { 
     Document test = new Document(my_corpus, filePath); 
test.setLabel("Test: "+filePath); 
    } 
}*/ 
    } 
} 

public void fixTheInput(){ 
    CredentialUtils.setServices(vcap); 
    File doc = new File("docs/cpc-scheme-A.pdf"); 
    Answers pdfToHTML = service.convertDocumentToHTML(doc); 
    System.out.println(pdfToHTML); 

    Corpus patents1 = new Corpus("evelandments", "PatentsCorpus"); 
    Corpus patents2 = new Corpus("perdermy1ipres6", "patents"); 

    Map<String, Object> params = new HashMap<String, Object>(); 
    params.put(ConceptInsights.LIMIT, 3); 

    Concepts concepts1 = service.getCorpusRelatedConcepts(patents1, params); 
    Concepts concepts2 = service.getCorpusRelatedConcepts(patents2, params); 

    System.out.println(concepts1); 
    System.out.println(concepts2); 
} 

public void createTheCorpus(){ 
    Corpus my_corpus = new Corpus(service.getAccountsInfo().getAccounts().get(0).getId(), "CPCScheme"); 

    // add corpus metadata 
    AccountPermission permission = new AccountPermission(); 
    permission.setAccountId("account_id"); 
    permission.setPermission("ReadWriteAdmin"); 
    List<AccountPermission> accountPermissions = new ArrayList<AccountPermission>(); 
    accountPermissions.add(permission); 
    my_corpus.setAccountPermissions(accountPermissions); 
    // 

    service.createCorpus(my_corpus); 
    System.out.println("Created corpus: "+ my_corpus.getName()); 

    Document test = new Document(my_corpus, "test.html"); 
    test.setLabel("Test: my first document"); 


    } 
} 

答えて

2

ここでは、ドキュメント変換を使用してpdfをテキストに変換するクラスの例を示します。 Concept Insightsを使用して概念を抽出します。

package com.ibm.watson.developer_cloud.retrieve_and_rank.v1; 

import java.io.File; 
import java.io.FileInputStream; 

import org.apache.commons.io.IOUtils; 

import com.ibm.watson.developer_cloud.concept_insights.v2.ConceptInsights; 
import com.ibm.watson.developer_cloud.concept_insights.v2.model.Annotations; 
import com.ibm.watson.developer_cloud.concept_insights.v2.model.Graph; 
import com.ibm.watson.developer_cloud.concept_insights.v2.model.ScoredConcept; 
import com.ibm.watson.developer_cloud.document_conversion.v1.DocumentConversion; 
import com.ibm.watson.developer_cloud.util.CredentialUtils; 


public class WatsonExample { 
    public static void loadCredentialsFromFile() { 
    try { 
     CredentialUtils.setServices(IOUtils.toString(new FileInputStream("vcap.txt"))); 
    } catch (Exception e) { 
     System.out.println("Error reading vcap.txt file"); 
     e.printStackTrace(); 
    } 
    } 


    public static void main(String[] args) { 
    loadCredentialsFromFile(); 
    ConceptInsights conceptInsights = new ConceptInsights(); 
    DocumentConversion documentConversion = new DocumentConversion(DocumentConversion.VERSION_DATE_2015_12_01); 

    // extract the text from the pdf 
    String text = documentConversion.convertDocumentToText(new File("docs/cpc-scheme-A.pdf")); 

    // annotate the text 
    Annotations annotations = conceptInsights.annotateText(Graph.WIKIPEDIA, text); 

    // print out concepts 
    for (ScoredConcept concept : annotations.getAnnotations()) { 
     System.out.println(concept.toString()); 
    } 
    } 

} 

資格情報がある場合はvcap.txtから読み込まれます。

+1

これを変更します。ありがとうございました。 –

+0

私はあなたの例を編集してビルドするのに困っています。私は何か間違っているのですか? –

+0

JDK> = 1.7、java-sdk> = 2.8.0、および両方のサービスの資格情報が必要です。 –

関連する問題