I'm learning Elixir and Phoenix - please review my code

I’m learning Elixir, I have chosen a small project for learning where the entered text is converted from Cyrillic into Latin. With clean text and text files fulfills the norms and quickly. Now I don’t know how to handle doc and docx. Anyone interested and have the time, you are welcome.

2 Likes

Docx is xml based so you should be able to view the actual xml files as a starting place. Years ago I had created a ruby library to generate docx documents so you should be able to put something together. I think the standard is called Open Office XML.

2 Likes

Hi @arruah! Nice job :+1:

Maybe this libreoffice library can help you with getting the text from different file formats.

Using the map lookups for this work maybe is too slow (for production code, but not for learning!). Pattern matching and recursion would be a better fit. &String.upcase/1 is a bit hard to understand, because of some macros etc, but it’s super fast and based on these two things. There should be other examples out there on this.

As you don’t have many entries, you wouldn’t need to get into macros to make them if you don’t want to.

3 Likes

Thanks for the reply. Can you tell me how I can use pattern matching in this project? And I’m also wondering if OpenCL and GPU usage can speed up the processing of large amounts of text?

Edit: About pattern matching vs maps. Found this article.

@arruah something like this maybe?

EDIT: this talks more about recursion: https://elixir-lang.org/getting-started/recursion.html

defmodule Transliterator do
  # just to define default initial value for the accumulator
  def transliterate(text, acc \\ [])
  
  # when string begins with a, add ã to the accumulator instead,
  # and call the same function recursively to the rest
  def transliterate("a" <> rest, acc) do
    transliterate(rest, ["ã" | acc])
  end

  # when string begins with n, add ñ to the accumulator instead,
  # and call the same function recursively to the rest
  def transliterate("n" <> rest, acc) do
    transliterate(rest, ["ñ" | acc])
  end

  # for another character that I don't care about transliterating,
  # just add it to the accumulator without changing,
  # and call the same function recursively to the rest
  def transliterate(<<char, rest::bits>>, acc) do
    transliterate(rest, [char | acc])
  end

  # when there's no rest anymore (empty string), we get to the end
  # of the recursion, and we get what we accumulated and convert back
  # to a string
  def transliterate("", acc) do
    acc
    |> :lists.reverse()
    |> IO.iodata_to_binary()
  end
end

Transliterator.transliterate("omg")
=> "omg"

Transliterator.transliterate("amg")
=> "ãmg"

Transliterator.transliterate("a")
=> "ã"
1 Like