Section alignment¶
Generate image suggestions for Wikipedia article sections based on aligned section titles.
Inputs:
Wikipedia article section images as output by
section_topics.pipelinemachine-learned section alignment dataset of (source, target) section title pairs
High-level steps given a target section:
skip it if it has more images than a threshold
gather all equivalent source pages with the same Wikidata QID as the target page
filter out irrelevant images from the source sections, namely icons or those appearing in the target too
project all source section images to the target
combine suggestion candidates
Output pyspark.sql.DataFrame row example:
target_id |
target_heading |
item_id |
target_title |
target_index |
recommended_images |
target_wiki_db |
|---|---|---|---|---|---|---|
1485753 |
Biografia |
Q317475 |
Ali_Shariati |
1 |
[{frwiki -> [Shariati7.jpg]}, {ruwiki -> [Shariati7.jpg]}, …] |
ptwiki |
More documentation lives in MediaWiki.
- class section_topics.section_alignment_image_suggestions.Page(item_id, page_id, page_title, wiki_db, section_images)[source]¶
dataclasses.dataclass()that stores a Wikipedia article page and its available images.
- class section_topics.section_alignment_image_suggestions.Recommendation(item_id, target_id, target_title, target_index, target_heading, target_wiki_db, source_heading, source_wiki_db, recommended_images)[source]¶
dataclasses.dataclass()that stores image suggestions for a target Wikipedia article page.- Parameters:
item_id (
str) – a page Wikidata QIDtarget_id (
int) – a page IDtarget_title (
str) – a page titletarget_index (
int) – a section numerical indextarget_heading (
str) – a section headingtarget_wiki_db (
str) – a page wikisource_heading (
str) – a section heading where suggestions come fromsource_wiki_db (
str) – a page wiki where suggestions come fromrecommended_images (
List[str]) – a list of suggested image file names
- section_topics.section_alignment_image_suggestions.load(article_images)[source]¶
Convert section images from an
section_topics.pipeline’s output row to aSectionImagesinstance.- Parameters:
article_images (
str) – a stringified JSON array of section images. Corresponds to a value of thearticle_imagescolumn insection_topics.pipeline’s outputpyspark.sql.DataFrame- Return type:
List[SectionImages]- Returns:
the list of converted section images
- section_topics.section_alignment_image_suggestions.rows_to_pages(df)[source]¶
Convert all input
pandas.DataFramerows toPageinstances.- Parameters:
df (
DataFrame) – an in-memory dataframe loaded fromsection_topics.pipeline’s outputpyspark.sql.DataFrame- Return type:
- Returns:
the list of pages
- section_topics.section_alignment_image_suggestions.filter_source_images(source_images, target_images)[source]¶
Filter out source images that either appear in target ones or are icons/indicators.
Icon and indicator file names have a
OOjs_UI_iconandOOjs_UI_indicatorprefix respectively.
- section_topics.section_alignment_image_suggestions.make_recommendations(target_page, source_page, max_target_images)[source]¶
Generate all image suggestion candidates for a target page via the Cartesian product of all (target, source) section pairs.
Filter source images through
filter_source_images(). Filter target sections with more images than a given threshold.- Parameters:
- Return type:
- Returns:
the list of image suggestion candidates
- section_topics.section_alignment_image_suggestions.combine_pages(target_pages, source_pages)[source]¶
Generate all (target, source) page pairs via their Cartesian product.
Ensure that no pair has the same language.
- section_topics.section_alignment_image_suggestions.generate_image_recommendations(target_wiki_dbs, max_target_images, df)[source]¶
Generate all possible suggestions for pages in the target wikis.
Filter out empty ones.
- section_topics.section_alignment_image_suggestions.get_image_recommendations(section_images_df, target_wiki_dbs, max_target_images)[source]¶
Generate the full dataset of all possible suggestions for all sections of all pages in the target wikis.
Corresponding source pages have the same Wikidata QID as a given target page.
- Parameters:
- Return type:
DataFrame- Returns:
the distributed dataframe of image suggestions
- section_topics.section_alignment_image_suggestions.normalize_heading_column(column, substitute_pattern='[\\\\s_]', strip_chars='!"#$%&\\' *+, -./:;<=>?@[\\\\]^_`{|}~')[source]¶
Same as
section_topics.pipeline.normalize_heading_column().- Return type:
Column
- section_topics.section_alignment_image_suggestions.process_image_recommendations(recommendations_df, alignments_df)[source]¶
Build the final output dataset of image suggestions.
Combine all suggestion candidates with aligned sections.
- Parameters:
recommendations_df (
DataFrame) – a distributed dataframe of all image suggestion candidatesalignments_df (
DataFrame) – a distributed dataframe of section alignments
- Return type:
DataFrame- Returns:
the distributed dataframe of final image suggestions