BERTopic icon indicating copy to clipboard operation
BERTopic copied to clipboard

`check_documents_type(documents)` in `utils.py` to support non-string portions of documents

Open armandidandeh opened this issue 5 years ago • 1 comments

Looking at the piece of code below in utils.py:

def check_documents_type(documents):
    """ Check whether the input documents are indeed a list of strings """
    if isinstance(documents, Iterable) and not isinstance(documents, str):
        if not any([isinstance(doc, str) for doc in documents]):
            raise TypeError("Make sure that the iterable only contains strings.")

    else:
        raise TypeError("Make sure that the documents variable is an iterable containing strings only.")

There are a lot of cases where a majority of a document is <class 'str'> and yet there will be an exception raised here.

Better support for such cases can be beneficial, for instance, to make a document with isinstance(documents, str) of True into an Iterable object or allowing a prop to decide what to do with numbers/dates/etc. within the document text.

There is also the case of double quotations within a text to show quotes from someone that also breaks the code resulting in a TypeError().

This solution may potentially return a modified version as the outcome of such alteration.

armandidandeh avatar Dec 20 '20 04:12 armandidandeh

I would like to steer clear of purely checking for an iterable as the code will break if the proper document format is not checked. I might check for different types that are relevant in these cases. For now, a str is necessary so it might be an idea to simply check if the documents can be converted to strings instead of checking for types.

MaartenGr avatar Dec 20 '20 06:12 MaartenGr