wuenlp_tools.utils.nrc

 1from pathlib import Path
 2from tempfile import TemporaryDirectory
 3
 4import pandas as pd
 5
 6from wuenlp_tools.utils import ensure_resource_path, resolve_resource_path, seed_resource_from_package
 7from wuenlp_tools.utils.sentiment import SentimentLexicon
 8
 9_NRC_PARTS = ("sentiment", "NRC-Emotion-Lexicon-German.csv")
10
11
12def download_nrc_lexicon(dest: Path) -> None:
13    """Download the NRC lexicon and store it at dest."""
14    import io
15    import zipfile
16
17    import requests
18
19    url = "https://saifmohammad.com/WebDocs/Lexicons/NRC-Emotion-Lexicon.zip"
20    headers = {
21        "User-Agent": (
22            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
23            "(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
24        )
25    }
26
27    response = requests.get(url, headers=headers)
28    response.raise_for_status()
29    file = io.BytesIO(response.content)
30
31    dest.parent.mkdir(parents=True, exist_ok=True)
32    with zipfile.ZipFile(file, "r") as zip_ref:
33        with TemporaryDirectory() as temp_dir:
34            zip_ref.extractall(temp_dir)
35            for extracted in Path(temp_dir).glob("**/*"):
36                if extracted.name == "German-NRC-EmoLex.txt":
37                    lexicon = pd.read_csv(extracted, delimiter="\t", header=0, decimal=",")
38                    lexicon.to_csv(dest, index=False)
39                    return
40    raise FileNotFoundError("Could not find the NRC lexicon in the downloaded zip file")
41
42
43def get_nrc_lexicon_path() -> Path:
44    """Resolve NRC CSV from user cache, shipped package, or download into user cache."""
45    seeded = seed_resource_from_package(*_NRC_PARTS)
46    if seeded.is_file():
47        return seeded
48    resolved = resolve_resource_path(*_NRC_PARTS)
49    if resolved.is_file():
50        return resolved
51    dest = ensure_resource_path(*_NRC_PARTS)
52    download_nrc_lexicon(dest)
53    return dest
54
55
56german_nrc = SentimentLexicon(pd.read_csv(get_nrc_lexicon_path(), index_col="German Word"))
57
58if __name__ == "__main__":
59    print(german_nrc.lexicon.head())
def download_nrc_lexicon(dest: pathlib.Path) -> None:
13def download_nrc_lexicon(dest: Path) -> None:
14    """Download the NRC lexicon and store it at dest."""
15    import io
16    import zipfile
17
18    import requests
19
20    url = "https://saifmohammad.com/WebDocs/Lexicons/NRC-Emotion-Lexicon.zip"
21    headers = {
22        "User-Agent": (
23            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
24            "(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
25        )
26    }
27
28    response = requests.get(url, headers=headers)
29    response.raise_for_status()
30    file = io.BytesIO(response.content)
31
32    dest.parent.mkdir(parents=True, exist_ok=True)
33    with zipfile.ZipFile(file, "r") as zip_ref:
34        with TemporaryDirectory() as temp_dir:
35            zip_ref.extractall(temp_dir)
36            for extracted in Path(temp_dir).glob("**/*"):
37                if extracted.name == "German-NRC-EmoLex.txt":
38                    lexicon = pd.read_csv(extracted, delimiter="\t", header=0, decimal=",")
39                    lexicon.to_csv(dest, index=False)
40                    return
41    raise FileNotFoundError("Could not find the NRC lexicon in the downloaded zip file")

Download the NRC lexicon and store it at dest.

def get_nrc_lexicon_path() -> pathlib.Path:
44def get_nrc_lexicon_path() -> Path:
45    """Resolve NRC CSV from user cache, shipped package, or download into user cache."""
46    seeded = seed_resource_from_package(*_NRC_PARTS)
47    if seeded.is_file():
48        return seeded
49    resolved = resolve_resource_path(*_NRC_PARTS)
50    if resolved.is_file():
51        return resolved
52    dest = ensure_resource_path(*_NRC_PARTS)
53    download_nrc_lexicon(dest)
54    return dest

Resolve NRC CSV from user cache, shipped package, or download into user cache.