File size: 3,641 Bytes
b261299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f426013
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b261299
f426013
 
 
 
 
b261299
 
f426013
 
 
 
 
 
 
 
 
 
 
 
b261299
 
f426013
 
b261299
 
 
f426013
 
b261299
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""STAN small dataset by Bansal et al.."""

import datasets
import pandas as pd

_CITATION = """

@misc{bansal2015deep,

      title={Towards Deep Semantic Analysis Of Hashtags}, 

      author={Piyush Bansal and Romil Bansal and Vasudeva Varma},

      year={2015},

      eprint={1501.03210},

      archivePrefix={arXiv},

      primaryClass={cs.IR}

}

"""

_DESCRIPTION = """

Manually Annotated Stanford Sentiment Analysis Dataset by Bansal et al..

"""
_URLS = {
    "test": "https://github.com/prashantkodali/HashSet/raw/master/datasets/stan-small-bansal_et_al.pkl"
}

class StanSmall(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "index": datasets.Value("int32"),
                    "hashtag": datasets.Value("string"),
                    "segmentation": datasets.Value("string"),
                    "alternatives": datasets.Sequence(
                        {
                            "segmentation": datasets.Value("string")
                        }
                    )
                }
            ),
            supervised_keys=None,
            homepage="https://github.com/mounicam/hashtag_master",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        downloaded_files = dl_manager.download(_URLS)
        return [
            datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"] }),
        ]

    def _generate_examples(self, filepath):

        def get_segmentation(row):
            needle = row["hashtags"]
            haystack = row["goldtruths"][0].strip()
            output = ""
            iterator = iter(haystack)
            for char in needle:
                output += char
                while True:
                    try:
                        next_char = next(iterator)
                        if next_char.lower() == char.lower():
                            break
                        elif next_char.isspace():
                            output = output[0:-1] + next_char + output[-1]
                    except StopIteration:
                        break
            return output

        def get_alternatives(row, segmentation):
            alts = list(set([x.strip() for x in row["goldtruths"]]))
            alts = [x for x in alts if x != segmentation]
            alts = [{"segmentation": x} for x in alts]
            return alts

        with open(filepath, 'rb') as f:
            try:
                import pickle
                records = pickle.load(f)
            except ValueError:
                try:
                    import pickle5 as pickle
                    records = pickle.load(f)
                except ModuleNotFoundError:
                    raise ImportError(
                        """To be able to use stan_small, you need to install the following dependencies['pickle5'] 

                        using 'pip install pickle5' for instance"""
                    )
        records = records.to_dict("records")
        for idx, row in enumerate(records):
            segmentation = get_segmentation(row)
            alternatives = get_alternatives(row, segmentation)
            yield idx, {
                "index": idx,
                "hashtag": row["hashtags"],
                "segmentation": segmentation,
                "alternatives": alternatives
            }