Skip to content

Commit

Permalink
add relevance to cp ninjs output (#239)
Browse files Browse the repository at this point in the history
SDCP-714
  • Loading branch information
petrjasek authored May 14, 2024
1 parent 0cbf813 commit 09fa463
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 14 deletions.
22 changes: 17 additions & 5 deletions client/extensions/auto-tagger/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export interface ITagBase {
altids: {[key: string]: string};
aliases?: Array<string>;
original_source?: string;
creator?: string;
relevance?: number;
}

export interface ISubjectTag extends ITagBase {
Expand Down Expand Up @@ -44,11 +46,11 @@ export function toClientFormat(response: IServerResponse): OrderedMap<string, IT
let tags = OrderedMap<string, ITagUi>();

response.subject?.forEach((item) => {
const {name, description, qcode, source, altids, aliases, original_source, parent, scheme} = item;
const {name, description, qcode, source, altids, aliases, original_source, parent, scheme, relevance, creator} = item;

// Checking if the item has original_source to filter auto tagger tags
if (original_source != null) {
if(scheme === 'http://cv.iptc.org/newscodes/mediatopic/' || scheme === 'subject') {
if (scheme === 'http://cv.iptc.org/newscodes/mediatopic/' || scheme === 'subject') {
const tag: ITagUi = {
name,
description,
Expand All @@ -62,6 +64,8 @@ export function toClientFormat(response: IServerResponse): OrderedMap<string, IT
kind: 'scheme',
value: 'subject' || '',
},
relevance,
creator,
};

tags = tags.set(tag.qcode, tag);
Expand All @@ -79,6 +83,8 @@ export function toClientFormat(response: IServerResponse): OrderedMap<string, IT
kind: 'scheme',
value: item.scheme || '',
},
relevance,
creator,
};

tags = tags.set(tag.qcode, tag);
Expand Down Expand Up @@ -110,7 +116,7 @@ export function toClientFormat(response: IServerResponse): OrderedMap<string, IT

others.forEach(({group, items}) => {
items.forEach((item) => {
const {name, description, qcode, source, altids, aliases, original_source, scheme} = item;
const {name, description, qcode, source, altids, aliases, original_source, scheme, relevance, creator} = item;

const tag: ITagUi = {
name,
Expand All @@ -125,6 +131,8 @@ export function toClientFormat(response: IServerResponse): OrderedMap<string, IT
kind: 'visual',
value: group,
},
relevance,
creator,
};

if (!tags.has(tag.name)) {
Expand All @@ -149,7 +157,7 @@ export function toServerFormat(items: OrderedMap<string, ITagUi>, superdesk: ISu
result.subject = [];
}

const {name, description, qcode, source, altids, aliases, original_source, parent} = item;
const {name, description, qcode, source, altids, aliases, original_source, parent, relevance, creator} = item;

const subjectTag: ISubjectTag = {
name,
Expand All @@ -161,6 +169,8 @@ export function toServerFormat(items: OrderedMap<string, ITagUi>, superdesk: ISu
scheme: item.group.value,
aliases,
original_source,
relevance,
creator,
};

result.subject.push(subjectTag);
Expand All @@ -171,7 +181,7 @@ export function toServerFormat(items: OrderedMap<string, ITagUi>, superdesk: ISu
result[groupValue] = [];
}

const {name, description, qcode, source, altids, aliases, original_source, scheme} = item;
const {name, description, qcode, source, altids, aliases, original_source, scheme, relevance, creator} = item;

const tagBase: ITagBase = {
name,
Expand All @@ -182,6 +192,8 @@ export function toServerFormat(items: OrderedMap<string, ITagUi>, superdesk: ISu
aliases,
original_source,
scheme,
relevance,
creator,
};

result[groupValue]!.push(tagBase);
Expand Down
2 changes: 2 additions & 0 deletions client/extensions/auto-tagger/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export interface ITagUi {
/** The identifier of a controlled vocabulary which includes a code for the tag. */
scheme?: string;
group: {kind: 'scheme' | 'visual'; value: string};
creator?: string;
relevance?: number;
}
16 changes: 11 additions & 5 deletions server/cp/ai/semaphore.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
TIMEOUT = (5, 30)


def format_relevance(value: str) -> int:
if value:
return int(float(value) * 100)
return 100


ResponseType = Mapping[str, Union[str, List[str]]]


Expand Down Expand Up @@ -121,7 +127,7 @@ def fetch_parent_info(self, qcode):
{
"name": field.get("NAME"),
"qcode": field.get("ID"),
"relevance": score,
"relevance": format_relevance(score),
"parent": None, # Set to None initially
}
)
Expand Down Expand Up @@ -195,7 +201,7 @@ def transform_xml_response(api_response):
"qcode": item["id"],
"source": "Semaphore",
"creator": "Human",
"relevance": score,
"relevance": int(score),
"altids": {"source_name": "source_id"},
"original_source": "original_source_value",
"scheme": scheme_url,
Expand Down Expand Up @@ -238,7 +244,7 @@ def transform_xml_response(api_response):
),
"creator": "Human",
"source": "Semaphore",
"relevance": "100",
"relevance": format_relevance("100"),
"altids": {"source_name": "source_id"},
"original_source": "original_source_value",
"scheme": "http://cv.iptc.org/newscodes/mediatopic/",
Expand Down Expand Up @@ -553,7 +559,7 @@ def adjust_score(score, existing_scores):
"qcode": meta_id if meta_id else "",
"creator": "Machine",
"source": "Semaphore",
"relevance": meta_score,
"relevance": format_relevance(meta_score),
"altids": f'{{"{meta_value}": "{meta_id}"}}',
"original_source": "original_source_value",
"scheme": scheme_url,
Expand All @@ -574,7 +580,7 @@ def adjust_score(score, existing_scores):
"parent": parent_qcode,
"source": "Semaphore",
"creator": "Machine",
"relevance": score,
"relevance": format_relevance(score),
"altids": {"source_name": "source_id"},
"original_source": "original_source_value",
"scheme": "http://cv.iptc.org/newscodes/mediatopic/",
Expand Down
4 changes: 4 additions & 0 deletions server/cp/output/formatter/cp_ninjs_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def format_cv_item(item, language):
"code": item.get("qcode"),
"name": get_locale_name(item, language),
"scheme": "http://cv.iptc.org/newscodes/mediatopic/",
"creator": item.get("creator", ""),
"relevance": item.get("relevance", 100),
}
)
else:
Expand All @@ -21,6 +23,8 @@ def format_cv_item(item, language):
"code": item.get("qcode"),
"name": get_locale_name(item, language),
"scheme": item.get("scheme"),
"creator": item.get("creator", ""),
"relevance": item.get("relevance", 100),
}
)

Expand Down
17 changes: 17 additions & 0 deletions server/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import flask
import pytest


@pytest.fixture(autouse=True)
def app():
app = flask.Flask(__name__)
app.config.update(
{
"VERSION": "version",
"DEFAULT_LANGUAGE": "en",
}
)
ctx = app.app_context()
ctx.push()
yield app
ctx.pop()
3 changes: 1 addition & 2 deletions server/tests/orangelogic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ def setUp(self):
self.ctx.push()

def tearDown(self):
if hasattr(self, "ctx"):
self.ctx.pop()
self.ctx.pop()

def test_instance(self):
OrangelogicSearchProvider(self.provider)
Expand Down
6 changes: 5 additions & 1 deletion server/tests/output/formatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ def setUp(self):
"DEFAULT_LANGUAGE": "en",
}
)
self.app.app_context().push()
self.ctx = self.app.app_context()
self.ctx.push()

def tearDown(self):
self.ctx.pop()

def format(self, updates=None, _all=False):
article = self.article.copy()
Expand Down
26 changes: 26 additions & 0 deletions server/tests/output/formatter/cp_ninjs_formatter_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from unittest.mock import patch

from cp.output.formatter.cp_ninjs_formatter import CPNINJSFormatter


@patch("superdesk.get_resource_service")
def test_subject_relevance(mock):
item = {
"type": "text",
"subject": [
{
"name": "Society",
"qcode": "eef4a135-e188-4d1f-93f1-cf7af1f594a6",
"source": "Semaphore",
"altids": {"source_name": "source_id"},
"scheme": "subject",
"original_source": "original_source_value",
"relevance": 54,
"creator": "Machine",
},
],
}
formatter = CPNINJSFormatter()
ninjs = formatter._transform_to_ninjs(item, {})
assert ninjs["subject"][0]["relevance"] == 54
assert ninjs["subject"][0]["creator"] == "Machine"
7 changes: 6 additions & 1 deletion server/tests/set_provice_on_publish_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ def setUp(self) -> None:
"DEFAULT_LANGUAGE": "en",
}
)
self.app.app_context().push()
self.ctx = self.app.app_context()
self.ctx.push()

def tearDown(self) -> None:
super().tearDown()
self.ctx.pop()

def test_publish_signal(self):
with patch.dict(superdesk.resources, resources):
Expand Down

0 comments on commit 09fa463

Please sign in to comment.