2016-10-31 31 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) ? –