Skip to content

Added analyze_document sample code in Elixir #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions elixir/sample_mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Elixir.MixProject do
use Mix.Project

def project do
[
app: :elixir,
version: "0.1.0",
elixir: "~> 1.12",
compilers: Mix.compilers(),
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# ...

def deps do
[
# TODO: Add this into your project and check the latest version from: https://hexdocs.pm/aws/readme.html#installation
{:aws, "~> 0.13.0"},
{:hackney, "~> 1.18"}
]
end

# ...
end
47 changes: 47 additions & 0 deletions elixir/services/textract.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule Elixir.Textract do
@moduledoc """
A sample code example in Elixir.
"""

@doc """
Analyze document.
> Copy this function into your Elixir project and copy the `deps/0` in `mix_sample.exs` into your `mix.exs`.

## Examples
# Run this to try out `analyze_document/3`/`analyze_document/4`
YourProject.Textract.analyze_document("access_key", "secret_key", "region", "path")

> More explanations here: https://docs.aws.amazon.com/textract/latest/dg/how-it-works-analyzing.html

Example provided by @enkr1 | [email protected]

"""
def analyze_document(access_key, secret_key, region, png_path \\ "/path/to/png") do
case AWS.Client.create(access_key, secret_key, region)
|> AWS.Textract.analyze_document(%{
"Document" => %{"Bytes" => png_path |> File.read!() |> Base.encode64()},
"FeatureTypes" => ["TABLES", "FORMS"]
}) do
{
:ok,
%{
"AnalyzeDocumentModelVersion" => _,
"Blocks" => blocks,
"DocumentMetadata" => _
} = _result_map,
response = _map_with_encoded_body_str
} ->
# ...
response |> IO.inspect(label: "response")

{:error, {_key, %{body: reason}} = reason_tupple} ->
# ...
reason_tupple |> IO.inspect(label: "reason_tupple")
{:error, reason}

edge_case ->
edge_case |> IO.inspect(label: "edge_case")
{:error, "Something went wrong when extracting the file ..."}
end
end
end