Repository: pytorch/text

Issue:
Try to get the vocab of NestedField but only get '[]'
# Here is the dataset

train1.csv: "3","EBay gets into rentals","EBay plans to buy the apartment and home rental service Rent.com for \$415 million, adding to its already exhaustive breadth of offerings."
test1.csv: "3","EBay gets into rentals","EBay plans to buy the apartment and home rental service Rent.com for \$415 million, adding to its already exhaustive breadth of offerings."

# Here is my code**

import os
import torch
import torchtext
from torchtext import data

import spacy 
spacy_en = spacy.load('en')

def tokenizer(text):# create a tokenizer function
    return [tok.text for tok in spacy_en.tokenizer(text)]
    
def sentenizer(text):
	return [sent.text for sent in spacy_en(text).sents]

LABEL = data.Field(sequential=False, use_vocab=False)

SENTENCE = data.Field(sequential=True, tokenize=sentenizer, lower=True)
TEXT_NESTING = data.Field(sequential=True, tokenize=tokenizer, lower=True)
TEXT_2d = data.NestedField(TEXT_NESTING)

zhang2015_datafields_2d = [('Label', LABEL), (('Sentence', 'Text'), (SENTENCE, TEXT_2d))]

train, test = data.TabularDataset.splits(path='data/ag_news_csv/', \
	train='train1.csv', \
	test='test1.csv', \
	format='csv', \
	fields=zhang2015_datafields_2d)

print(train.fields)
print(len(train))
print(vars(train[0]))

TEXT_2d.build_vocab(train)

print (TEXT_2d.vocab.freqs.most_common(10))

# Here is my output

{'Text': <torchtext.data.field.NestedField object at 0x118871208>, 'Sentence': <torchtext.data.field.Field object at 0x1188714e0>, 'Label': <torchtext.data.field.Field object at 0x118871518>}
1
{'Sentence': ['ebay gets into rentals'], 'Text': [['ebay'], ['gets'], ['into'], ['rentals']], 'Label': '3'}
[]


Discussion:
I have the same issue for using `NestedField` to do character-level embedding. 

I followed the [test script](https://github.com/pytorch/text/blob/master/test/sequence_tagging.py#L69) but `CHAR.vocab.freqs` is empty.

Resolving patch:
diff --git a/torchtext/data/field.py b/torchtext/data/field.py
--- a/torchtext/data/field.py
+++ b/torchtext/data/field.py
@@ -644,6 +644,7 @@ def build_vocab(self, *args, **kwargs):
         self.nesting_field.build_vocab(*flattened, **kwargs)
         super(NestedField, self).build_vocab()
         self.vocab.extend(self.nesting_field.vocab)
+        self.vocab.freqs = self.nesting_field.vocab.freqs.copy()
         if old_vectors is not None:
             self.vocab.load_vectors(old_vectors,
                                     unk_init=old_unk_init, cache=old_vectors_cache)


Test patch:
diff --git a/test/data/test_field.py b/test/data/test_field.py
--- a/test/data/test_field.py
+++ b/test/data/test_field.py
@@ -514,6 +514,9 @@ def test_build_vocab_from_dataset(self):
         for c in expected:
             assert c in CHARS.vocab.stoi
 
+        expected_freqs = Counter({"a": 6, "b": 6, "c": 1})
+        assert CHARS.vocab.freqs == CHARS.nesting_field.vocab.freqs == expected_freqs
+
     def test_build_vocab_from_iterable(self):
         nesting_field = data.Field(unk_token="<cunk>", pad_token="<cpad>")
         CHARS = data.NestedField(nesting_field)
@@ -527,6 +530,9 @@ def test_build_vocab_from_iterable(self):
         for c in expected:
             assert c in CHARS.vocab.stoi
 
+        expected_freqs = Counter({"a": 6, "b": 12, "c": 4})
+        assert CHARS.vocab.freqs == CHARS.nesting_field.vocab.freqs == expected_freqs
+
     def test_pad(self):
         nesting_field = data.Field(tokenize=list, unk_token="<cunk>", pad_token="<cpad>",
                                    init_token="<w>", eos_token="</w>")