Hello Gökhan DUMLUPINAR,
Welcome to Microsoft Q&A and Thank you for reaching out.
I understand that you are trying to get Azure OCR to return the full sentence or line whenever a specific word is clicked. Azure OCR does give you word-level bounding boxes, but the sentence/line structure isn't always clean because the API mainly organizes text into blocks → lines → words.
To achieve what you want, here are the key things to adjust:
- Use line or block context when a word is selected
Each word already belongs to a specific line, and each line belongs to a block. So instead of relying on extracted text (which might not be well separated), you should use the API’s structure directly:
- When a word is clicked → find its parent line
- Return line.text (which is the closest equivalent to a “sentence”)
- If you need a bigger context, return block text
- Modify your logic to capture the entire line on word click
Right now you are looping only through line.words. You should simply map the clicked word back to its parent line:
iaResult.readResult.blocks.forEach((block) => {
block.lines.forEach((line) => {
line.words.forEach((word) => {
if (word.text === targetWord) {
console.log(line.text);
// This gives the complete sentence/line for the clicked word
}
});
});
});
- Improve extracted text structure (optional)
If the extracted text looks jumbled when you concatenate it, make sure you group it by:
const extractedText =
iaResult.readResult.blocks
.map(block => block.lines.map(line => line.text).join('\n'))
.join('\n\n');
For better sentence/paragraph separation
Azure OCR is line-based. If you need richer structure (paragraphs, tables, semantic grouping), consider using Azure Document Intelligence, which gives better layout and sentence continuity.
You can find which sentence a clicked word belongs to by using the OCR hierarchy. Every word already has a parent line, so when you click a word, simply return line.text. This is the correct and most reliable way to show the full sentence or line for that word, since Azure OCR does not automatically separate full sentences. If you need more advanced text grouping, Azure Document Intelligence can provide better structure.
I Hope this helps. Do let me know if you have any further queries.
Thank you!