コード例 #1
0
ファイル: aggregation.py プロジェクト: roman-kutlak/nlglib
def try_to_aggregate(sent1, sent2, marker='and'):
    """ Attempt to combine two elements into one by replacing the differing
    elements by a conjunction.

    """
    if sent1 is None or sent2 is None: return None
    if not isinstance(sent1, Clause) or not isinstance(sent2, Clause):
        return None

    replacement = Word("REPLACEMENT", "REPLACEMENT")
    for e1 in sentence_iterator(sent1):
        s1 = deepcopy(sent1)
        s1.replace(e1, replacement) # replace one element

        for e2 in sentence_iterator(sent2):
            s2 = deepcopy(sent2)
            s2.replace(e2, replacement) #replace one element

            if s1.subj is None:
                if s1.vp == replacement:
                    continue
                if (isinstance(s1.vp, Phrase) and
                    isinstance(s2.vp, Phrase) and
                    s1.vp.head != s2.vp.head):
                    continue

            # if sentences are equal (eg s1: load x; s2: load x) aggregate
            if (s1 == s2):
                get_log().debug('Aggregating:\n\t%s\n\t%s' % (str(s1), str(s2)))
                cc = add_elements(e1, e2, conj=marker)
                s1.replace(replacement, cc)
                get_log().debug('Result of aggregation:\n%s' % repr(s1))
                return s1
#            else:
#                print('Did not aggregate:\n\t%s\n\t%s' % (str(s1), str(s2)))
#                if (str(s1) == str(s2)):
#                    print('s1 == s2: %s' % str(s1 == s2))
    return None
コード例 #2
0
ファイル: aggregation.py プロジェクト: roman-kutlak/nlglib
def _can_aggregate(message, max):
    """ Return true if this message can be aggregated.
    max - maximum number of coordinates in a coordingated clause
    If the message does not have a coordinated clause or if the number
    of coordinates is less then 'max', return True.

    """
    if message is None: return False
    for part in sentence_iterator(message):
        if not isinstance(part, Coordination):
            continue
        else:
            return (len(part.coords) < max)
    # simple sentence - no coordinated clause
    return True