简体   繁体   中英

Convert NLTK LazySubsequence to a list

Is there any NLTK built-in functions to convert an NLTK LazySubsequence to a list?

Eg

from nltk.corpus import brown
corpus = brown.sents()
LS = corpus[0:250]
print('type(corpus)[0:250]: {0}'.format(type(LS)))

returns type(corpus)[0:250]: <class 'nltk.util.LazySubsequence'> .

I know I could convert looping around eg as follows with convert_LazySubsequence_to_list() , but I wonder whether NLTK offers any native way to cast to a Python list:

from nltk.corpus import brown

def convert_LazySubsequence_to_list(LS):
    corpus = []
    for s in LS:
        corpus.append(s)
    return corpus

corpus = brown.sents()
LS = corpus[0:250]
print('type(corpus)[0:250]: {0}'.format(type(LS)))

my_list = convert_LazySubsequence_to_list(LS)
print('type(my_list): {0}'.format(type(my_list)))

outputs

type(corpus)[0:250]: <class 'nltk.util.LazySubsequence'>
type(my_list): <type 'list'>

Actually this is easier than you might think

>>> from nltk.corpus import brown
>>> corpus = brown.sents()
>>> LS = corpus[0:250]
>>> print('type(corpus)[0:250]: {0}'.format(type(LS)))
type(corpus)[0:250]: <class 'nltk.util.LazySubsequence'>
>>> my_list = list(LS)
>>> print('type(my_list): {0}'.format(type(my_list)))
type(my_list): <class 'list'>

No need for any special NLTK functions. Pythons built in list() can do it for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM