Solved - 'list' object has no attribute 'split' in listed list for vocabulary in nlp

Viewed 27

I have the input in a picke that have this form:

[['ads',
  'make',
  'hanging',
  'seem',
  'like',
  'upbeat',
  'comedy'],
['ladybugs',
  'typical',
  'comedy',
  'relies',
  'three',
  'supposed']]

The code is:
```python

    # calculate the maximum document length
    def max_length(lines):
        return max([len(s.split()) for s in lines])
    
    from pickle import load
    from numpy import array
    # load training dataset
    trainLines, trainLabels = load_dataset('train.pkl')
    # create tokenizer
    #tokenizer = create_tokenizer(trainLines)
    # calculate max document length    
    length = max_length(trainLines)
    print('Max document length: %d' % length)

I have this error:

AttributeError                            Traceback (most recent call last)
Input In [83], in <cell line: 21>()
     17 trainLines, trainLabels = load_dataset('train.pkl')
     18 # create tokenizer
     19 #tokenizer = create_tokenizer(trainLines)
     20 # calculate max document length    
---> 21 length = max_length(trainLines)
     22 print('Max document length: %d' % length)

Input In [65], in max_length(lines)
      2 def max_length(lines):
----> 3     return max([len(s.split(',')) for s in lines])

AttributeError: 'list' object has no attribute 'split'

And the output must be:


    Max document length: 1380

Anyone knows how do I resolve this error? Note that I have a listed list with two [[]]

Thanks in advance

Solution: In function remove "split()":


    # calculate the maximum document length
        def max_length(lines):
            return max([len(s) for s in lines])

0 Answers
Related