def BoW_data(datadir='../Logical_Entailment/temp', filename='train.txt'): A, B, y = zip(*data.read_data(os.path.join(datadir, filename))) X_1 = BoW_embedding(A, vocabulary(A)) X_2 = BoW_embedding(B, vocabulary(A)) X = np.concatenate((X_1, X_2), axis=1) y = np.array(y, dtype=np.int32) return X, y
def select_renderer(widget, data): tag = data.tag value = fetch_value(widget, data) if value is None: value = [] if isinstance(value, basestring) or not hasattr(value, '__iter__'): value = [value] if widget.attrs['format'] == 'block': optiontags = [] for key, term in vocabulary(widget.attrs.get('vocabulary', [])): attrs = { 'selected': (key in value) and 'selected' or None, 'value': key, 'id': cssid(widget, 'input', key), } optiontags.append(tag('option', term, **attrs)) select_attrs = { 'name_': widget.dottedpath, 'id': cssid(widget, 'input'), 'class_': cssclasses(widget, data), 'multiple': widget.attrs['multivalued'] and 'multiple' or None, } return tag('select', *optiontags, **select_attrs) else: tags = [] for key, term in vocabulary(widget.attrs.get('vocabulary', [])): if widget.attrs['multivalued']: tagtype = 'checkbox' else: tagtype = 'radio' attrs = { 'type': tagtype, 'value': key, 'checked': (key in value) and 'checked' or None, 'name_': widget.dottedpath, 'id': cssid(widget, 'input', key), 'class_': cssclasses(widget, data), } input = tag('input', **attrs) text = tag('span', term) tags.append(tag('div', input, text, **{'id': cssid(widget, 'radio', key)})) attrs = { 'type': 'hidden', 'value': 'exists', 'name_': "%s-exists" % widget.dottedpath, 'id': cssid(widget, 'exists'), } exists_marker = tag('input', **attrs) return exists_marker + u''.join(tags)
def data(): A_0, B_0, y_0 = zip(*data.read_data('../Logical_Entailment/temp/train.txt')) A_1, B_1, y_1 = zip(*data.read_data('../Logical_Entailment/temp/validate.txt')) y_0, y_1 = np.array(y_0), np.array(y_1) X = [a+'_'+b for a, b in zip(*[A_0,B_0])] X_val = [a+'_'+b for a, b in zip(*[A_1,B_1])] vocab = vocabulary(X) X = to_numeric(X, vocab) X_val = to_numeric(X_val, vocab) maxlen = len(max(*X, key=len)) vocab_size = len(vocab) #print('Pad sequences A, B') X = pad_sequences(X, padding='post', maxlen=maxlen) y = np.array(y_0, dtype=int) X_val = pad_sequences(X_val, padding='post', maxlen=maxlen) y_val = np.array(y_1, dtype=int) return X, y, X_val, y_val, maxlen, vocab_size
def select_display_renderer(widget, data): value = fetch_value(widget, data) if not widget.attrs['multivalued'] or not value: return generic_display_renderer(widget, data) attrs = { 'id': cssid(widget, 'display'), 'class_': 'display-%s' % widget.attrs['class'] or 'generic' } content = u'' vocab = dict(vocabulary(widget.attrs.get('vocabulary', []))) for key in value: content += data.tag('li', vocab[key]) return data.tag('ul', content, **attrs)
def checkbox_display_renderer(widget, data): """Generic display renderer to render a value. """ value = fetch_value(widget, data) if widget.attrs['format'] == 'string' and bool(value): content = value else: vocab = dict(vocabulary(widget.attrs.get('vocabulary', []))) content = vocab[bool(value)] attrs = { 'id': cssid(widget, 'display'), 'class_': 'display-%s' % widget.attrs['class'] or 'generic' } return data.tag('div', content, **attrs)
def file_options_renderer(widget, data): if data.value in [None, UNSET, '']: return data.rendered tag = data.tag if data.request: value = [data.request.get('%s-action' % widget.dottedpath, 'keep')] else: value = ['keep'] tags = [] for key, term in vocabulary(widget.attrs.get('vocabulary', [])): attrs = { 'type': 'radio', 'value': key, 'checked': (key in value) and 'checked' or None, 'name_': '%s-action' % widget.dottedpath, 'id': cssid(widget, 'input', key), 'class_': cssclasses(widget, data), } input = tag('input', **attrs) text = tag('span', term) tags.append( tag('div', input, text, **{'id': cssid(widget, 'radio', key)})) return data.rendered + u''.join(tags)
def file_options_renderer(widget, data): if data.value in [None, UNSET, '']: return data.rendered tag = data.tag if data.request: value = [data.request.get('%s-action' % widget.dottedpath, 'keep')] else: value = ['keep'] tags = [] for key, term in vocabulary(widget.attrs.get('vocabulary', [])): attrs = { 'type': 'radio', 'value': key, 'checked': (key in value) and 'checked' or None, 'name_': '%s-action' % widget.dottedpath, 'id': cssid(widget, 'input', key), 'class_': cssclasses(widget, data), } input = tag('input', **attrs) text = tag('span', term) tags.append(tag('div', input, text, **{'id': cssid(widget, 'radio', key)})) return data.rendered + u''.join(tags)
def select_edit_renderer(widget, data): tag = data.tag value = fetch_value(widget, data) if isinstance(value, basestring) or not hasattr(value, '__iter__'): value = [value] if not widget.attrs['multivalued'] and len(value) > 1: raise ValueError(u"Multiple values for single selection.") disabled = widget.attrs.get('disabled', False) if widget.attrs['format'] == 'block': optiontags = [] for key, term in vocabulary(widget.attrs.get('vocabulary', [])): attrs = { 'selected': (key in value) and 'selected' or None, 'value': key, 'id': cssid(widget, 'input', key), } if disabled and disabled is not True and key in disabled: attrs['disabled'] = 'disabled' optiontags.append(tag('option', term, **attrs)) select_attrs = { 'name_': widget.dottedpath, 'id': cssid(widget, 'input'), 'class_': cssclasses(widget, data), 'multiple': widget.attrs['multivalued'] and 'multiple' or None, 'placeholder': widget.attrs.get('placeholder') or None, 'autofocus': widget.attrs.get('autofocus') and 'autofocus' or None, 'required': widget.attrs.get('required') and 'required' or None, } if disabled is True: select_attrs['disabled'] = 'disabled' rendered = tag('select', *optiontags, **select_attrs) if widget.attrs['multivalued']: attrs = { 'type': 'hidden', 'value': 'exists', 'name_': "%s-exists" % widget.dottedpath, 'id': cssid(widget, 'exists'), } rendered = select_exists_marker(widget, data) + rendered return rendered else: tags = [] label_pos = widget.attrs['listing_label_position'] listing_tag = widget.attrs['listing_tag'] item_tag = listing_tag == 'div' and 'div' or 'li' if widget.attrs['multivalued']: tagtype = 'checkbox' else: tagtype = 'radio' for key, term in vocabulary(widget.attrs.get('vocabulary', [])): attrs = { 'type': tagtype, 'value': key, 'checked': (key in value) and 'checked' or None, 'name_': widget.dottedpath, 'id': cssid(widget, 'input', key), 'class_': cssclasses(widget, data), } if (disabled and disabled is not True and key in disabled) \ or disabled is True: attrs['disabled'] = 'disabled' input = tag('input', **attrs) if label_pos == 'inner': item = tag('label', term, input, for_=attrs['id']) elif label_pos == 'after': item = input + tag('label', term, for_=attrs['id']) else: item = tag('label', term, for_=attrs['id']) + input tags.append( tag(item_tag, item, **{'id': cssid(widget, tagtype, key)})) return select_exists_marker(widget, data) + \ tag(listing_tag, *tags, **{'id': cssid(widget, tagtype, 'wrapper')})
def select_edit_renderer(widget, data): tag = data.tag value = fetch_value(widget, data) if isinstance(value, basestring) or not hasattr(value, '__iter__'): value = [value] if not widget.attrs['multivalued'] and len(value) > 1: raise ValueError(u"Multiple values for single selection.") disabled = widget.attrs.get('disabled', False) if widget.attrs['format'] == 'block': optiontags = [] for key, term in vocabulary(widget.attrs.get('vocabulary', [])): attrs = { 'selected': (key in value) and 'selected' or None, 'value': key, 'id': cssid(widget, 'input', key), } if disabled and disabled is not True and key in disabled: attrs['disabled'] = 'disabled' optiontags.append(tag('option', term, **attrs)) select_attrs = { 'name_': widget.dottedpath, 'id': cssid(widget, 'input'), 'class_': cssclasses(widget, data), 'multiple': widget.attrs['multivalued'] and 'multiple' or None, 'placeholder': widget.attrs.get('placeholder') or None, 'autofocus': widget.attrs.get('autofocus') and 'autofocus' or None, 'required': widget.attrs.get('required') and 'required' or None, } if disabled is True: select_attrs['disabled'] = 'disabled' rendered = tag('select', *optiontags, **select_attrs) if widget.attrs['multivalued']: attrs = { 'type': 'hidden', 'value': 'exists', 'name_': "%s-exists" % widget.dottedpath, 'id': cssid(widget, 'exists'), } rendered = select_exists_marker(widget, data) + rendered return rendered else: tags = [] label_pos = widget.attrs['listing_label_position'] listing_tag = widget.attrs['listing_tag'] item_tag = listing_tag == 'div' and 'div' or 'li' if widget.attrs['multivalued']: tagtype = 'checkbox' else: tagtype = 'radio' for key, term in vocabulary(widget.attrs.get('vocabulary', [])): attrs = { 'type': tagtype, 'value': key, 'checked': (key in value) and 'checked' or None, 'name_': widget.dottedpath, 'id': cssid(widget, 'input', key), 'class_': cssclasses(widget, data), } if (disabled and disabled is not True and key in disabled) \ or disabled is True: attrs['disabled'] = 'disabled' input = tag('input', **attrs) if label_pos == 'inner': item = tag('label', term, input, for_=attrs['id']) elif label_pos == 'after': item = input + tag('label', term, for_=attrs['id']) else: item = tag('label', term, for_=attrs['id']) + input tags.append(tag(item_tag, item, **{'id': cssid(widget, tagtype, key)})) return select_exists_marker(widget, data) + \ tag(listing_tag, *tags, **{'id': cssid(widget, tagtype, 'wrapper')})