Justinbenfit23

Justinbenfit23

Getting ID (first element of response) into a map while manipulating second element

New to Elixir, I built three functions that give me the number of times one of a list of special words occurs in each car dealership review scraped from the internet. Scraped responses look like this before applying the functions:

[
  {"July 03, 2021",
   "We had been looking for a 2021 Suburban for about 6 months and no one could find exactly what we wanted! We contacted Adrian at McKaig and he told us he could order one for us! Our Suburban was delivered in 4 weeks and had everything on it that we wanted! Adrian, Brandon, Dennis and Freddie all worked with us to get exactly what we wanted! They made phone calls and deals for us right on the spot and we drove out with a beautiful black Suburban! We will definitely use Adrian and McKaig Chevrolet again! Thank you for a fun car buying experience!"},
  {"July 03, 2021",
   "Adrian first educated me on trade ins. And Joe helped me have a car with a better fit and one I can feel good about! "},
  {"July 03, 2021",
   "Adrian was able to finally help my fiance get the truck he needed for quite some time now! We left Mckaig extremely happy and grateful! As always customer service was amazing especially Adrian’s and Joe’s!"},... ]

The number of occurrences only looks at the second element (the review text itself) and is saved in key titled score. I’m trying to keep the date included to act as an ID. but can’t figure out how to include ID in the comprehension without screwing it up.

Here is the code and current result as well as desired result:

def list_conv() do
      input = get_body()
      new_input = Enum.map(input, fn n -> elem(n,1) end)
      
      for n <- new_input do
        String.downcase(n)
        |> String.split(" ", trim: true)
      end
end


def special_words() do
      special_words = ["extremely", "definitely","amazing","very","best","great","excellent","awesome","incredibly","beyond","loved","really","highly"]

     input = list_conv()
     for i <- input do
        {Enum.count(i, &(&1 in special_words)), i}
      end
    
def spec_map() do
      nl = special_words()
      for {score, x} <- nl do
        %{score: score,
        }
      end
end

Result:

[
  %{score: 0},
  %{score: 1},
  %{score: 0}, ...]

Desired Result:

[
  %{score: 0, id: "July 10, 2021"},
  %{score: 1,id: "July 03, 2021"},
  %{score: 0, id: "July 03, 2021"}... ]

Thank you!

Marked As Solved

gregvaughn

gregvaughn

Consider something like this, given your reviews and special_words in the top post:

iex(6)> compute_score = fn review -> {special, total} = review |> String.downcase |> String.split |> Enum.reduce({0, 0}, fn w, {sp, to} ->
...(6)> if w in special_words, do: {sp + 1, to + 1}, else: {sp, to + 1} end)                                                              
...(6)> special / total                                                                                                                   
...(6)> end                                                                                                                               
#Function<44.79398840/1 in :erl_eval.expr/5>
iex(7)> for {d, r} <- reviews, do: %{id: :erlang.phash2(r), date: d, score: compute_score.(r)}                                            
[
  %{date: "July 03, 2021", id: 59481064, score: 0.009615384615384616},
  %{date: "July 03, 2021", id: 47140814, score: 0.0},
  %{date: "July 03, 2021", id: 76368188, score: 0.05714285714285714}
]

compute_score uses a two-tuple accumulator to Enum.reduce to keep track of the words that match special_words and a total. I also used :erlang.phash2 to get a unique identifier based upon the review text.

Also Liked

thiagomajesk

thiagomajesk

Hi @Justinbenfit23!

Check if something like this helps you get an idea on how you can tackle this:

reviews = [
  {"July 03, 2021",
   "We had been looking for a 2021 Suburban for about 6 months and no one could find exactly what we wanted! We contacted Adrian at McKaig and he told us he could order one for us! Our Suburban was delivered in 4 weeks and had everything on it that we wanted! Adrian, Brandon, Dennis and Freddie all worked with us to get exactly what we wanted! They made phone calls and deals for us right on the spot and we drove out with a beautiful black Suburban! We will definitely use Adrian and McKaig Chevrolet again! Thank you for a fun car buying experience!"},
  {"July 03, 2021",
   "Adrian first educated me on trade ins. And Joe helped me have a car with a better fit and one I can feel good about! "},
  {"July 03, 2021",
   "Adrian was able to finally help my fiance get the truck he needed for quite some time now! We left Mckaig extremely happy and grateful! As always customer service was amazing especially Adrian’s and Joe’s!"}
]

special_words = ["extremely", "definitely","amazing","very","best","great","excellent","awesome","incredibly","beyond","loved","really","highly"]

reviews
|> Stream.map(fn {k, v} -> {k, String.split(v)} end)
|> Stream.map(fn {k, v} -> {k, Enum.filter(v, &(&1 in special_words))} end)
|> Stream.map(fn {k, v} -> {k, Enum.frequencies(v)} end)
|> Stream.map(fn {k, v} -> Map.put(v, "id", k) end)
|> Enum.to_list()

This will yield something like this:

[
  %{"definitely" => 1, "id" => "July 03, 2021"},
  %{"id" => "July 03, 2021"},
  %{"amazing" => 1, "extremely" => 1, "id" => "July 03, 2021"}
]

The structure is a bit different from what you asked but you can get a rough idea on how you are able to transform your data into the desired result with Elixir.

Cheers!

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement