Skip to main content

Source Label Map

Converts news sources into human-friendly labels

Quickstart

Installation

Install the package with pip:

pip install -U bilbyai-source-label-map

Usage

Using get_source_labels directly

from bilbyai.source_label_map import get_source_labels

get_source_labels("ben")
# 'Beijing Evening News'

get_source_labels(["gmwnews", "ben"]) 
# ['Enlightenment Daily', 'Beijing Evening News']

# Since the key 'cannot-be-found' is not in the dictionary, it returns the original input.
get_source_labels("cannot-be-found")
get_source_labels("cannot-be-found", when_not_found="preserve") # this is the default.
# 'cannot-be-found'

# When the option is set to 'set_none', the function returns None when the key is not found.
get_source_labels("cannot-be-found", when_not_found="set_none")
# None

# When the option is set to 'set_unknown', the function returns the string "unknown".
get_source_labels("cannot-be-found", when_not_found="set_unknown") 
# 'unknown'

# When the option is set to 'raise_error', the function raises a ValueError.
get_source_labels("ben", when_not_found="raise_error") 
# raises ValueError

Using get_source_labels with DataFrames

import pandas as pd
from bilbyai.source_label_map import get_source_labels

df = pd.DataFrame(
    {
        "source": [
            "gmwnews",
            "ben",
            "cannot-be-found",
            "zqrb",
        ]
    }
)

# Option A: using list comprehension
df["source_label"] = get_source_labels(df["source"])

# Option B: using apply
df["source_label"] = df["source"].apply(get_source_labels)

df["source_label"]
# Outputs: 
# 0     Enlightenment Daily
# 1    Beijing Evening News
# 2         cannot-be-found
# 3        Securities Daily
# Name: source_label, dtype: object

Function Specs

Get source labels for a list of inputs.

Args

source (str | Iterable[str]): The string or list of strings to get source labels for.

when_not_found: The action to take when a source label is not found. Set to "preserve_source_name" by default.

  • "preserve": Preserve the source name as the source label.
  • "set_none": Set the source label to None.
  • "set_unknown": Set the source label to "unknown".
  • "raise_error": Raise an error if a source label is not found.

source_label_dict: A dictionary mapping source names to source labels. Set this value to override the default source label dictionary. It should be a dictionary mapping source names to source labels, like this:

source_label_dict = {
    "gmwnews": "Enlightenment Daily",
    "sina_news": "Sina News",
    "xueqiu": "Xueqiu (Snowball Finance)",
    "peoplenews": "People's Daily",
}

Returns

A list of source labels for the inputs.

Raises

ValueError: If the when_not_found value is not recognized. ValueError: If the inputs are not a string or iterable of strings. ValueError: If when_not_found is set to "raise_error" and a source label is not found.

The project owner is @leetdavid.

Development

If not already in a virtual environement, create and use one. Read about it in the Python documentation: venv — Creation of virtual environments.

python3 -m venv .venv
source .venv/bin/activate

Install the pinned pip version:

pip install -r $(git rev-parse --show-toplevel)/pip-requirements.txt

Finally, install the dependencies:

pip install -r $(git rev-parse --show-toplevel)/dev-requirements.txt -r requirements.txt

Testing

Execute tests from the library's folder (after having loaded the virtual environment, see above) as follows:

python3 -m pytest tests/

Execute the library's CI locally with act as follows:

act -j ci-libs-source_label_map

Metadata

Release files for bilbyai-source-label-map 0.2.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bilbyai-source-label-map 0.2.8
File Size Uploaded Provenance Actions
bilbyai_source_label_map-0.2.8.tar.gz 14.3 kB Without provenance Details for bilbyai_source_label_map-0.2.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bilbyai-source-label-map 0.2.8
File Interpreter ABI Platform Actions
bilbyai_source_label_map-0.2.8-py3-none-any.whl Python 3 none any Details for bilbyai_source_label_map-0.2.8-py3-none-any.whl

Total release size: 28.1 kB

Release files / bilbyai_source_label_map-0.2.8.tar.gz

Download URL bilbyai_source_label_map-0.2.8.tar.gz
Size 14.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1145e8f9ab308ff23968789c900fb34dedf862e54dab3e088b8bfbd5fe9d52ec
BLAKE2b-256 checksum
How to use checksums
97adbb772f81526390571edffdb181927c498bdc8f1c1ccfe76b9185cd075c5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / bilbyai_source_label_map-0.2.8-py3-none-any.whl

Download URL bilbyai_source_label_map-0.2.8-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f741e9259e2138d4ec312093e0444b0a1596f09151c439e44f5e9630124aef54
BLAKE2b-256 checksum
How to use checksums
760e60b035ac69e92b1899764bfd6c878e3768016decfa41d94763422c8fda37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for bilbyai-source-label-map 0.2.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page