whatstk._chat¶
Library objects.
Classes:
|
Base chat object. |
- class whatstk._chat.BaseChat(df: DataFrame, platform: Optional[str] = None)[source]¶
Bases:
objectBase chat object.
- df¶
Chat as pandas.DataFrame.
See also
Attributes:
Chat as DataFrame.
Chat as DataFrame.
Chat end date.
True if the chart is a group.
Name of the chat.
Chat starting date.
List with users.
Methods:
from_source(**kwargs)Load chat.
merge(chat[, rename_users])Merge current instance with
chat.rename_users(mapping)Rename users.
to_csv(filepath)Save chat as csv.
- property df: DataFrame¶
Chat as DataFrame.
- Returns
pandas.DataFrame
- property df_system: DataFrame¶
Chat as DataFrame.
- Returns
pandas.DataFrame
- property end_date: Union[str, datetime]¶
Chat end date.
- Returns
datetime
- classmethod from_source(**kwargs: Any) None[source]¶
Load chat.
- Parameters
kwargs – Specific to the child class.
- Raises
NotImplementedError – Must be implemented in children.
See also
- property is_group: bool¶
True if the chart is a group.
A chat is detected as a group if it has more than 2 users (including the ‘system’). Groups with one person will not be detected as groups.
- Returns
bool
- merge(chat: BaseChat, rename_users: Optional[Dict[str, str]] = None) BaseChat[source]¶
Merge current instance with
chat.- Parameters
chat (WhatsAppChat) – Another chat.
rename_users (dict) – Dictionary mapping old names to new names. Example: {‘John’:[‘Jon’, ‘J’], ‘Ray’: [‘Raymond’]} will map ‘Jon’ and ‘J’ to ‘John’, and ‘Raymond’ to ‘Ray’. Note that old names must come as list (even if there is only one).
- Returns
BaseChat – Merged chat.
See also
Example
Merging two chats can become handy when you have exported a chat in different times with your phone and hence each exported file might contain data that is unique to that file.
In this example however, we merge files from different chats.
>>> from whatstk.whatsapp.objects import WhatsAppChat >>> from whatstk.data import whatsapp_urls >>> filepath_1 = whatsapp_urls.LOREM1 >>> filepath_2 = whatsapp_urls.LOREM2 >>> chat_1 = WhatsAppChat.from_source(filepath=filepath_1) >>> chat_2 = WhatsAppChat.from_source(filepath=filepath_2) >>> chat = chat_1.merge(chat_2)
- property name: Optional[str]¶
Name of the chat.
Returns None if no name could be found. The name is extracted from the username of with the first system message in the chat.
- Returns
list
- rename_users(mapping: Dict[str, str]) BaseChat[source]¶
Rename users.
This might be needed in multiple occations:
Change typos in user names stored in phone.
- If a user appears multiple times with different usernames, group these under the same name (this might
happen when multiple chats are merged).
- Parameters
mapping (dict) – Dictionary mapping old names to new names, example: {‘John’: [‘Jon’, ‘J’], ‘Ray’: [‘Raymond’]} will map ‘Jon’ and ‘J’ to ‘John’, and ‘Raymond’ to ‘Ray’. Note that old names must come as list (even if there is only one).
- Returns
pandas.DataFrame – DataFrame with users renamed according to mapping.
- Raises
ValueError – Raised if mapping is not correct.
Examples
Load LOREM2 chat and rename users Maria and Maria2 to Mary.
>>> from whatstk.whatsapp.objects import WhatsAppChat >>> from whatstk.data import whatsapp_urls >>> chat = WhatsAppChat.from_source(filepath=whatsapp_urls.LOREM2) >>> chat.users ['+1 123 456 789', 'Giuseppe', 'John', 'Maria', 'Maria2'] >>> chat = chat.rename_users(mapping={'Mary': ['Maria', 'Maria2']}) >>> chat.users ['+1 123 456 789', 'Giuseppe', 'John', 'Mary']
- property start_date: Union[str, datetime]¶
Chat starting date.
- Returns
datetime
- property users: List[str]¶
List with users.
- Returns
list