Skip to content

Whatsapp

WhatsappChatLoader #

Bases: BaseReader

Whatsapp chat data loader.

Parameters:

Name Type Description Default
path str

Path to Whatsapp chat file.

required
Source code in llama-index-integrations/readers/llama-index-readers-whatsapp/llama_index/readers/whatsapp/base.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class WhatsappChatLoader(BaseReader):
    """
    Whatsapp chat data loader.

    Args:
        path (str): Path to Whatsapp chat file.
    """

    def __init__(self, path: str):
        """Initialize with path."""
        self.file_path = path

    def load_data(self) -> List[Document]:
        """
        Parse Whatsapp file into Documents.
        """
        from chatminer.chatparsers import WhatsAppParser

        path = Path(self.file_path)

        parser = WhatsAppParser(path)
        parser.parse_file()
        df = parser.parsed_messages.get_df()

        logging.debug(f"> Number of messages: {len(df)}.")

        docs = []
        n = 0
        for row in df.itertuples():
            extra_info = {
                "source": str(path).split("/")[-1].replace(".txt", ""),
                "author": row.author,
                "timestamp": str(row.timestamp),
            }

            docs.append(
                Document(
                    text=str(row.timestamp)
                    + " "
                    + row.author
                    + ":"
                    + " "
                    + row.message,
                    extra_info=extra_info,
                )
            )

            n += 1
            logging.debug(f"Added {n} of {len(df)} messages.")

        logging.debug(f"> Document creation for {path} is complete.")
        return docs

load_data #

load_data() -> List[Document]

Parse Whatsapp file into Documents.

Source code in llama-index-integrations/readers/llama-index-readers-whatsapp/llama_index/readers/whatsapp/base.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def load_data(self) -> List[Document]:
    """
    Parse Whatsapp file into Documents.
    """
    from chatminer.chatparsers import WhatsAppParser

    path = Path(self.file_path)

    parser = WhatsAppParser(path)
    parser.parse_file()
    df = parser.parsed_messages.get_df()

    logging.debug(f"> Number of messages: {len(df)}.")

    docs = []
    n = 0
    for row in df.itertuples():
        extra_info = {
            "source": str(path).split("/")[-1].replace(".txt", ""),
            "author": row.author,
            "timestamp": str(row.timestamp),
        }

        docs.append(
            Document(
                text=str(row.timestamp)
                + " "
                + row.author
                + ":"
                + " "
                + row.message,
                extra_info=extra_info,
            )
        )

        n += 1
        logging.debug(f"Added {n} of {len(df)} messages.")

    logging.debug(f"> Document creation for {path} is complete.")
    return docs