roganjoshua

roganjoshua

Guards or pattern matching processing a text file - =~

I am trying to process a text file like a kind of plain text guitar tab with etc. a bit like the Ultimate Guitar Tab site.
Each line will either blanks or chords or lyrics or something like that.

defmodule TextFileProcessor do
  def process_file(file_path) do
    File.stream!(file_path)
    |> Enum.each(&process_line/1)
  end

  defp process_line(line) when line =~ ~r/^LYRICS:/ do
    # Process lines starting with "LYRICS:"
    IO.puts("Processing lyrics line: #{line}")
  end

  defp process_line(line) when line =~ ~r/^CHORDS:/ do
    # Process lines starting with "CHORDS:"
    IO.puts("Processing chords line: #{line}")
  end

  defp process_line(line) do
    # Default processing for other lines
    IO.puts("Processing line: #{line}")
  end
end

I can’t use =~ in a guard or other Kernel functions is there a better way to do this?

I hope there is enough in here.

TIA

Marked As Solved

derpycoder

derpycoder

Here’s my take on it:

"""
LYRICS: L1
CHORDS: C1
LYRICS: L2
CHORDS: C2
CHORDS: C3
CHORDS: C4
CHORDS: C5
CHORDS: C6
TEMPO: T1
foobar\
"""
|> String.split("\n")
|> Enum.map_reduce(%{}, fn line, acc ->
  hd = line |> String.split(":") |> hd()

  {line, if(hd in ["LYRICS", "CHORDS", "TEMPO"]) do
      Map.update(acc, hd, [line], fn arr -> [line | arr] end)
  else
      Map.update(acc, "REST", [line], fn arr -> [line | arr] end)
  end}
end)
|> then(fn {_lines, acc} -> acc end)

or if you don’t care about specifics in the first iteration, you can do:

"""
LYRICS: L1
CHORDS: C1
LYRICS: L2
CHORDS: C2
CHORDS: C3
CHORDS: C4
CHORDS: C5
CHORDS: C6
TEMPO: T1
foobar\
"""
|> String.split("\n")
|> Enum.map_reduce(%{}, fn line, acc ->
  hd = line |> String.split(":") |> hd() || "REST"

  {line, Map.update(acc, hd, [line], fn arr -> [line | arr] end)}
end)
|> then(fn {_lines, acc} -> acc end)

Both the pipelines, spit out an organized map, which you can then process.

%{
  "CHORDS" => ["CHORDS: C6", "CHORDS: C5", "CHORDS: C4", "CHORDS: C3", "CHORDS: C2", "CHORDS: C1"],
  "LYRICS" => ["LYRICS: L2", "LYRICS: L1"],
  "REST" => ["foobar"],
  "TEMPO" => ["TEMPO: T1"]
}

If you don’t want that map, you can directly process it within the pipeline above.


P.S.

  1. Instead of split, you can use regex.
  2. Instead of if-else, you can use cond.

Personally, I prefer this way because it makes it composable and allows me to use dbg() to see if anything in the pipeline is not working as expected.

Also Liked

dimitarvp

dimitarvp

I am pretty late here but I’d advise against regexes unless they are very straightforward. Haven’t looked into yours in details (and length is not always an indicator of a complex regex) but I’d probably reach for nimble_parsec and give it a go for a day or two.

That being said, being productive in that particular library is a skill in and of itself so if you are pressed for time you should probably keep your regex solution but also add edge case unit tests.

christhekeele

christhekeele

Not in guards/function heads. You’ll need to use a single function with something like a cond inside.

roganjoshua

roganjoshua

If anyone is interested I think I kind of got to where I wanted like this:

defmodule Textprocessor do
  @regex_find_chords ~r/\b(?:[BE]b?|[ACDFG]#?)(?:sus|m|maj|min|[-1-9\/m])?(?:sus|m|maj|min|[-1-9\/m])?\b#?/
  @empty_line ~r/^\s*$/
  @instruction ~r/^\[[\w+ ]+\]\s*$/

  @moduledoc """
  Documentation for `Textprocessor`.
  """

  @doc """
  Process song

  ## Examples
    # iex> Textprocessor.process_file("./lyrics.text")
    # :ok
  """

  def process_file(filename) do
    File.stream!(filename)
    |> Enum.map(&String.trim/1)
    |> Enum.each(&process_line/1)
  end

  defp process_line(line) do
    cond do
      String.match?(line, @regex_find_chords) ->
        IO.puts("CHORDS: #{line}")

      String.match?(line, @instruction) ->
        IO.puts("INSTRUCTION: #{line}")

      String.match?(line, @empty_line) ->
        IO.puts("EMPTY: #{line}")

      true ->
        IO.puts("LYRICS: #{line}")
        :ok
    end
  end
end

Not sure this is optimal but I would be interested in any criticism

Last Post!

roganjoshua

roganjoshua

Thanks Dimitar

I think the likely size of the files is going to be pretty small, in the order 100 lines so hopefully regex will do for now.

I will see how I go with it before attempting nimble_parsec.

I am very much at the beginning of my elixir journey.

Where Next?

Popular in Questions Top

PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement