Class: Broadlistening::Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/broadlistening/argument.rb

Overview

Represents an extracted argument (opinion) from a comment.

Arguments are created during the extraction step and enriched through subsequent pipeline steps (embedding, clustering).

Examples:

Creating an argument

arg = Argument.new(arg_id: "A1_0", argument: "We need more parks", comment_id: "1")
arg.embedding = [0.1, 0.2, 0.3]  # Added by embedding step
arg.x = 0.5                       # Added by clustering step

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg_id:, argument:, comment_id:, embedding: nil, x: nil, y: nil, cluster_ids: nil, attributes: nil, url: nil, properties: nil) ⇒ Argument



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/broadlistening/argument.rb', line 18

def initialize(
  arg_id:,
  argument:,
  comment_id:,
  embedding: nil,
  x: nil,
  y: nil,
  cluster_ids: nil,
  attributes: nil,
  url: nil,
  properties: nil
)
  @arg_id = arg_id
  @argument = argument
  @comment_id = comment_id
  @embedding = embedding
  @x = x
  @y = y
  @cluster_ids = cluster_ids
  @attributes = attributes
  @url = url
  @properties = properties
end

Instance Attribute Details

#arg_idObject

Returns the value of attribute arg_id.



14
15
16
# File 'lib/broadlistening/argument.rb', line 14

def arg_id
  @arg_id
end

#argumentObject

Returns the value of attribute argument.



14
15
16
# File 'lib/broadlistening/argument.rb', line 14

def argument
  @argument
end

#attributesObject

Returns the value of attribute attributes.



14
15
16
# File 'lib/broadlistening/argument.rb', line 14

def attributes
  @attributes
end

#cluster_idsObject

Returns the value of attribute cluster_ids.



14
15
16
# File 'lib/broadlistening/argument.rb', line 14

def cluster_ids
  @cluster_ids
end

#comment_idObject

Returns the value of attribute comment_id.



14
15
16
# File 'lib/broadlistening/argument.rb', line 14

def comment_id
  @comment_id
end

#embeddingObject

Returns the value of attribute embedding.



14
15
16
# File 'lib/broadlistening/argument.rb', line 14

def embedding
  @embedding
end

#propertiesObject

Returns the value of attribute properties.



14
15
16
# File 'lib/broadlistening/argument.rb', line 14

def properties
  @properties
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/broadlistening/argument.rb', line 14

def url
  @url
end

#xObject

Returns the value of attribute x.



14
15
16
# File 'lib/broadlistening/argument.rb', line 14

def x
  @x
end

#yObject

Returns the value of attribute y.



14
15
16
# File 'lib/broadlistening/argument.rb', line 14

def y
  @y
end

Class Method Details

.from_comment(comment, opinion_text, index) ⇒ Argument

Create an Argument from a Comment during extraction



67
68
69
70
71
72
73
74
75
76
# File 'lib/broadlistening/argument.rb', line 67

def self.from_comment(comment, opinion_text, index)
  new(
    arg_id: "A#{comment.id}_#{index}",
    argument: opinion_text,
    comment_id: comment.id,
    attributes: comment.attributes,
    url: comment.source_url,
    properties: comment.properties
  )
end

.from_hash(hash) ⇒ Argument

Create an Argument from a hash



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/broadlistening/argument.rb', line 46

def self.from_hash(hash)
  new(
    arg_id: hash[:arg_id] || hash["arg_id"],
    argument: hash[:argument] || hash["argument"],
    comment_id: hash[:comment_id] || hash["comment_id"],
    embedding: hash[:embedding] || hash["embedding"],
    x: hash[:x] || hash["x"],
    y: hash[:y] || hash["y"],
    cluster_ids: hash[:cluster_ids] || hash["cluster_ids"],
    attributes: hash[:attributes] || hash["attributes"],
    url: hash[:url] || hash["url"],
    properties: hash[:properties] || hash["properties"]
  )
end

Instance Method Details

#comment_id_intInteger

Extract numeric comment_id from arg_id if comment_id is not set



129
130
131
132
133
134
# File 'lib/broadlistening/argument.rb', line 129

def comment_id_int
  return @comment_id.to_i if @comment_id

  match = @arg_id&.match(/\AA(\d+)_/)
  match ? match[1].to_i : 0
end

#in_cluster?(cluster_id) ⇒ Boolean

Check if argument belongs to a specific cluster



122
123
124
# File 'lib/broadlistening/argument.rb', line 122

def in_cluster?(cluster_id)
  @cluster_ids&.include?(cluster_id) || false
end

#to_clustering_hHash

Convert to hash with only clustering data (for clustering.json)



109
110
111
112
113
114
115
116
# File 'lib/broadlistening/argument.rb', line 109

def to_clustering_h
  {
    arg_id: @arg_id,
    x: @x,
    y: @y,
    cluster_ids: @cluster_ids
  }
end

#to_embedding_hHash

Convert to hash with only embedding data (for embeddings.json)



99
100
101
102
103
104
# File 'lib/broadlistening/argument.rb', line 99

def to_embedding_h
  {
    arg_id: @arg_id,
    embedding: @embedding
  }
end

#to_hHash

Convert to hash for serialization



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/broadlistening/argument.rb', line 81

def to_h
  {
    arg_id: @arg_id,
    argument: @argument,
    comment_id: @comment_id,
    embedding: @embedding,
    x: @x,
    y: @y,
    cluster_ids: @cluster_ids,
    attributes: @attributes,
    url: @url,
    properties: @properties
  }.compact
end