<commit_before>package MediaWords::DBI::Stories::AP;

# routines for determining whether a given story is syndicated from the Associated Press.
# the algorithm used in this module was developed using a decision tree algorithm:
#
#           'ap_mentions_sentences',
#             '1' => '1', X
#             '0' => [ X
#                    'associated_press_mentions',
#                      '1' => [ X
#                             'quoted_associated_press_first_quarter_mentions',
#                               '1' => '1', X
#                               '0' => [ X
#                                      'dup_sentences_32',
#                                        '1' => '1', X
#                                        '0' => [ X
#                                               'associated_press_near_title',
#                                                 '1' => '1', X
#                                                 '0' => [ X
#                                                        'ap_news_mentions',
#                                                          '1' => '1', X
#                                                          '0' => [ X
#                                                                 'ap_mentions',
#                                                                   '1' => '1', X
#                                                                   '0' => '0' X
#                                        '2' => [ X
#                                                 'associated_press_near_title', X
#                                                   '1' => '1', X
#                                                   '0' => [ X
#                                                          'associated_press_tag_mentions', X
#                                                            '1' => '0', X
#                                                            '0' => '1' X
#                      '0' => [
#                             'dup_sentences_32',
#                               '1' => [
#                                        'ap_mentions',
#                                          '1' => [
#                                                 'ap_mentions_uppercase_location',
#                                                   '1' => '1',
#                                                   '0' => '0'
#                                          '0' => '0'
#                               '0' => '0',
#                               '2' => '1'

use strict;
use warnings;

use Modern::Perl "2015";
use MediaWords::CommonLibs;
use MediaWords::DBI::Downloads;
use MediaWords::Util::CSV;

use Digest::MD5 qw(md5);

sub _get_story_content
{
    my ( $db, $story ) = @_;

    if ( $story->{ content } )
    {
        return $story->{ content };
    }

    my $download = $story->{ download };
    if ( !$download )
    {
        $download = $db->query( <<SQL, $story->{ stories_id } )->hash;
select * from downloads where stories_id = \$1 order by downloads_id limit 1
SQL
    }

    return '' unless ( $download->{ state } eq 'success' );

    my $content_ref;

    eval { $content_ref = MediaWords::DBI::Downloads::fetch_content( $db, $download ) };
    if ( $@ || !$content_ref )
    {
        WARN "error fetching content: $@";
        return 0;
    }
<commit_msg>Skip AP stuff if there's no content download around<commit_after>package MediaWords::DBI::Stories::AP;

# routines for determining whether a given story is syndicated from the Associated Press.
# the algorithm used in this module was developed using a decision tree algorithm:
#
#           'ap_mentions_sentences',
#             '1' => '1', X
#             '0' => [ X
#                    'associated_press_mentions',
#                      '1' => [ X
#                             'quoted_associated_press_first_quarter_mentions',
#                               '1' => '1', X
#                               '0' => [ X
#                                      'dup_sentences_32',
#                                        '1' => '1', X
#                                        '0' => [ X
#                                               'associated_press_near_title',
#                                                 '1' => '1', X
#                                                 '0' => [ X
#                                                        'ap_news_mentions',
#                                                          '1' => '1', X
#                                                          '0' => [ X
#                                                                 'ap_mentions',
#                                                                   '1' => '1', X
#                                                                   '0' => '0' X
#                                        '2' => [ X
#                                                 'associated_press_near_title', X
#                                                   '1' => '1', X
#                                                   '0' => [ X
#                                                          'associated_press_tag_mentions', X
#                                                            '1' => '0', X
#                                                            '0' => '1' X
#                      '0' => [
#                             'dup_sentences_32',
#                               '1' => [
#                                        'ap_mentions',
#                                          '1' => [
#                                                 'ap_mentions_uppercase_location',
#                                                   '1' => '1',
#                                                   '0' => '0'
#                                          '0' => '0'
#                               '0' => '0',
#                               '2' => '1'

use strict;
use warnings;

use Modern::Perl "2015";
use MediaWords::CommonLibs;
use MediaWords::DBI::Downloads;
use MediaWords::Util::CSV;

use Digest::MD5 qw(md5);

sub _get_story_content
{
    my ( $db, $story ) = @_;

    if ( $story->{ content } )
    {
        return $story->{ content };
    }

    my $download = $story->{ download };
    if ( !$download )
    {
        $download = $db->query( <<SQL, $story->{ stories_id } )->hash;
select * from downloads where stories_id = \$1 order by downloads_id limit 1
SQL
    }

    # There might be no download at all for full text RSS story (e.g. Superglue)
    return '' unless ( $download );

    return '' unless ( $download->{ state } eq 'success' );

    my $content_ref;

    eval { $content_ref = MediaWords::DBI::Downloads::fetch_content( $db, $download ) };
    if ( $@ || !$content_ref )
    {
        WARN "error fetching content: $@";
        return 0;
    }

{'fraction_non_alphanumeric': 0.11261792452830188, 'fraction_numerical': 0.015919811320754717, 'mean_word_length': 0.7233934467868935, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 133, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4531853', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1472, 'n_words': 371}