2016-10-31 33 views
-2

我使用谷歌雲視覺API(Text_Detection)這是工作正常,但在谷歌我回來的答案的時候,消息樣式如何修復響應文本?像圖像

我想只是一個文本如「學術策劃師」這樣我怎麼能去除學術面前「null:」等字?

圖像e.g

enter image description here

這裏是我的代碼;

private String convertResponseToString(BatchAnnotateImagesResponse response) { 
    String message = "I found these things:\n\n"; 

    List<EntityAnnotation> labels = response.getResponses().get(0).getTextAnnotations(); 
    if (labels != null) { 
     for (EntityAnnotation label : labels) { 
      message += String.format("%.3f: %s", label.getScore(), label.getDescription()); 
      message += "\n"; 
     } 
    } else { 
     message += "nothing"; 
    } 

    return message; 
} 

回答

0

答:

private String convertResponseToString(BatchAnnotateImagesResponse response) { 
    String message = "I found these things:\n\n"; 
    List<EntityAnnotation> labels = response.getResponses().get(0).getTextAnnotations(); 
    if (labels != null) { 
     message += labels.get(0).getDescription(); 
    } else { 
     message += "nothing"; 
    } 
    return message; 
} 
-1

如果我正確理解你的問題,那麼你只需要與Stringsubstring()trim()方法消毒標籤的描述。這裏是你的代碼的修改後的版本:

private String convertResponseToString(BatchAnnotateImagesResponse response) { 

    String message = "I found these things:\n\n"; 
    List<EntityAnnotation> labels = response.getResponses().get(0).getTextAnnotations(); 
    if (labels != null) { 
     for (EntityAnnotation label : labels) { 
      // 5=index of the character after "null:" (zero-based array counting), trim() removes whitespace on both sides of the string. 
      message += String.format("%.3f: %s", label.getScore(), label.getDescription().substring(5).trim()); 
      message += "\n"; 
     } 
    } else { 
     message += "nothing"; 
    } 

    return message; 
} 

PS:從文檔,getDescription()方法返回一個字符串和getScore()返回一個浮點數。我敢打賭,分數並沒有給你任何問題。我沒有測試過你的實際數據。

+0

都能跟得上它不是工作。我嘗試但我會得到eror。 imgur.com/uEckW8Z Android工作室不允許這個代碼,另一個想法? –

+0

如果你想在這裏是完整的代碼:https://github.com/GoogleCloudPlatform/cloud-vision/tree/master/android/CloudVision –

+0

糟糕,這些方法只適用於字符串和'EntityAnnotation'不是一個字符串。 ..我測試了我對一系列字符串的調整,很抱歉;看到編輯的答案。 – Dut

0

你的分數可能是null。這樣做:

message += String.format("%s", label.getDescription()); 

要只有一個字,你讓你的方法是這樣的:

private String convertResponseToString(BatchAnnotateImagesResponse response) { 
    String message = "I found these things:\n\n"; 

    List<EntityAnnotation> label = response.getResponses().get(0).getTextAnnotations().get(0); 
    if (label != null) { 
      message += String.format("%s", label.getDescription()); 
      message += "\n"; 
    } else { 
     message += "nothing"; 
    } 

    return message; 
} 
+0

Yeap。這是真的謝謝你和其他問題你有什麼想法嗎? –

+0

@EmreAkbaki看到我的更新。 – BlackHatSamurai

+0

不工作它不接受「label.getScore(),label.getDescription());」和爲什麼你寫兩次.get(0) ? –