Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pp entities["person"]
entities["person"].first.text
# => "Tim Cook"

entities["person"].first.confidence
entities["person"].first.probability
# => 92.4

entities["person"].first.offsets
Expand Down Expand Up @@ -78,7 +78,7 @@ pp result
result["sentiment"].label
# => "negative"

result["sentiment"].confidence
result["sentiment"].probability
# => 87.1
```

Expand Down
2 changes: 1 addition & 1 deletion SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ result = model[text]
result["sentiment"].label
# => "negative"

result["sentiment"].confidence
result["sentiment"].probability
# => 87.1
```

Expand Down
4 changes: 2 additions & 2 deletions lib/gliner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Gliner
:text_len
)

Entity = Data.define(:index, :offsets, :text, :name, :confidence) do
Entity = Data.define(:index, :offsets, :text, :name, :probability) do
def to_s = text.to_s
def to_str = text.to_s
end
Expand All @@ -43,7 +43,7 @@ def list
end
end

Label = Data.define(:label, :confidence) do
Label = Data.define(:label, :probability) do
def to_s = label.to_s
def to_str = label.to_s
end
Expand Down
18 changes: 9 additions & 9 deletions lib/gliner/classifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def classification_scores(logits, labels, label_positions, prepared)
end
end

def format_classification(scores, labels:, multi_label:, include_confidence:, cls_threshold:)
def format_classification(scores, labels:, multi_label:, include_probability:, cls_threshold:)
label_scores = sorted_label_scores(scores, labels)

return format_multi_label(label_scores, cls_threshold, include_confidence) if multi_label
return format_multi_label(label_scores, cls_threshold, include_probability) if multi_label

format_single_label(label_scores.first, include_confidence)
format_single_label(label_scores.first, include_probability)
end

private
Expand All @@ -44,27 +44,27 @@ def sorted_label_scores(scores, labels)
.sort_by { |(_label, score)| -score }
end

def format_multi_label(label_scores, cls_threshold, include_confidence)
def format_multi_label(label_scores, cls_threshold, include_probability)
chosen = labels_above_threshold(label_scores, cls_threshold)

chosen
.sort_by { |(_label, score)| -score }
.map { |label, score| format_label(label, score, include_confidence) }
.map { |label, score| format_label(label, score, include_probability) }
end

def labels_above_threshold(label_scores, threshold)
above = label_scores.select { |_label, score| score >= threshold }
above.empty? && label_scores.first ? [label_scores.first] : above
end

def format_single_label(label_score, include_confidence)
def format_single_label(label_score, include_probability)
label, score = label_score

format_label(label, score, include_confidence)
format_label(label, score, include_probability)
end

def format_label(label, score, _include_confidence)
Gliner::Label.new(label: label, confidence: score * 100.0)
def format_label(label, score, _include_probability)
Gliner::Label.new(label: label, probability: score * 100.0)
end
end
end
12 changes: 6 additions & 6 deletions lib/gliner/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,40 +101,40 @@ def json_task

def extract_entities(text, entity_types, **options)
threshold = options.fetch(:threshold, Gliner.config.threshold)
include_confidence = options.fetch(:include_confidence, false)
include_probability = options.fetch(:include_probability, false)
include_spans = options.fetch(:include_spans, false)

pipeline.execute(
entity_task,
text,
entity_types,
threshold: threshold,
include_confidence: include_confidence,
include_probability: include_probability,
include_spans: include_spans
)
end

def classify_text(text, tasks, **options)
include_confidence = options.fetch(:include_confidence, false)
include_probability = options.fetch(:include_probability, false)
threshold = options[:threshold]

task_options = { include_confidence: include_confidence }
task_options = { include_probability: include_probability }
task_options[:threshold] = threshold unless threshold.nil?

classification_task.execute_all(pipeline, text, tasks, **task_options)
end

def extract_json(text, structures, **options)
threshold = options.fetch(:threshold, Gliner.config.threshold)
include_confidence = options.fetch(:include_confidence, false)
include_probability = options.fetch(:include_probability, false)
include_spans = options.fetch(:include_spans, false)

json_task.execute_all(
pipeline,
text,
structures,
threshold: threshold,
include_confidence: include_confidence,
include_probability: include_probability,
include_spans: include_spans
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gliner/span_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def format_span(span, _opts, label:, index:)
offsets: [span.start, span.end],
text: span.text,
name: label&.to_s,
confidence: span.score * 100.0
probability: span.score * 100.0
)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/gliner/tasks/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def needs_cls_logits?
end

def process_output(logits, parsed, prepared, options)
include_confidence = options.fetch(:include_confidence, false)
include_probability = options.fetch(:include_probability, false)
threshold_override = options[:threshold]
cls_threshold = threshold_override.nil? ? parsed[:cls_threshold] : threshold_override

Expand All @@ -47,7 +47,7 @@ def process_output(logits, parsed, prepared, options)
scores,
labels: parsed[:labels],
multi_label: parsed[:multi_label],
include_confidence: include_confidence,
include_probability: include_probability,
cls_threshold: cls_threshold
)
end
Expand Down
18 changes: 9 additions & 9 deletions spec/gliner/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
Gliner::Span.new(text: 'Cook', score: 0.7, start: 4, end: 8)
]

out = span_extractor.format_spans(spans, include_confidence: false, include_spans: false, label: 'person')
out = span_extractor.format_spans(spans, include_probability: false, include_spans: false, label: 'person')
expect(out.map(&:text)).to eq(['Tim Cook'])
expect(out.first).to eq(
Gliner::Entity.new(
index: 0,
offsets: [0, 8],
text: 'Tim Cook',
name: 'person',
confidence: 90.0
probability: 90.0
)
)
end
Expand Down Expand Up @@ -99,7 +99,7 @@
}

instances = structured_extractor
.build_structure_instances(parsed_fields, spans_by_label, include_confidence: false, include_spans: false)
.build_structure_instances(parsed_fields, spans_by_label, include_probability: false, include_spans: false)

expect(instances.length).to eq(2)
expect(instances[0]).to eq(
Expand All @@ -109,21 +109,21 @@
offsets: [0, 5],
text: 'Jan 5',
name: 'date',
confidence: 90.0
probability: 90.0
),
'merchant' => Gliner::Entity.new(
index: 0,
offsets: [7, 16],
text: 'Starbucks',
name: 'merchant',
confidence: 90.0
probability: 90.0
),
'amount' => Gliner::Entity.new(
index: 0,
offsets: [17, 22],
text: '$5.50',
name: 'amount',
confidence: 90.0
probability: 90.0
)
})
)
Expand All @@ -134,21 +134,21 @@
offsets: [40, 45],
text: 'Jan 6',
name: 'date',
confidence: 80.0
probability: 80.0
),
'merchant' => Gliner::Entity.new(
index: 0,
offsets: [47, 53],
text: 'Amazon',
name: 'merchant',
confidence: 70.0
probability: 70.0
),
'amount' => Gliner::Entity.new(
index: 0,
offsets: [54, 61],
text: '$156.99',
name: 'amount',
confidence: 80.0
probability: 80.0
)
})
)
Expand Down
2 changes: 1 addition & 1 deletion spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

expect(sentiment).to be_a(Gliner::Label)
expect(sentiment.label).to eq('negative')
expect(sentiment.confidence).to be_a(Float)
expect(sentiment.probability).to be_a(Float)
end
end

Expand Down