示例#1
0
    def test_two_sentences(self) -> None:
        """Two sentences should be linked by 'mais'."""

        self.assertEqual(
            "Vous avez de l'expérience mais vous êtes vieux.",
            french.join_sentences_properly(
                ["vous avez de l'expérience", 'vous êtes vieux']))
示例#2
0
    def test_double_sentences(self) -> None:
        """Internal punctation/capitalization should be kept."""

        self.assertEqual(
            'Votre recherche est sur la bonne voie mais votre projet est compliqué. \
Il faut attendre.',
            french.join_sentences_properly([
                'votre recherche est sur la bonne voie',
                'votre projet est compliqué. Il faut attendre',
            ]))
示例#3
0
    def test_empty_list(self) -> None:
        """Empty list should return empty string."""

        self.assertEqual('', french.join_sentences_properly([]))
示例#4
0
    def test_one_sentence(self) -> None:
        """One sentence list should be stripped, capitalized and punctuated."""

        self.assertEqual(
            'Bonjour, le monde.',
            french.join_sentences_properly(['bonjour, le monde   ']))
示例#5
0
def _compute_diagnostic_topic_score_and_text(topic, scorers, scoring_project):
    """Create the score and text for a given diagnostic submetric on a given project.

    Args:
        topic: the diagnostic topic we wish to evaluate
        scorers: a list of scorers for the given topic, with their template sentences
        scoring_project: the project we want to score
    Returns:
        the populated subdiagnostic protobuf.
    """

    topic_score = 0
    topic_weight = 0
    min_score = None
    max_score = None
    max_scorer = None
    min_scorer = None
    for scorer in scorers:
        model = scoring.get_scoring_model(scorer.trigger_scoring_model)
        if not model:
            logging.error(
                'Diagnostic for topic "%s" uses the scoring model "%s" which does not exist.',
                diagnostic_pb2.DiagnosticTopic.Name(topic),
                scorer.trigger_scoring_model)
            continue
        try:
            score = model.score(scoring_project)
        except scoring.NotEnoughDataException:
            continue
        # Use default weight of 1
        weight = scorer.weight or 1
        weighted_score = score * weight
        topic_score += weighted_score
        topic_weight += weight
        # Use positive sentence only for scores above average.
        if score > _SCORE_AVERAGE:
            positive_score = (score - _SCORE_AVERAGE) * weight
            if max_score is None or positive_score > max_score:
                max_score = positive_score
                max_scorer = scorer
        # Use negative sentence only for scores below average.
        else:
            negative_score = (_SCORE_AVERAGE - score) * weight
            if min_score is None or negative_score > min_score:
                min_score = negative_score
                min_scorer = scorer
    if not topic_weight:
        return None

    sub_diagnostic = diagnostic_pb2.SubDiagnostic(
        topic=topic, score=round(topic_score / topic_weight * 100 / 3))
    sentences = []

    def _append_sentence(template):
        translated_template = scoring_project.translate_string(template)
        sentences.append(
            scoring_project.populate_template(translated_template))

    # Do not put positive sentence if score is below 40.
    if max_scorer and sub_diagnostic.score > 40:
        _append_sentence(max_scorer.positive_sentence_template)
    # Do not put negative sentence if score is above 80.
    if min_scorer and sub_diagnostic.score < 80:
        _append_sentence(min_scorer.negative_sentence_template)

    sub_diagnostic.text = french.join_sentences_properly(sentences)

    return sub_diagnostic