Skip to content

Instantly share code, notes, and snippets.

@kle91203
Last active November 1, 2017 05:10
Show Gist options
  • Save kle91203/75a27f5ce0b6772fb3a75a88ccc2769b to your computer and use it in GitHub Desktop.
Save kle91203/75a27f5ce0b6772fb3a75a88ccc2769b to your computer and use it in GitHub Desktop.
def add_attr(event_attr, person_attrs) do
new_attr_value = [%Person.AttributeValue{
:timestamp => event_attr.timestamp,
:confidence => event_attr.confidence,
:value => event_attr.value
}]
curr_attr = Map.get(person_attrs, event_attr.field)
if (curr_attr === nil) do #the person doesn't have that attr
# add_new(person_attrs, )
new_attr_source = %{
event_attr.source => new_attr_value
}
Map.update(person_attrs, event_attr.field, new_attr_source, fn(_) -> new_attr_source end)
else #the person has that attr
curr_source = Map.get(curr_attr, event_attr.source)
if (curr_source === nil) do #but they dont have that source
new_field_map = Map.update(curr_attr, event_attr.source, new_attr_value, fn(_) -> new_attr_value end)
Map.update(person_attrs, event_attr.field, new_field_map, fn(_) -> new_field_map end)
else #they have the attr and source
[elem|_] = curr_source
if (event_attr.timestamp > elem.timestamp) do #and this one is newer
new_field_map = Map.update(curr_attr, event_attr.source, new_attr_value, fn(_) -> new_attr_value end)
Map.update(person_attrs, event_attr.field, new_field_map, fn(_) -> new_field_map end)
else #it's older. ignore it.
person_attrs
end
end
end
end
defmodule Person.EventAttributeValue do
@type t :: %{
:field => String.t,
:source => String.t,
:timestamp => integer(),
:confidence => number(),
:value => any()
}
defstruct field: nil, source: nil, timestamp: 0, confidence: 0, value: nil
end
defmodule Person.AttributeValue do
@type t :: %{
:timestamp => integer(),
:confidence => number(),
:value => any()
}
defstruct timestamp: 0, confidence: 0, value: nil
end
defmodule KBS.Person do
@spec add_attr(Person.EventAttributeValue.t, map()) :: map()
def add_attr(
%Person.EventAttributeValue{
:field => event_field,
:source => event_source,
:timestamp => event_timestamp,
:confidence => event_confidence,
:value => event_value
} = event_attr,
%{
"field_name" => field_name =
%{ "source_name" => source_name = [
%Person.AttributeValue{
:timestamp => attr_timestamp,
:confidence => attr_confidence,
:value => attr_value
}
]
}
} = person_attrs) do
IO.puts "event_field: #{event_field}"
IO.puts "event_field: #{event_field}"
IO.puts "event_source: #{event_source}"
IO.puts "event_timestamp: #{event_timestamp}"
IO.puts "event_confidence: #{event_confidence}"
IO.puts "event_value: #{event_value}"
# IO.puts "field_name: #{inspect field_name}"
# IO.puts "source_name: #{source_name}"
# IO.puts "attr_timestamp: #{attr_timestamp}"
# IO.puts "attr_confidence: #{attr_confidence}"
# IO.puts "attr_value: #{attr_value}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment