[GitHub] marcoabreu commented on a change in pull request #10074: Add vocabulary and embedding

2018-03-12 Thread GitBox
marcoabreu commented on a change in pull request #10074: Add vocabulary and 
embedding
URL: https://github.com/apache/incubator-mxnet/pull/10074#discussion_r173892544
 
 

 ##
 File path: python/mxnet/text/embedding.py
 ##
 @@ -0,0 +1,583 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+# pylint: disable=consider-iterating-dictionary
+# pylint: disable=super-init-not-called
+# pylint: disable=arguments-differ
 
 Review comment:
   Are the last two pylint ignores really invalid?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] marcoabreu commented on a change in pull request #10074: Add vocabulary and embedding

2018-03-12 Thread GitBox
marcoabreu commented on a change in pull request #10074: Add vocabulary and 
embedding
URL: https://github.com/apache/incubator-mxnet/pull/10074#discussion_r173891663
 
 

 ##
 File path: tests/python/unittest/test_text.py
 ##
 @@ -0,0 +1,675 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# 'License'); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+
+from __future__ import absolute_import
+from __future__ import print_function
+
+from collections import Counter
+
+from common import assertRaises
+from mxnet import ndarray as nd
+from mxnet.test_utils import *
+from mxnet import text
+
+
+def _get_test_str_of_tokens(token_delim, seq_delim):
+seq1 = token_delim + token_delim.join(['Life', 'is', 'great', '!']) + 
token_delim + seq_delim
+seq2 = token_delim + token_delim.join(['life', 'is', 'good', '.']) + 
token_delim + seq_delim
+seq3 = token_delim + token_delim.join(['life', "isn't", 'bad', '.']) + 
token_delim + seq_delim
+seqs = seq1 + seq2 + seq3
+return seqs
+
+
+def _test_count_tokens_from_str_with_delims(token_delim, seq_delim):
+source_str = _get_test_str_of_tokens(token_delim, seq_delim)
+
+cnt1 = text.count_tokens_from_str(source_str, token_delim, seq_delim, 
to_lower=False)
+assert cnt1 == Counter(
+{'is': 2, 'life': 2, '.': 2, 'Life': 1, 'great': 1, '!': 1, 'good': 1, 
"isn't": 1,
+ 'bad': 1})
+
+cnt2 = text.count_tokens_from_str(source_str, token_delim, seq_delim, 
to_lower=True)
+assert cnt2 == Counter(
+{'life': 3, 'is': 2, '.': 2, 'great': 1, '!': 1, 'good': 1, "isn't": 
1, 'bad': 1})
+
+counter_to_update = Counter({'life': 2})
+
+cnt3 = text.utils.count_tokens_from_str(source_str, token_delim, 
seq_delim, to_lower=False,
+
counter_to_update=counter_to_update.copy())
+assert cnt3 == Counter(
+{'is': 2, 'life': 4, '.': 2, 'Life': 1, 'great': 1, '!': 1, 'good': 1, 
"isn't": 1,
+ 'bad': 1})
+
+cnt4 = text.count_tokens_from_str(source_str, token_delim, seq_delim, 
to_lower=True,
+  
counter_to_update=counter_to_update.copy())
+assert cnt4 == Counter(
+{'life': 5, 'is': 2, '.': 2, 'great': 1, '!': 1, 'good': 1, "isn't": 
1, 'bad': 1})
+
+
+def test_count_tokens_from_str():
+_test_count_tokens_from_str_with_delims(' ', '\n')
+_test_count_tokens_from_str_with_delims('IS', 'LIFE')
+
+
+def test_vocabulary_getitem():
+counter = Counter(['a', 'b', 'b', 'c', 'c', 'c', 'some_word$'])
+
+vocab = text.Vocabulary(counter, max_size=None, min_freq=1, 
unknown_token='',
+reserved_tokens=None)
+
+i1 = vocab['c']
+assert i1 == 1
+
+i2 = vocab[['c']]
+assert i2 == [1]
+
+i3 = vocab[['', 'non-exist']]
+assert i3 == [0, 0]
+
+i4 = vocab[['a', 'non-exist', 'a', 'b']]
+assert i4 == [3, 0, 3, 2]
+
+
+def test_vocabulary_to_tokens():
+counter = Counter(['a', 'b', 'b', 'c', 'c', 'c', 'some_word$'])
+
+vocab = text.Vocabulary(counter, max_size=None, min_freq=1,
+unknown_token='', reserved_tokens=None)
+i1 = vocab.to_tokens(1)
+assert i1 == 'c'
+
+i2 = vocab.to_tokens([1])
+assert i2 == ['c']
+
+i3 = vocab.to_tokens([0, 0])
+assert i3 == ['', '']
+
+i4 = vocab.to_tokens([3, 0, 3, 2])
+assert i4 == ['a', '', 'a', 'b']
+
+assertRaises(ValueError, vocab.to_tokens, 5)
+assertRaises(ValueError, vocab.to_tokens, [5, 6])
+
+
+def test_vocabulary():
+counter = Counter(['a', 'b', 'b', 'c', 'c', 'c', 'some_word$'])
+
+v1 = text.Vocabulary(counter, max_size=None, min_freq=1, 
unknown_token='',
+ reserved_tokens=None)
+assert len(v1) == 5
+assert v1.token_to_idx == {'': 0, 'c': 1, 'b': 2, 'a': 3, 
'some_word$': 4}
+assert v1.idx_to_token[1] == 'c'
+assert v1.unknown_token == ''
+assert v1.reserved_tokens is None
+assert v1.embedding is None
+assert 'a' in v1
+assert v1.unknown_token in v1
+
+v2 = text.Vocabulary(counter, max_size=None, min_freq=2, 
unknown_token='',
+ reserved_tokens=None)
+assert len(v2) == 3
+assert v2.token_to_idx == {'': 0, 'c': 1, 'b': 2}
+assert 

[GitHub] marcoabreu commented on a change in pull request #10074: Add vocabulary and embedding

2018-03-12 Thread GitBox
marcoabreu commented on a change in pull request #10074: Add vocabulary and 
embedding
URL: https://github.com/apache/incubator-mxnet/pull/10074#discussion_r173891599
 
 

 ##
 File path: tests/python/unittest/test_text.py
 ##
 @@ -0,0 +1,675 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# 'License'); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+
+from __future__ import absolute_import
+from __future__ import print_function
+
+from collections import Counter
+
+from common import assertRaises
+from mxnet import ndarray as nd
+from mxnet.test_utils import *
+from mxnet import text
+
+
+def _get_test_str_of_tokens(token_delim, seq_delim):
+seq1 = token_delim + token_delim.join(['Life', 'is', 'great', '!']) + 
token_delim + seq_delim
+seq2 = token_delim + token_delim.join(['life', 'is', 'good', '.']) + 
token_delim + seq_delim
+seq3 = token_delim + token_delim.join(['life', "isn't", 'bad', '.']) + 
token_delim + seq_delim
+seqs = seq1 + seq2 + seq3
+return seqs
+
+
+def _test_count_tokens_from_str_with_delims(token_delim, seq_delim):
+source_str = _get_test_str_of_tokens(token_delim, seq_delim)
+
+cnt1 = text.count_tokens_from_str(source_str, token_delim, seq_delim, 
to_lower=False)
+assert cnt1 == Counter(
+{'is': 2, 'life': 2, '.': 2, 'Life': 1, 'great': 1, '!': 1, 'good': 1, 
"isn't": 1,
+ 'bad': 1})
+
+cnt2 = text.count_tokens_from_str(source_str, token_delim, seq_delim, 
to_lower=True)
+assert cnt2 == Counter(
+{'life': 3, 'is': 2, '.': 2, 'great': 1, '!': 1, 'good': 1, "isn't": 
1, 'bad': 1})
+
+counter_to_update = Counter({'life': 2})
+
+cnt3 = text.utils.count_tokens_from_str(source_str, token_delim, 
seq_delim, to_lower=False,
+
counter_to_update=counter_to_update.copy())
+assert cnt3 == Counter(
+{'is': 2, 'life': 4, '.': 2, 'Life': 1, 'great': 1, '!': 1, 'good': 1, 
"isn't": 1,
+ 'bad': 1})
+
+cnt4 = text.count_tokens_from_str(source_str, token_delim, seq_delim, 
to_lower=True,
+  
counter_to_update=counter_to_update.copy())
+assert cnt4 == Counter(
+{'life': 5, 'is': 2, '.': 2, 'great': 1, '!': 1, 'good': 1, "isn't": 
1, 'bad': 1})
+
+
+def test_count_tokens_from_str():
+_test_count_tokens_from_str_with_delims(' ', '\n')
+_test_count_tokens_from_str_with_delims('IS', 'LIFE')
+
+
+def test_vocabulary_getitem():
+counter = Counter(['a', 'b', 'b', 'c', 'c', 'c', 'some_word$'])
+
+vocab = text.Vocabulary(counter, max_size=None, min_freq=1, 
unknown_token='',
+reserved_tokens=None)
+
+i1 = vocab['c']
+assert i1 == 1
+
+i2 = vocab[['c']]
+assert i2 == [1]
+
+i3 = vocab[['', 'non-exist']]
+assert i3 == [0, 0]
+
+i4 = vocab[['a', 'non-exist', 'a', 'b']]
+assert i4 == [3, 0, 3, 2]
+
+
+def test_vocabulary_to_tokens():
+counter = Counter(['a', 'b', 'b', 'c', 'c', 'c', 'some_word$'])
+
+vocab = text.Vocabulary(counter, max_size=None, min_freq=1,
+unknown_token='', reserved_tokens=None)
+i1 = vocab.to_tokens(1)
+assert i1 == 'c'
+
+i2 = vocab.to_tokens([1])
+assert i2 == ['c']
+
+i3 = vocab.to_tokens([0, 0])
+assert i3 == ['', '']
+
+i4 = vocab.to_tokens([3, 0, 3, 2])
+assert i4 == ['a', '', 'a', 'b']
+
+assertRaises(ValueError, vocab.to_tokens, 5)
+assertRaises(ValueError, vocab.to_tokens, [5, 6])
+
+
+def test_vocabulary():
+counter = Counter(['a', 'b', 'b', 'c', 'c', 'c', 'some_word$'])
+
+v1 = text.Vocabulary(counter, max_size=None, min_freq=1, 
unknown_token='',
+ reserved_tokens=None)
+assert len(v1) == 5
+assert v1.token_to_idx == {'': 0, 'c': 1, 'b': 2, 'a': 3, 
'some_word$': 4}
+assert v1.idx_to_token[1] == 'c'
+assert v1.unknown_token == ''
+assert v1.reserved_tokens is None
+assert v1.embedding is None
+assert 'a' in v1
+assert v1.unknown_token in v1
+
+v2 = text.Vocabulary(counter, max_size=None, min_freq=2, 
unknown_token='',
+ reserved_tokens=None)
+assert len(v2) == 3
+assert v2.token_to_idx == {'': 0, 'c': 1, 'b': 2}
+assert 

[GitHub] marcoabreu commented on a change in pull request #10074: Add vocabulary and embedding

2018-03-12 Thread GitBox
marcoabreu commented on a change in pull request #10074: Add vocabulary and 
embedding
URL: https://github.com/apache/incubator-mxnet/pull/10074#discussion_r173891556
 
 

 ##
 File path: tests/python/unittest/test_text.py
 ##
 @@ -0,0 +1,675 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# 'License'); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+
+from __future__ import absolute_import
+from __future__ import print_function
+
+from collections import Counter
+
+from common import assertRaises
+from mxnet import ndarray as nd
+from mxnet.test_utils import *
+from mxnet import text
+
+
+def _get_test_str_of_tokens(token_delim, seq_delim):
+seq1 = token_delim + token_delim.join(['Life', 'is', 'great', '!']) + 
token_delim + seq_delim
+seq2 = token_delim + token_delim.join(['life', 'is', 'good', '.']) + 
token_delim + seq_delim
+seq3 = token_delim + token_delim.join(['life', "isn't", 'bad', '.']) + 
token_delim + seq_delim
+seqs = seq1 + seq2 + seq3
+return seqs
+
+
+def _test_count_tokens_from_str_with_delims(token_delim, seq_delim):
+source_str = _get_test_str_of_tokens(token_delim, seq_delim)
+
+cnt1 = text.count_tokens_from_str(source_str, token_delim, seq_delim, 
to_lower=False)
+assert cnt1 == Counter(
+{'is': 2, 'life': 2, '.': 2, 'Life': 1, 'great': 1, '!': 1, 'good': 1, 
"isn't": 1,
+ 'bad': 1})
+
+cnt2 = text.count_tokens_from_str(source_str, token_delim, seq_delim, 
to_lower=True)
+assert cnt2 == Counter(
+{'life': 3, 'is': 2, '.': 2, 'great': 1, '!': 1, 'good': 1, "isn't": 
1, 'bad': 1})
+
+counter_to_update = Counter({'life': 2})
+
+cnt3 = text.utils.count_tokens_from_str(source_str, token_delim, 
seq_delim, to_lower=False,
+
counter_to_update=counter_to_update.copy())
+assert cnt3 == Counter(
+{'is': 2, 'life': 4, '.': 2, 'Life': 1, 'great': 1, '!': 1, 'good': 1, 
"isn't": 1,
+ 'bad': 1})
+
+cnt4 = text.count_tokens_from_str(source_str, token_delim, seq_delim, 
to_lower=True,
+  
counter_to_update=counter_to_update.copy())
+assert cnt4 == Counter(
+{'life': 5, 'is': 2, '.': 2, 'great': 1, '!': 1, 'good': 1, "isn't": 
1, 'bad': 1})
+
+
+def test_count_tokens_from_str():
+_test_count_tokens_from_str_with_delims(' ', '\n')
+_test_count_tokens_from_str_with_delims('IS', 'LIFE')
+
+
+def test_vocabulary_getitem():
+counter = Counter(['a', 'b', 'b', 'c', 'c', 'c', 'some_word$'])
+
+vocab = text.Vocabulary(counter, max_size=None, min_freq=1, 
unknown_token='',
+reserved_tokens=None)
+
+i1 = vocab['c']
+assert i1 == 1
+
+i2 = vocab[['c']]
+assert i2 == [1]
+
+i3 = vocab[['', 'non-exist']]
+assert i3 == [0, 0]
+
+i4 = vocab[['a', 'non-exist', 'a', 'b']]
+assert i4 == [3, 0, 3, 2]
+
+
+def test_vocabulary_to_tokens():
+counter = Counter(['a', 'b', 'b', 'c', 'c', 'c', 'some_word$'])
+
+vocab = text.Vocabulary(counter, max_size=None, min_freq=1,
+unknown_token='', reserved_tokens=None)
+i1 = vocab.to_tokens(1)
+assert i1 == 'c'
+
+i2 = vocab.to_tokens([1])
+assert i2 == ['c']
+
+i3 = vocab.to_tokens([0, 0])
+assert i3 == ['', '']
+
+i4 = vocab.to_tokens([3, 0, 3, 2])
+assert i4 == ['a', '', 'a', 'b']
+
+assertRaises(ValueError, vocab.to_tokens, 5)
+assertRaises(ValueError, vocab.to_tokens, [5, 6])
+
+
+def test_vocabulary():
+counter = Counter(['a', 'b', 'b', 'c', 'c', 'c', 'some_word$'])
+
+v1 = text.Vocabulary(counter, max_size=None, min_freq=1, 
unknown_token='',
+ reserved_tokens=None)
+assert len(v1) == 5
+assert v1.token_to_idx == {'': 0, 'c': 1, 'b': 2, 'a': 3, 
'some_word$': 4}
+assert v1.idx_to_token[1] == 'c'
+assert v1.unknown_token == ''
+assert v1.reserved_tokens is None
+assert v1.embedding is None
+assert 'a' in v1
+assert v1.unknown_token in v1
+
+v2 = text.Vocabulary(counter, max_size=None, min_freq=2, 
unknown_token='',
+ reserved_tokens=None)
+assert len(v2) == 3
+assert v2.token_to_idx == {'': 0, 'c': 1, 'b': 2}
+assert