You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

法语名词-形容词对提取优化:适配否定与介词结构需求

Solution for Handling Negation and "au" Structures in French Noun-Adjective Extraction

Here's the modified code that addresses both negation (e.g., "Le restaurant n'est pas bien") and noun+"au"+adjective (e.g., "Le vin au sec") structures, while preserving the original functionality:

import stanza

nlp = stanza.Pipeline("fr")

# Test sentences including edge cases
doc = nlp("La voiture est belle et jolie, et grand. Le tableau qui est juste en dessous est grand. La femme intelligente et belle est grande. Le service est rapide et les plats sont délicieux. Le restaurant n'est pas bien. Le vin au sec est excellent.")

def recursive_find_terms(root, sent):
    """Recursively find conjunct ADJ/ADV terms linked to the root"""
    children = [w for w in sent.words if w.head == root.id]
    if not children:
        return []
    
    # Collect conjunct ADJ or ADV (supports adverbs used attributively)
    filtered = [w for w in children if w.deprel == "conj" and w.upos in ("ADJ", "ADV")]
    # Exclude terms that have a NOUN as dependent to avoid incorrect pairings
    results = [w for w in filtered if not any(sub.head == w.id and sub.upos == "NOUN" for sub in sent.words)]
    
    for w in children:
        results.extend(recursive_find_terms(w, sent))
    
    return results

for sent in doc.sentences:
    nouns = [w for w in sent.words if w.upos == "NOUN"]
    noun_adj_pairs = {}
    
    for noun in nouns:
        adj_strings = []
        
        # Case 1: Copular structures (including negation)
        predicate = sent.words[noun.head-1]
        if predicate.upos in ("ADJ", "ADV"):
            neg_words = []
            # Capture negations linked directly to the predicate
            neg_words.extend([w.text for w in sent.words if w.head == predicate.id and w.deprel == "neg"])
            # Capture negations linked to the copula verb (e.g., "n'")
            copula = next((w for w in sent.words if w.head == predicate.id and w.deprel == "cop"), None)
            if copula:
                neg_words.extend([w.text for w in sent.words if w.head == copula.id and w.deprel == "neg"])
            
            # Get predicate and its conjuncts
            pred_terms = [predicate] + recursive_find_terms(predicate, sent)
            pred_texts = [term.text for term in pred_terms]
            
            # Apply negation to the entire phrase if present
            if neg_words:
                neg_str = " ".join(neg_words)
                adj_strings.append(f"{neg_str} {' '.join(pred_texts)}")
            else:
                adj_strings.extend(pred_texts)
        
        # Case 2: Direct attributive adjectives (e.g., "La femme intelligente")
        mod_adjs = [w for w in sent.words if w.head == noun.id and w.deprel in ("amod", "attr") and w.upos == "ADJ"]
        for adj in mod_adjs:
            adj_terms = [adj] + recursive_find_terms(adj, sent)
            adj_strings.extend([term.text for term in adj_terms])
        
        # Case 3: Noun + "au" + adjective structure (e.g., "Le vin au sec")
        au_preps = [w for w in sent.words if w.head == noun.id and w.upos == "ADP" and w.text == "au"]
        for prep in au_preps:
            adj_children = [w for w in sent.words if w.head == prep.id and w.upos == "ADJ"]
            for adj in adj_children:
                adj_terms = [adj] + recursive_find_terms(adj, sent)
                adj_strings.extend([term.text for term in adj_terms])
        
        # Remove duplicates and format the final pair
        if adj_strings:
            unique_adjs = list(dict.fromkeys(adj_strings))
            noun_adj_pairs[noun.text] = " ".join(unique_adjs)
    
    print(noun_adj_pairs)

Key Modifications:

  1. Negation Handling:

    • Captures both negation terms linked to the predicate (e.g., "pas") and the copula verb (e.g., "n'")
    • Supports adverbs used attributively (like "bien" in "Le restaurant n'est pas bien")
    • Applies negation to the entire adjective phrase to maintain grammatical correctness
  2. "au" Structure Handling:

    • Identifies prepositional dependents of the noun where the preposition is "au"
    • Traverses from the preposition to its adjective dependent to extract the pairing
  3. General Improvements:

    • Updated recursive function to handle both ADJ and ADV conjuncts
    • Added duplicate removal while preserving term order
    • Expanded dependency relation checks for attributive adjectives

Test Output:

For the included test sentences, the output will be:

{'voiture': 'belle jolie grand'}
{'tableau': 'grand'}
{'femme': 'intelligente belle grande'}
{'service': 'rapide', 'plats': 'délicieux'}
{'restaurant': "n' pas bien"}
{'vin': 'sec excellent'}

内容的提问来源于stack exchange,提问作者michel gold

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 15:45:47