pyconversations.convo¶
- class pyconversations.convo.Conversation(posts=None, convo_id=None)[source]¶
A container class for managing collections of UniMessage (post) objects.
- __add__(other)[source]¶
Defines the addition operation over Conversation objects. Returns a new copy of a conversation.
- Parameters
other (UniMessage) – Another conversation to be added to this one.
- Returns
Conversation – The combination of this conversation and the conversation in other
- __init__(posts=None, convo_id=None)[source]¶
Constructor for Conversation object.
- Parameters
posts – An optional dictionary of messages/posts; keys should be unique IDs.
- __weakref__¶
list of weak references to the object (if defined)
- add_post(post)[source]¶
Adds a post to the conversational container.
- Parameters
post (UniMessage, or derivative concrete class) – The post object to be added.
- Returns
None
- as_graph()[source]¶
Constructs (and returns) a networkx Graph object from the contained posts and edges.
- Returns
networkx.Graph – The networkx graph associated with this Conversation
- property convo_id¶
The conversation identifier
- Returns
Any (or str) – Returns a conversation identifier. Creates ones from sources if unspecified.
- filter(by_langs=None, min_chars=0, before=None, after=None, by_tags=None, by_platform=None, by_author=None)[source]¶
Returns the set of post UIDs that meet the parameterized criteria
- Parameters
by_langs (set(str)) – The desired language codes to be retained. (Default: None)
min_chars (int) – The minimum number of characters a post should have. (Default: 0)
before (datetime.datetime) – The earliest datetime desired. (Default: None)
after (datetime.datetime) – The latest datetime desired. (Default: None)
by_tags (set(str)) – The required tags. (Default: None)
by_platform (set(str)) – A set of string names of platforms that should be retained
by_author (str) – An author
- Returns
set(hashable) – Set of UIDs
- static from_json(raw)[source]¶
Converts a JSON representation of a Conversation into a full object.
- Parameters
raw (JSON/dict) – The raw JSON
- Returns
Conversation – The conversation read from the raw JSON
- get_after(uid, include_post=False)[source]¶
Returns the collection of posts in this conversation that were created after the post with UID uid
- Parameters
uid (Hashable) – The UID of the post that is the pivot
include_post (bool) – Whether the post should be included in returned collection. Default: False
- Returns
Conversation – The collection of posts posted after uid
- Raises
KeyError – When uid is not in the Conversation
- get_ancestors(uid, include_post=False)[source]¶
Returns the ancestor posts/path for post uid.
- Parameters
uid (Hashable) – The unique identifier of desired post
include_post (bool) – Whether the post should be included in returned collection. Default: False
- Returns
Conversation – The collection of ancestor posts
- get_before(uid, include_post=False)[source]¶
Returns the collection of posts in this conversation that were created before the post with UID uid
- Parameters
uid (Hashable) – The UID of the post that is the pivot
include_post (bool) – Whether the post should be included in returned collection. Default: False
- Returns
Conversation – The collection of posts posted before uid
- Raises
KeyError – When uid is not in the Conversation
- get_children(uid, include_post=False)[source]¶
Returns the children of a post specified by uid.
- Parameters
uid (Hashable) – The unique identifier of desired post
include_post (bool) – Whether the post should be included in returned collection. Default: False
- Returns
Conversation – The collection of children posts
- get_descendants(uid, include_post=False)[source]¶
Returns the descendant sub-tree for post uid.
- Parameters
uid (Hashable) – The unique identifier of desired post
include_post (bool) – Whether the post should be included in returned collection. Default: False
- Returns
Conversation – The collection of descendant posts
- get_parents(uid, include_post=False)[source]¶
Returns the parent(s) of a post specified by uid.
- Parameters
uid (Hashable) – The unique identifier of desired post
include_post (bool) – Whether the post should be included in returned collection. Default: False
- Returns
Conversation – The collection of parent posts
- get_siblings(uid, include_post=False)[source]¶
Returns the siblings of a post specified by uid. Siblings are the child posts of this post’s parent posts.
- Parameters
uid (Hashable) – The unique identifier of desired post
include_post (bool) – Whether the post should be included in returned collection. Default: False
- Returns
Conversation – The collection of sibling posts
- get_sources()[source]¶
Returns the originating (non-reply) posts included in this conversation.
- Returns
set(UID) – The set of unique IDs of posts that originate conversation (are not replies)
- property posts¶
Returns a dictionary of posts, keyed by their UIDs.
- Returns
dict(UID, UniMessage) – The dictionary of posts contained in this Conversation object
- redact(assign_ints=True)[source]¶
Redacts user information from the conversation.
- Parameters
assign_ints (bool) – If True, assigns a unique integer to each user such the user will be referred to as USER><d+> Otherwise, all user redactions will become a USER token.
- Returns
None
- remove_post(uid)[source]¶
Deletes a post from the conversational container using its UID.
- Parameters
uid (Hashable) – Unique identifier for the post to delete.
- Returns
None
- segment()[source]¶
Segments a conversation into disjoint (i.e., not connected by any replies) sub-conversations. If a single conversation is contained in this object, this function will return a list with a single element: a copy of this object.
- Returns
list(Conversation) – A list of sub-conversations
- text_stream()[source]¶
Returns the text of the Conversation as a single stream. If timestamps are available, text will appear in temporal order.
- Returns
list(str) – The text of the conversation, by post, in temporal order (if available)