File size: 4,862 Bytes
05cd399
 
 
 
 
 
6ff8284
05cd399
 
 
 
 
61f309d
 
 
05cd399
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d857d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
041a858
4d857d2
 
 
 
 
 
 
 
 
 
 
 
 
05cd399
 
 
 
041a858
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05cd399
cbd10a7
05cd399
 
 
 
 
 
 
abe9b79
05cd399
 
 
32b61af
05cd399
c9b3a0a
343278c
598a076
e73c2c5
598a076
 
 
 
 
 
 
4d857d2
e73c2c5
343278c
 
 
 
598a076
05cd399
041a858
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# /home/perk/mymodel/categorisation-mt5x/tasks.py

import functools
import seqio
import tensorflow_datasets as tfds
from t5.evaluation import metrics
import my_metrics
from t5.data import preprocessors
import t5
import tensorflow.compat.v1 as tf

tsv_path = {
        "train": "gs://eu-jav-t5x/corpus/labeled/datasetA_train_3categories.tsv",
        "validation": "gs://eu-jav-t5x/corpus/labeled/datasetA_dev_3categories.tsv",
        "test": "gs://eu-jav-t5x/corpus/labeled/ datasetA_test_3categories.tsv"
}

vocabulary = seqio.SentencePieceVocabulary(
                'gs://t5-data/vocabs/mc4.250000.100extra/sentencepiece.model', extra_ids=0)

DEFAULT_OUTPUT_FEATURES = {
    "inputs":
        seqio.Feature(
            vocabulary=vocabulary, add_eos=True),
    "targets":
        seqio.Feature(
            vocabulary=vocabulary, add_eos=True)
}

def categorise_preprocessor(ds):
  def normalize_text(text):
    """Lowercase and remove quotes from a TensorFlow string."""
    text = tf.strings.regex_replace(text,"'(.*)'", r"\1")
    return text

  def to_inputs_and_targets(ex):
    """Map {"source": ..., "source": ...}->{"target": ..., "target": ...}."""
    return {
        "inputs":
             tf.strings.join(
                 [normalize_text(ex["source"])]),
        "targets": 
	    tf.strings.join(
                 [normalize_text(ex["target"])]),
    }

  return ds.map(to_inputs_and_targets, 
                num_parallel_calls=tf.data.experimental.AUTOTUNE)


def categorise_fulltext_preprocessor(ds):
  def normalize_text(text):
    """Lowercase and remove quotes from a TensorFlow string."""
    text = tf.strings.regex_replace(text,"'(.*)'", r"\1")
    return text

  def fulltext(t):
    if t=="0":
        t="il testo è favorevole alla vaccinazione"
    elif t=="1":
        t="il testo è neutro rispetto alla vaccinazione"
    elif t=="2":
        t="is testo è sfavorevole alla vaccinazione"
    return t

  def to_inputs_and_targets(ex):
    """Map {"source": ..., "source": ...}->{"target": ..., "target": ...}."""
    return {
        "inputs":
             tf.strings.join(
                 [normalize_text(ex["source"])]),
        "targets": 
	    tf.strings.join(
                 [fulltext(normalize_text(ex["target"]))]),
    }
  return ds.map(to_inputs_and_targets, 
                num_parallel_calls=tf.data.experimental.AUTOTUNE)


def categorise_fulltext_word_preprocessor(ds):
  def normalize_text(text):
    """Lowercase and remove quotes from a TensorFlow string."""
    text = tf.strings.regex_replace(text,"'(.*)'", r"\1")
    return text

  def fulltext(t):
    if t=="0":
        t="promozionale"
    elif t=="1":
        t="neutro"
    elif t=="2":
        t="scoraggiante"
    return t

  def to_inputs_and_targets(ex):
    """Map {"source": ..., "source": ...}->{"target": ..., "target": ...}."""
    return {
        "inputs":
             tf.strings.join(
                 [normalize_text(ex["source"])]),
        "targets": 
	    tf.strings.join(
                 [fulltext(normalize_text(ex["target"]))]),
    }
  return ds.map(to_inputs_and_targets, 
                num_parallel_calls=tf.data.experimental.AUTOTUNE)


seqio.TaskRegistry.add(
    "classify_tweets",
    source=seqio.TextLineDataSource(
        split_to_filepattern=tsv_path,
        #num_input_examples=num_nq_examples
        ),
    preprocessors=[
      functools.partial(
          t5.data.preprocessors.parse_tsv,
          field_names=["annotator1","annotator2","annotator3","target","source","id"]),
      categorise_preprocessor,
      seqio.preprocessors.tokenize_and_append_eos,
    ],
    metric_fns=[metrics.accuracy,my_metrics.f1_macro],
    output_features=DEFAULT_OUTPUT_FEATURES,
)

seqio.TaskRegistry.add(
    "classify_tweets_fulltext",
    source=seqio.TextLineDataSource(
        split_to_filepattern=tsv_path,
        #num_input_examples=num_nq_examples
        ),
    preprocessors=[
      functools.partial(
          t5.data.preprocessors.parse_tsv,
          field_names=["annotator1","annotator2","annotator3","target","source","id"]),
      categorise_fulltext_preprocessor,
      seqio.preprocessors.tokenize_and_append_eos,
    ],
    metric_fns=[metrics.accuracy,my_metrics.f1_macro],
    output_features=DEFAULT_OUTPUT_FEATURES,
)

seqio.TaskRegistry.add(
    "classify_tweets_fulltext_word",
    source=seqio.TextLineDataSource(
        split_to_filepattern=tsv_path,
        #num_input_examples=num_nq_examples
        ),
    preprocessors=[
      functools.partial(
          t5.data.preprocessors.parse_tsv,
          field_names=["annotator1","annotator2","annotator3","target","source","id"]),
      categorise_fulltext_word_preprocessor,
      seqio.preprocessors.tokenize_and_append_eos,
    ],
    metric_fns=[metrics.accuracy,my_metrics.f1_macro],
    output_features=DEFAULT_OUTPUT_FEATURES,
)