| """Extract node and relation data from the rehydrated Twitter data""" |
|
|
| import multiprocessing as mp |
| import re |
| from collections import defaultdict |
| from typing import Dict, List, Optional, Tuple, Union |
|
|
| import numpy as np |
| import pandas as pd |
| from tqdm.auto import tqdm |
|
|
| from .article import process_article_url |
| from .image import process_image_url |
|
|
|
|
| class DataExtractor: |
| """Extract node and relation data from the rehydrated Twitter data. |
| |
| Args: |
| include_replies (bool): |
| Whether to include replies. |
| include_articles (bool): |
| Whether to include articles. |
| include_tweet_images (bool): |
| Whether to include tweet images. |
| include_extra_images (bool): |
| Whether to include extra images. |
| include_hashtags (bool): |
| Whether to include hashtags. |
| include_mentions (bool): |
| Whether to include mentions. |
| n_jobs (int): |
| The number of parallel jobs to use. |
| chunksize (int): |
| The number of samples to process at a time. |
| |
| Attributes: |
| include_replies (bool): |
| Whether to include replies. |
| include_articles (bool): |
| Whether to include articles. |
| include_tweet_images (bool): |
| Whether to include tweet images. |
| include_extra_images (bool): |
| Whether to include extra images. |
| include_hashtags (bool): |
| Whether to include hashtags. |
| include_mentions (bool): |
| Whether to include mentions. |
| """ |
|
|
| def __init__( |
| self, |
| include_replies: bool, |
| include_articles: bool, |
| include_tweet_images: bool, |
| include_extra_images: bool, |
| include_hashtags: bool, |
| include_mentions: bool, |
| n_jobs: int, |
| chunksize: int, |
| ): |
| self.include_replies = include_replies |
| self.include_articles = include_articles |
| self.include_tweet_images = include_tweet_images |
| self.include_extra_images = include_extra_images |
| self.include_hashtags = include_hashtags |
| self.include_mentions = include_mentions |
| self.n_jobs = n_jobs |
| self.chunksize = chunksize |
|
|
| def extract_all( |
| self, |
| nodes: Dict[str, pd.DataFrame], |
| rels: Dict[Tuple[str, str, str], pd.DataFrame], |
| ) -> Tuple[dict, dict]: |
| """Extract all node and relation data. |
| |
| Args: |
| nodes (Dict[str, pd.DataFrame]): |
| A dictionary of node dataframes. |
| rels (Dict[Tuple[str, str, str], pd.DataFrame]): |
| A dictionary of relation dataframes. |
| |
| Returns: |
| pair of dicts: |
| A tuple of updated node and relation dictionaries. |
| """ |
| |
| nodes["hashtag"] = self._extract_hashtags( |
| tweet_df=nodes["tweet"], user_df=nodes["user"] |
| ) |
| rels[("user", "posted", "tweet")] = self._extract_user_posted_tweet( |
| tweet_df=nodes["tweet"], user_df=nodes["user"] |
| ) |
| rels[("user", "posted", "reply")] = self._extract_user_posted_reply( |
| reply_df=nodes["reply"], user_df=nodes["user"] |
| ) |
| rels[("tweet", "mentions", "user")] = self._extract_tweet_mentions_user( |
| tweet_df=nodes["tweet"], user_df=nodes["user"] |
| ) |
| rels[("user", "mentions", "user")] = self._extract_user_mentions_user( |
| user_df=nodes["user"] |
| ) |
|
|
| |
| rels[ |
| ("tweet", "has_hashtag", "hashtag") |
| ] = self._extract_tweet_has_hashtag_hashtag( |
| tweet_df=nodes["tweet"], hashtag_df=nodes["hashtag"] |
| ) |
| rels[ |
| ("user", "has_hashtag", "hashtag") |
| ] = self._extract_user_has_hashtag_hashtag( |
| user_df=nodes["user"], hashtag_df=nodes["hashtag"] |
| ) |
|
|
| |
| |
| nodes["url"] = self._extract_urls( |
| tweet_dicusses_claim_df=rels[("tweet", "discusses", "claim")], |
| user_posted_tweet_df=rels[("user", "posted", "tweet")], |
| user_df=nodes["user"], |
| tweet_df=nodes["tweet"], |
| article_df=nodes.get("article"), |
| image_df=nodes.get("image"), |
| ) |
|
|
| |
| nodes["article"] = self._extract_articles( |
| url_df=nodes["url"], |
| ) |
|
|
| |
| nodes["url"] = self._update_urls_from_articles( |
| url_df=nodes["url"], article_df=nodes["article"] |
| ) |
|
|
| |
| rels[("user", "has_url", "url")] = self._extract_user_has_url_url( |
| user_df=nodes["user"], url_df=nodes["url"] |
| ) |
| rels[ |
| ("user", "has_profile_picture_url", "url") |
| ] = self._extract_user_has_profile_picture_url_url( |
| user_df=nodes["user"], url_df=nodes["url"] |
| ) |
|
|
| |
| rels[("tweet", "has_url", "url")] = self._extract_tweet_has_url_url( |
| tweet_df=nodes["tweet"], url_df=nodes["url"], image_df=nodes.get("image") |
| ) |
|
|
| |
| nodes["image"] = self._extract_images( |
| url_df=nodes["url"], article_df=nodes["article"] |
| ) |
| rels[ |
| ("article", "has_top_image_url", "url") |
| ] = self._extract_article_has_top_image_url_url( |
| article_df=nodes["article"], url_df=nodes["url"] |
| ) |
|
|
| |
| |
| rels[ |
| ("tweet", "has_article", "article") |
| ] = self._extract_tweet_has_article_article( |
| tweet_has_url_url_df=rels[("tweet", "has_url", "url")], |
| article_df=nodes["article"], |
| url_df=nodes["url"], |
| ) |
|
|
| |
| |
| rels[("tweet", "has_image", "image")] = self._extract_tweet_has_image_image( |
| tweet_has_url_url_df=rels[("tweet", "has_url", "url")], |
| url_df=nodes["url"], |
| image_df=nodes["image"], |
| ) |
|
|
| |
| |
| top_image_url_rel = rels[("article", "has_top_image_url", "url")] |
| rels[ |
| ("article", "has_top_image", "image") |
| ] = self._extract_article_has_top_image_image( |
| article_has_top_image_url_url_df=top_image_url_rel, |
| url_df=nodes["url"], |
| image_df=nodes["image"], |
| ) |
|
|
| |
| |
| has_profile_pic_df = rels[("user", "has_profile_picture_url", "url")] |
| rels[ |
| ("user", "has_profile_picture", "image") |
| ] = self._extract_user_has_profile_picture_image( |
| user_has_profile_picture_url_url_df=has_profile_pic_df, |
| url_df=nodes["url"], |
| image_df=nodes["image"], |
| ) |
|
|
| return nodes, rels |
|
|
| def _extract_user_posted_tweet( |
| self, tweet_df: pd.DataFrame, user_df: pd.DataFrame |
| ) -> pd.DataFrame: |
| """Extract (:User)-[:POSTED]->(:Tweet) relation data. |
| |
| Args: |
| tweet_df (pd.DataFrame): |
| A dataframe of tweets. |
| user_df (pd.DataFrame): |
| A dataframe of users. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of relations. |
| """ |
| if len(tweet_df) and len(user_df): |
| merged = ( |
| tweet_df[["author_id"]] |
| .dropna() |
| .reset_index() |
| .rename(columns=dict(index="tweet_idx")) |
| .astype({"author_id": np.uint64}) |
| .merge( |
| user_df[["user_id"]] |
| .reset_index() |
| .rename(columns=dict(index="user_idx")), |
| left_on="author_id", |
| right_on="user_id", |
| ) |
| ) |
| data_dict = dict( |
| src=merged.user_idx.tolist(), tgt=merged.tweet_idx.tolist() |
| ) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_user_posted_reply( |
| self, reply_df: pd.DataFrame, user_df: pd.DataFrame |
| ) -> pd.DataFrame: |
| """Extract (:User)-[:POSTED]->(:Reply) relation data. |
| |
| Args: |
| reply_df (pd.DataFrame): |
| A dataframe of replies. |
| user_df (pd.DataFrame): |
| A dataframe of users. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of relations. |
| """ |
| if self.include_replies and len(reply_df) and len(user_df): |
| merged = ( |
| reply_df[["author_id"]] |
| .dropna() |
| .reset_index() |
| .rename(columns=dict(index="reply_idx")) |
| .astype({"author_id": np.uint64}) |
| .merge( |
| user_df[["user_id"]] |
| .reset_index() |
| .rename(columns=dict(index="user_idx")), |
| left_on="author_id", |
| right_on="user_id", |
| ) |
| ) |
| data_dict = dict( |
| src=merged.user_idx.tolist(), tgt=merged.reply_idx.tolist() |
| ) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_tweet_mentions_user( |
| self, tweet_df: pd.DataFrame, user_df: pd.DataFrame |
| ) -> pd.DataFrame: |
| """Extract (:Tweet)-[:MENTIONS]->(:User) relation data. |
| |
| Args: |
| tweet_df (pd.DataFrame): |
| A dataframe of tweets. |
| user_df (pd.DataFrame): |
| A dataframe of users. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of relations. |
| """ |
| mentions_exist = "entities.mentions" in tweet_df.columns |
| if self.include_mentions and mentions_exist and len(tweet_df) and len(user_df): |
|
|
| def extract_mention(dcts: List[dict]) -> List[Union[int, str]]: |
| """Extracts user ids from a list of dictionaries. |
| |
| Args: |
| dcts (List[dict]): |
| A list of dictionaries. |
| |
| Returns: |
| List[Union[int, str]]: |
| A list of user ids. |
| """ |
| return [dct["id"] for dct in dcts] |
|
|
| merged = ( |
| tweet_df[["entities.mentions"]] |
| .dropna() |
| .applymap(extract_mention) |
| .reset_index() |
| .rename(columns=dict(index="tweet_idx")) |
| .explode("entities.mentions") |
| .astype({"entities.mentions": np.uint64}) |
| .merge( |
| user_df[["user_id"]] |
| .reset_index() |
| .rename(columns=dict(index="user_idx")), |
| left_on="entities.mentions", |
| right_on="user_id", |
| ) |
| ) |
| data_dict = dict( |
| src=merged.tweet_idx.tolist(), tgt=merged.user_idx.tolist() |
| ) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_user_mentions_user(self, user_df: pd.DataFrame) -> pd.DataFrame: |
| """Extract (:User)-[:MENTIONS]->(:User) relation data. |
| |
| Args: |
| user_df (pd.DataFrame): |
| A dataframe of users. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of relations. |
| """ |
| user_cols = user_df.columns |
| mentions_exist = "entities.description.mentions" in user_cols |
| if self.include_mentions and mentions_exist and len(user_df): |
|
|
| def extract_mention(dcts: List[dict]) -> List[str]: |
| """Extracts user ids from a list of dictionaries. |
| |
| Args: |
| dcts (List[dict]): |
| A list of dictionaries. |
| |
| Returns: |
| List[str]: |
| A list of user ids. |
| """ |
| return [dct["username"] for dct in dcts] |
|
|
| merged = ( |
| user_df[["entities.description.mentions"]] |
| .dropna() |
| .applymap(extract_mention) |
| .reset_index() |
| .rename(columns=dict(index="user_idx1")) |
| .explode("entities.description.mentions") |
| .merge( |
| user_df[["username"]] |
| .reset_index() |
| .rename(columns=dict(index="user_idx2")), |
| left_on="entities.description.mentions", |
| right_on="username", |
| ) |
| ) |
| data_dict = dict( |
| src=merged.user_idx1.tolist(), tgt=merged.user_idx2.tolist() |
| ) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_tweet_has_hashtag_hashtag( |
| self, tweet_df: pd.DataFrame, hashtag_df: pd.DataFrame |
| ) -> pd.DataFrame: |
| """Extract (:Tweet)-[:HAS_HASHTAG]->(:Hashtag) relation data. |
| |
| Args: |
| tweet_df (pd.DataFrame): |
| A dataframe of tweets. |
| hashtag_df (pd.DataFrame): |
| A dataframe of hashtags. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of relations. |
| """ |
| hashtags_exist = "entities.hashtags" in tweet_df.columns |
| if ( |
| self.include_hashtags |
| and hashtags_exist |
| and len(tweet_df) |
| and len(hashtag_df) |
| ): |
|
|
| def extract_hashtag(dcts: List[dict]) -> List[Union[str, None]]: |
| """Extracts hashtags from a list of dictionaries. |
| |
| Args: |
| dcts (list of dict): |
| A list of dictionaries. |
| |
| Returns: |
| list of str or None: |
| A list of hashtags. |
| """ |
| return [dct.get("tag") for dct in dcts] |
|
|
| merged = ( |
| tweet_df[["entities.hashtags"]] |
| .dropna() |
| .applymap(extract_hashtag) |
| .reset_index() |
| .rename(columns=dict(index="tweet_idx")) |
| .explode("entities.hashtags") |
| .merge( |
| hashtag_df[["tag"]] |
| .reset_index() |
| .rename(columns=dict(index="tag_idx")), |
| left_on="entities.hashtags", |
| right_on="tag", |
| ) |
| ) |
| data_dict = dict(src=merged.tweet_idx.tolist(), tgt=merged.tag_idx.tolist()) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_user_has_hashtag_hashtag( |
| self, user_df: pd.DataFrame, hashtag_df: pd.DataFrame |
| ) -> pd.DataFrame: |
| """Extract (:User)-[:HAS_HASHTAG]->(:Hashtag) relation data. |
| |
| Args: |
| user_df (pd.DataFrame): |
| A dataframe of users. |
| hashtag_df (pd.DataFrame): |
| A dataframe of hashtags. |
| |
| Returns: |
| pd.DataFrame: A dataframe of relations. |
| """ |
| user_cols = user_df.columns |
| hashtags_exist = "entities.description.hashtags" in user_cols |
| if ( |
| self.include_hashtags |
| and hashtags_exist |
| and len(user_df) |
| and len(hashtag_df) |
| ): |
|
|
| def extract_hashtag(dcts: List[dict]) -> List[Union[str, None]]: |
| """Extracts hashtags from a list of dictionaries. |
| |
| Args: |
| dcts (list of dict): |
| A list of dictionaries. |
| |
| Returns: |
| list of str or None: |
| A list of hashtags. |
| """ |
| return [dct.get("tag") for dct in dcts] |
|
|
| merged = ( |
| user_df[["entities.description.hashtags"]] |
| .dropna() |
| .applymap(extract_hashtag) |
| .reset_index() |
| .rename(columns=dict(index="user_idx")) |
| .explode("entities.description.hashtags") |
| .merge( |
| hashtag_df[["tag"]] |
| .reset_index() |
| .rename(columns=dict(index="tag_idx")), |
| left_on="entities.description.hashtags", |
| right_on="tag", |
| ) |
| ) |
| data_dict = dict(src=merged.user_idx.tolist(), tgt=merged.tag_idx.tolist()) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_tweet_has_url_url( |
| self, |
| tweet_df: pd.DataFrame, |
| url_df: pd.DataFrame, |
| image_df: Optional[pd.DataFrame] = None, |
| ) -> pd.DataFrame: |
| """Extract (:Tweet)-[:HAS_URL]->(:Url) relation data. |
| |
| Args: |
| tweet_df (pd.DataFrame): |
| A dataframe of tweets. |
| url_df (pd.DataFrame): |
| A dataframe of urls. |
| image_df (pd.DataFrame or None, optional): |
| A dataframe of images, or None if it is not available. Defaults |
| to None. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of relations. |
| """ |
| urls_exist = ( |
| "entities.urls" in tweet_df.columns |
| or "attachments.media_keys" in tweet_df.columns |
| ) |
| include_images = self.include_tweet_images or self.include_extra_images |
| if ( |
| (self.include_articles or include_images) |
| and urls_exist |
| and len(tweet_df) |
| and len(url_df) |
| ): |
|
|
| |
| def extract_url(dcts: List[dict]) -> List[Union[str, None]]: |
| """Extracts urls from a list of dictionaries. |
| |
| Args: |
| dcts (List[dict]): |
| A list of dictionaries. |
| |
| Returns: |
| List[str]: |
| A list of urls. |
| """ |
| return [dct.get("expanded_url") or dct.get("url") for dct in dcts] |
|
|
| merged = ( |
| tweet_df[["entities.urls"]] |
| .dropna() |
| .applymap(extract_url) |
| .reset_index() |
| .rename(columns=dict(index="tweet_idx")) |
| .explode("entities.urls") |
| .merge( |
| url_df[["url"]].reset_index().rename(columns=dict(index="ul_idx")), |
| left_on="entities.urls", |
| right_on="url", |
| ) |
| ) |
| data_dict = dict(src=merged.tweet_idx.tolist(), tgt=merged.ul_idx.tolist()) |
| rel_df = pd.DataFrame(data_dict) |
|
|
| |
| if image_df is not None and len(image_df) and include_images: |
| merged = ( |
| tweet_df.reset_index() |
| .rename(columns=dict(index="tweet_idx")) |
| .explode("attachments.media_keys") |
| .merge( |
| image_df[["media_key", "url"]], |
| left_on="attachments.media_keys", |
| right_on="media_key", |
| ) |
| .merge( |
| url_df[["url"]] |
| .reset_index() |
| .rename(columns=dict(index="ul_idx")), |
| on="url", |
| ) |
| ) |
| data_dict = dict( |
| src=merged.tweet_idx.tolist(), tgt=merged.ul_idx.tolist() |
| ) |
| rel_df = pd.concat((rel_df, pd.DataFrame(data_dict))).drop_duplicates() |
|
|
| return rel_df |
|
|
| def _extract_user_has_url_url( |
| self, user_df: pd.DataFrame, url_df: pd.DataFrame |
| ) -> pd.DataFrame: |
| """Extract (:User)-[:HAS_URL]->(:Url) relation data. |
| |
| Args: |
| user_df (pd.DataFrame): |
| A dataframe of users. |
| url_df (pd.DataFrame): |
| A dataframe of urls. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of relations. |
| """ |
| user_cols = user_df.columns |
| url_urls_exist = "entities.url.urls" in user_cols |
| desc_urls_exist = "entities.description.urls" in user_cols |
| if url_urls_exist or desc_urls_exist and len(user_df) and len(url_df): |
|
|
| def extract_url(dcts: List[dict]) -> List[Union[str, None]]: |
| """Extracts urls from a list of dictionaries. |
| |
| Args: |
| dcts (list of dict): |
| A list of dictionaries. |
| |
| Returns: |
| list of str or None: |
| A list of urls. |
| """ |
|
|
| return [dct.get("expanded_url") or dct.get("url") for dct in dcts] |
|
|
| |
| rel_df = pd.DataFrame() |
|
|
| if url_urls_exist: |
| merged = ( |
| user_df[["entities.url.urls"]] |
| .dropna() |
| .applymap(extract_url) |
| .reset_index() |
| .rename(columns=dict(index="user_idx")) |
| .explode("entities.url.urls") |
| .merge( |
| url_df[["url"]] |
| .reset_index() |
| .rename(columns=dict(index="ul_idx")), |
| left_on="entities.url.urls", |
| right_on="url", |
| ) |
| ) |
| data_dict = dict( |
| src=merged.user_idx.tolist(), tgt=merged.ul_idx.tolist() |
| ) |
| rel_df = ( |
| pd.concat((rel_df, pd.DataFrame(data_dict)), axis=0) |
| .drop_duplicates() |
| .reset_index(drop=True) |
| ) |
|
|
| if desc_urls_exist: |
| merged = ( |
| user_df[["entities.description.urls"]] |
| .dropna() |
| .applymap(extract_url) |
| .reset_index() |
| .rename(columns=dict(index="user_idx")) |
| .explode("entities.description.urls") |
| .merge( |
| url_df[["url"]] |
| .reset_index() |
| .rename(columns=dict(index="ul_idx")), |
| left_on="entities.description.urls", |
| right_on="url", |
| ) |
| ) |
| data_dict = dict( |
| src=merged.user_idx.tolist(), tgt=merged.ul_idx.tolist() |
| ) |
| rel_df = ( |
| pd.concat((rel_df, pd.DataFrame(data_dict)), axis=0) |
| .drop_duplicates() |
| .reset_index(drop=True) |
| ) |
|
|
| return rel_df |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_user_has_profile_picture_url_url( |
| self, user_df: pd.DataFrame, url_df: pd.DataFrame |
| ) -> pd.DataFrame: |
| """Extract (:User)-[:HAS_PROFILE_PICTURE]->(:Url) relation data. |
| |
| Args: |
| user_df (pd.DataFrame): |
| A dataframe of users. |
| url_df (pd.DataFrame): |
| A dataframe of urls. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of relations. |
| """ |
| user_cols = user_df.columns |
| profile_images_exist = "profile_image_url" in user_cols |
| if ( |
| self.include_extra_images |
| and profile_images_exist |
| and len(user_df) |
| and len(url_df) |
| ): |
| merged = ( |
| user_df[["profile_image_url"]] |
| .dropna() |
| .reset_index() |
| .rename(columns=dict(index="user_idx")) |
| .merge( |
| url_df[["url"]].reset_index().rename(columns=dict(index="ul_idx")), |
| left_on="profile_image_url", |
| right_on="url", |
| ) |
| ) |
| data_dict = dict(src=merged.user_idx.tolist(), tgt=merged.ul_idx.tolist()) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_articles(self, url_df: pd.DataFrame) -> pd.DataFrame: |
| """Extract (:Article) nodes. |
| |
| Args: |
| url_df (pd.DataFrame): |
| A dataframe of urls. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of articles. |
| """ |
| if self.include_articles and len(url_df): |
| |
| |
| non_article_regexs = [ |
| "youtu[.]*be", |
| "vimeo", |
| "spotify", |
| "twitter", |
| "instagram", |
| "tiktok", |
| "gab[.]com", |
| "https://t[.]me", |
| "imgur", |
| "/photo/", |
| "mp4", |
| "mov", |
| "jpg", |
| "jpeg", |
| "bmp", |
| "png", |
| "gif", |
| "pdf", |
| ] |
| non_article_regex = "(" + "|".join(non_article_regexs) + ")" |
|
|
| |
| article_urls = [ |
| url |
| for url in url_df.url.tolist() |
| if re.search(non_article_regex, url) is None |
| ] |
|
|
| |
| data_dict = defaultdict(list) |
| with mp.Pool(processes=self.n_jobs) as pool: |
| for result in tqdm( |
| pool.imap_unordered( |
| process_article_url, article_urls, chunksize=self.chunksize |
| ), |
| desc="Parsing articles", |
| total=len(article_urls), |
| ): |
|
|
| |
| if result is None: |
| continue |
|
|
| |
| data_dict["url"].append(result["url"]) |
| data_dict["title"].append(result["title"]) |
| data_dict["content"].append(result["content"]) |
| data_dict["authors"].append(result["authors"]) |
| data_dict["publish_date"].append(result["publish_date"]) |
| data_dict["top_image_url"].append(result["top_image_url"]) |
|
|
| |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_article_has_top_image_url_url( |
| self, article_df: pd.DataFrame, url_df: pd.DataFrame |
| ) -> pd.DataFrame: |
| """Extract (:Article)-[:HAS_TOP_IMAGE_URL]->(:Url) relation data. |
| |
| Args: |
| article_df (pd.DataFrame): |
| A dataframe of articles. |
| url_df (pd.DataFrame): |
| A dataframe of urls. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of relations. |
| """ |
| if ( |
| self.include_articles |
| and self.include_extra_images |
| and len(article_df) |
| and len(url_df) |
| ): |
|
|
| |
| merged = ( |
| article_df[["top_image_url"]] |
| .dropna() |
| .reset_index() |
| .rename(columns=dict(index="art_idx")) |
| .merge( |
| url_df[["url"]].reset_index().rename(columns=dict(index="ul_idx")), |
| left_on="top_image_url", |
| right_on="url", |
| ) |
| ) |
| data_dict = dict(src=merged.art_idx.tolist(), tgt=merged.ul_idx.tolist()) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_tweet_has_article_article( |
| self, |
| tweet_has_url_url_df: pd.DataFrame, |
| article_df: pd.DataFrame, |
| url_df: pd.DataFrame, |
| ) -> pd.DataFrame: |
| """Extract (:Tweet)-[:HAS_ARTICLE]->(:Article) relation data. |
| |
| Args: |
| tweet_has_url_url_df (pd.DataFrame): |
| A dataframe of relations. |
| article_df (pd.DataFrame): |
| A dataframe of articles. |
| url_df (pd.DataFrame): |
| A dataframe of urls. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of relations. |
| """ |
| if ( |
| self.include_articles |
| and len(tweet_has_url_url_df) |
| and len(article_df) |
| and len(url_df) |
| ): |
| merged = ( |
| tweet_has_url_url_df.rename(columns=dict(src="tweet_idx", tgt="ul_idx")) |
| .merge( |
| url_df[["url"]].reset_index().rename(columns=dict(index="ul_idx")), |
| on="ul_idx", |
| ) |
| .merge( |
| article_df[["url"]] |
| .reset_index() |
| .rename(columns=dict(index="art_idx")), |
| on="url", |
| ) |
| ) |
| data_dict = dict(src=merged.tweet_idx.tolist(), tgt=merged.art_idx.tolist()) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_images( |
| self, url_df: pd.DataFrame, article_df: pd.DataFrame |
| ) -> pd.DataFrame: |
| """Extract (:Image) nodes. |
| |
| Args: |
| url_df (pd.DataFrame): |
| A dataframe of urls. |
| article_df (pd.DataFrame): |
| A dataframe of articles. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of images. |
| """ |
| if self.include_tweet_images or self.include_extra_images and len(url_df): |
|
|
| |
| |
| if not len(article_df): |
| article_df = pd.DataFrame(columns=["url"]) |
|
|
| |
| image_urls = [ |
| url for url in url_df.url.tolist() if url not in article_df.url.tolist() |
| ] |
|
|
| |
| regex = "|".join( |
| [ |
| "png", |
| "jpg", |
| "jpeg", |
| "bmp", |
| "pdf", |
| "jfif", |
| "tiff", |
| "ppm", |
| "pgm", |
| "pbm", |
| "pnm", |
| "webp", |
| "hdr", |
| "heif", |
| ] |
| ) |
| image_urls = [ |
| url for url in image_urls if re.search(regex, url) is not None |
| ] |
|
|
| |
| data_dict = defaultdict(list) |
| with mp.Pool(processes=self.n_jobs) as pool: |
| for result in tqdm( |
| pool.imap_unordered( |
| process_image_url, image_urls, chunksize=self.chunksize |
| ), |
| desc="Parsing images", |
| total=len(image_urls), |
| ): |
|
|
| |
| if ( |
| result is not None |
| and len(result["pixels"].shape) == 3 |
| and result["pixels"].shape[2] == 3 |
| ): |
| data_dict["url"].append(result["url"]) |
| data_dict["pixels"].append(result["pixels"]) |
| data_dict["height"].append(result["height"]) |
| data_dict["width"].append(result["width"]) |
|
|
| |
| |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_tweet_has_image_image( |
| self, |
| tweet_has_url_url_df: pd.DataFrame, |
| url_df: pd.DataFrame, |
| image_df: pd.DataFrame, |
| ) -> pd.DataFrame: |
| """Extract (:Tweet)-[:HAS_IMAGE]->(:Image) relation data. |
| |
| Args: |
| tweet_has_url_url_df (pd.DataFrame): |
| A dataframe of relations. |
| url_df (pd.DataFrame): |
| A dataframe of urls. |
| image_df (pd.DataFrame): |
| A dataframe of images. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of relations. |
| """ |
| if ( |
| len(tweet_has_url_url_df) |
| and len(url_df) |
| and len(image_df) |
| and self.include_tweet_images |
| ): |
| url_idx = dict(index="ul_idx") |
| img_idx = dict(index="im_idx") |
| if len(tweet_has_url_url_df): |
| merged = ( |
| tweet_has_url_url_df.rename( |
| columns=dict(src="tweet_idx", tgt="ul_idx") |
| ) |
| .merge( |
| url_df[["url"]].reset_index().rename(columns=url_idx), |
| on="ul_idx", |
| ) |
| .merge( |
| image_df[["url"]].reset_index().rename(columns=img_idx), |
| on="url", |
| ) |
| ) |
| data_dict = dict( |
| src=merged.tweet_idx.tolist(), tgt=merged.im_idx.tolist() |
| ) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_article_has_top_image_image( |
| self, |
| article_has_top_image_url_url_df: pd.DataFrame, |
| url_df: pd.DataFrame, |
| image_df: pd.DataFrame, |
| ) -> pd.DataFrame: |
| """Extract (:Article)-[:HAS_TOP_IMAGE]->(:Image) relation data. |
| |
| Args: |
| article_has_top_image_url_url_df (pd.DataFrame): |
| A dataframe of relations. |
| url_df (pd.DataFrame): |
| A dataframe of urls. |
| image_df (pd.DataFrame): |
| A dataframe of images. |
| |
| Returns: |
| pd.DataFrame: A dataframe of relations. |
| """ |
| if ( |
| self.include_articles |
| and self.include_extra_images |
| and len(article_has_top_image_url_url_df) |
| and len(url_df) |
| and len(image_df) |
| ): |
| url_idx = dict(index="ul_idx") |
| img_idx = dict(index="im_idx") |
| if self.include_articles: |
| merged = ( |
| article_has_top_image_url_url_df.rename( |
| columns=dict(src="art_idx", tgt="ul_idx") |
| ) |
| .merge( |
| url_df[["url"]].reset_index().rename(columns=url_idx), |
| on="ul_idx", |
| ) |
| .merge( |
| image_df[["url"]].reset_index().rename(columns=img_idx), |
| on="url", |
| ) |
| ) |
| data_dict = dict( |
| src=merged.art_idx.tolist(), tgt=merged.im_idx.tolist() |
| ) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_user_has_profile_picture_image( |
| self, |
| user_has_profile_picture_url_url_df: pd.DataFrame, |
| url_df: pd.DataFrame, |
| image_df: pd.DataFrame, |
| ) -> pd.DataFrame: |
| """Extract (:User)-[:HAS_PROFILE_PICTURE]->(:Image) relation data. |
| |
| Args: |
| user_has_profile_picture_url_url_df (pd.DataFrame): |
| A dataframe of relations. |
| url_df (pd.DataFrame): |
| A dataframe of urls. |
| image_df (pd.DataFrame): |
| A dataframe of images. |
| |
| Returns: |
| pd.DataFrame: A dataframe of relations. |
| """ |
| if ( |
| self.include_extra_images |
| and len(image_df) |
| and len(url_df) |
| and len(user_has_profile_picture_url_url_df) |
| ): |
| merged = ( |
| user_has_profile_picture_url_url_df.rename( |
| columns=dict(src="user_idx", tgt="ul_idx") |
| ) |
| .merge( |
| url_df[["url"]].reset_index().rename(columns=dict(index="ul_idx")), |
| on="ul_idx", |
| ) |
| .merge( |
| image_df[["url"]] |
| .reset_index() |
| .rename(columns=dict(index="im_idx")), |
| on="url", |
| ) |
| ) |
| data_dict = dict(src=merged.user_idx.tolist(), tgt=merged.im_idx.tolist()) |
| return pd.DataFrame(data_dict) |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_hashtags( |
| self, tweet_df: pd.DataFrame, user_df: pd.DataFrame |
| ) -> pd.DataFrame: |
| """Extract (:Hashtag) node data. |
| |
| Args: |
| tweet_df (pd.DataFrame): |
| A dataframe of tweets. |
| user_df (pd.DataFrame): |
| A dataframe of users. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe of hashtags. |
| """ |
| if self.include_hashtags and len(tweet_df) and len(user_df): |
|
|
| |
| hashtag_df = pd.DataFrame() |
|
|
| def extract_hashtag(dcts: List[dict]) -> List[Union[str, None]]: |
| """Extracts hashtags from a list of dictionaries. |
| |
| Args: |
| dcts (list of dict): |
| A list of dictionaries. |
| |
| Returns: |
| list of str or None: |
| A list of hashtags. |
| """ |
| return [dct.get("tag") for dct in dcts] |
|
|
| |
| if "entities.hashtags" in tweet_df.columns: |
| hashtags = ( |
| tweet_df["entities.hashtags"] |
| .dropna() |
| .map(extract_hashtag) |
| .explode() |
| .tolist() |
| ) |
| hashtag_dict = pd.DataFrame(dict(tag=hashtags)) |
| hashtag_df = ( |
| pd.concat((hashtag_df, pd.DataFrame(hashtag_dict)), axis=0) |
| .drop_duplicates() |
| .reset_index(drop=True) |
| ) |
|
|
| |
| if "entities.description.hashtags" in user_df.columns: |
| hashtags = ( |
| user_df["entities.description.hashtags"] |
| .dropna() |
| .map(extract_hashtag) |
| .explode() |
| .tolist() |
| ) |
| hashtag_dict = pd.DataFrame(dict(tag=hashtags)) |
| hashtag_df = ( |
| pd.concat((hashtag_df, pd.DataFrame(hashtag_dict)), axis=0) |
| .drop_duplicates() |
| .reset_index(drop=True) |
| ) |
|
|
| return hashtag_df |
| else: |
| return pd.DataFrame() |
|
|
| def _extract_urls( |
| self, |
| tweet_dicusses_claim_df: pd.DataFrame, |
| user_posted_tweet_df: pd.DataFrame, |
| user_df: pd.DataFrame, |
| tweet_df: pd.DataFrame, |
| article_df: Optional[pd.DataFrame] = None, |
| image_df: Optional[pd.DataFrame] = None, |
| ) -> pd.DataFrame: |
| """Extract (:Url) node data. |
| |
| Note that this does not extract the top image urls from the articles. |
| This will be added with the _update_urls_from_articles method, after |
| the articles have been extracted. |
| |
| Args: |
| tweet_dicusses_claim_df (pd.DataFrame): |
| A dataframe of tweet_dicusses_claim nodes. |
| user_posted_tweet_df (pd.DataFrame): |
| A dataframe of user_posted_tweet nodes. |
| user_df (pd.DataFrame): |
| A dataframe of user nodes. |
| tweet_df (pd.DataFrame): |
| A dataframe of tweet nodes. |
| article_df (pd.DataFrame or None, optional): |
| A dataframe of article nodes, of None if no articles are |
| available. Defaults to None. |
| image_df (pd.DataFrame, or None, optional): |
| A dataframe of image nodes, or None if no images are available. |
| Defaults to None. |
| |
| Returns: |
| pd.DataFrame: A dataframe of url nodes. |
| """ |
| if ( |
| len(tweet_dicusses_claim_df) == 0 |
| or len(user_df) == 0 |
| or len(tweet_df) == 0 |
| or len(user_posted_tweet_df) == 0 |
| ): |
| return pd.DataFrame() |
|
|
| |
| is_src = tweet_df.index.isin(tweet_dicusses_claim_df.src.tolist()) |
| src_tweets = tweet_df[is_src] |
|
|
| |
| posted_rel = user_posted_tweet_df |
| is_src = posted_rel[posted_rel.tgt.isin(src_tweets.index.tolist())].src.tolist() |
| src_users = user_df.loc[is_src] |
|
|
| |
| url_df = pd.DataFrame() |
|
|
| |
| if "entities.urls" in tweet_df.columns: |
|
|
| def extract_url(dcts: List[dict]) -> List[Union[str, None]]: |
| """Extracts urls from a list of dictionaries. |
| |
| Args: |
| dcts (List[dict]): |
| A list of dictionaries. |
| |
| Returns: |
| List[Union[str, None]]: |
| A list of urls. |
| """ |
| return [dct.get("expanded_url") or dct.get("url") for dct in dcts] |
|
|
| urls = ( |
| src_tweets["entities.urls"].dropna().map(extract_url).explode().tolist() |
| ) |
| url_dict = pd.DataFrame(dict(url=urls)) |
| url_df = ( |
| pd.concat((url_df, pd.DataFrame(url_dict)), axis=0) |
| .drop_duplicates() |
| .reset_index(drop=True) |
| ) |
|
|
| |
| if "entities.url.urls" in user_df.columns: |
|
|
| def extract_url(dcts: List[dict]) -> List[Union[str, None]]: |
| """Extracts urls from a list of dictionaries. |
| |
| Args: |
| dcts (List[dict]): |
| A list of dictionaries. |
| |
| Returns: |
| List[Union[str, None]]: |
| A list of urls. |
| """ |
| return [dct.get("expanded_url") or dct.get("url") for dct in dcts] |
|
|
| urls = ( |
| src_users["entities.url.urls"] |
| .dropna() |
| .map(extract_url) |
| .explode() |
| .tolist() |
| ) |
| url_dict = pd.DataFrame(dict(url=urls)) |
| url_df = ( |
| pd.concat((url_df, pd.DataFrame(url_dict)), axis=0) |
| .drop_duplicates() |
| .reset_index(drop=True) |
| ) |
|
|
| |
| if "entities.description.urls" in user_df.columns: |
|
|
| def extract_url(dcts: List[dict]) -> List[Union[str, None]]: |
| """Extracts urls from a list of dictionaries. |
| |
| Args: |
| dcts (List[dict]): |
| A list of dictionaries. |
| |
| Returns: |
| List[Union[str, None]]: |
| A list of urls. |
| """ |
| return [dct.get("expanded_url") or dct.get("url") for dct in dcts] |
|
|
| urls = ( |
| src_users["entities.description.urls"] |
| .dropna() |
| .map(extract_url) |
| .explode() |
| .tolist() |
| ) |
| url_dict = pd.DataFrame(dict(url=urls)) |
| url_df = ( |
| pd.concat((url_df, pd.DataFrame(url_dict)), axis=0) |
| .drop_duplicates() |
| .reset_index(drop=True) |
| ) |
|
|
| |
| if ( |
| image_df is not None |
| and len(image_df) |
| and self.include_tweet_images |
| and "attachments.media_keys" in tweet_df.columns |
| ): |
| urls = ( |
| src_tweets[["attachments.media_keys"]] |
| .dropna() |
| .explode("attachments.media_keys") |
| .merge( |
| image_df[["media_key", "url"]], |
| left_on="attachments.media_keys", |
| right_on="media_key", |
| ) |
| .url.tolist() |
| ) |
| url_dict = pd.DataFrame(dict(url=urls)) |
| url_df = ( |
| pd.concat((url_df, pd.DataFrame(url_dict)), axis=0) |
| .drop_duplicates() |
| .reset_index(drop=True) |
| ) |
|
|
| |
| if self.include_extra_images and "profile_image_url" in user_df.columns: |
|
|
| def extract_url(dcts: List[dict]) -> List[Union[str, None]]: |
| """Extracts urls from a list of dictionaries. |
| |
| Args: |
| dcts (List[dict]): |
| A list of dictionaries. |
| |
| Returns: |
| List[Union[str, None]]: |
| A list of urls. |
| """ |
| return [dct.get("expanded_url") or dct.get("url") for dct in dcts] |
|
|
| urls = src_users["profile_image_url"].dropna().tolist() |
| url_dict = pd.DataFrame(dict(url=urls)) |
| url_df = ( |
| pd.concat((url_df, pd.DataFrame(url_dict)), axis=0) |
| .drop_duplicates() |
| .reset_index(drop=True) |
| ) |
|
|
| |
| if article_df is not None and len(article_df) and self.include_articles: |
| urls = article_df.url.dropna().tolist() |
| url_dict = pd.DataFrame(dict(url=urls)) |
| url_df = ( |
| pd.concat((url_df, pd.DataFrame(url_dict)), axis=0) |
| .drop_duplicates() |
| .reset_index(drop=True) |
| ) |
|
|
| return url_df |
|
|
| def _update_urls_from_articles( |
| self, url_df: pd.DataFrame, article_df: pd.DataFrame |
| ) -> pd.DataFrame: |
| """Updates the url_df with the urls from the articles. |
| |
| Args: |
| url_df (pd.DataFrame): |
| A dataframe with the urls. |
| article_df (pd.DataFrame): |
| A dataframe with the articles. |
| |
| Returns: |
| pd.DataFrame: |
| A dataframe with the urls. |
| """ |
| if self.include_extra_images and len(article_df) and len(url_df): |
| urls = article_df.top_image_url.dropna().tolist() |
| url_dict = pd.DataFrame(dict(url=urls)) |
| url_df = ( |
| pd.concat((url_df, pd.DataFrame(url_dict)), axis=0) |
| .drop_duplicates() |
| .reset_index(drop=True) |
| ) |
| return url_df |
|
|