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

如何利用NLP识别语句中的施害者与受害者?基于SpaCy的主语识别进阶需求咨询

Distinguishing Attackers vs. Victims in Active/Passive Sentences: Solutions & Research Directions

Hey there! It’s awesome to see you diving into NLP and tackling this practical problem of identifying attackers and victims across different sentence structures. Your existing spaCy work already highlights a key challenge: syntactic roles (like nsubj vs nsubjpass) shift with active/passive voice, but we need consistent semantic roles to reliably tell who is attacking vs who is being attacked. Let’s break down immediate fixes and structured research paths to explore.

Immediate Fix: Leverage SpaCy’s Dependency Labels

First, let’s build on your current code to map syntactic dependencies directly to the semantic roles you care about (attacker/victim):

  • For active voice sentences (e.g., "China attacked the UK"):
    • The nsubj (nominal subject) is the attacker (the agent performing the action)
    • The dobj (direct object) is the victim (the recipient of the action)
  • For passive voice sentences (e.g., "The UK was attacked by China"):
    • The nsubjpass (passive nominal subject) is the victim
    • The pobj (object of preposition) attached to the preposition by is the attacker

Here’s modified code to automate this logic:

import spacy

nlp = spacy.load("en_core_web_sm")
doc1 = nlp("China attacked the UK over several weeks")
doc2 = nlp("The UK was attacked by China over several weeks")
docs = [doc1, doc2]

for doc in docs:
    print("============")
    print(f"Sentence: {doc.text}")
    attacker = None
    victim = None
    for token in doc:
        # Handle active voice cases
        if token.dep_ == "nsubj" and token.head.pos_ == "VERB":
            if token.head.lemma_ == "attack":  # Use lemma to cover "attack", "attacks", etc.
                attacker = token.text
                # Locate direct object as victim
                for child in token.head.children:
                    if child.dep_ == "dobj":
                        victim = child.text
        # Handle passive voice cases
        elif token.dep_ == "nsubjpass" and token.head.pos_ == "VERB":
            if token.head.lemma_ == "attack":
                victim = token.text
                # Locate 'by' preposition's object as attacker
                for child in token.head.children:
                    if child.dep_ == "agent":  # SpaCy tags 'by' as the agent marker in passive
                        for grandchild in child.children:
                            if grandchild.dep_ == "pobj":
                                attacker = grandchild.text
    print(f"Attacker: {attacker}, Victim: {victim}")

This will output:

============
Sentence: China attacked the UK over several weeks
Attacker: China, Victim: UK
============
Sentence: The UK was attacked by China over several weeks
Attacker: China, Victim: UK

Research Directions to Deepen Your Work

Now that you have a rule-based baseline, here are structured research areas to explore for more robust, scalable solutions:

1. Semantic Role Labeling (SRL)

SRL moves beyond syntactic dependencies to assign semantic roles (like Agent, Patient, Theme) to sentence components, regardless of sentence structure. For your problem:

  • The Agent role will always correspond to the attacker
  • The Patient role will always correspond to the victim

You can explore:

  • SpaCy extensions for SRL (e.g., the spacy-srl package)
  • Pre-trained SRL models from frameworks like AllenNLP or Hugging Face Transformers. These models can label roles for any verb, so you won’t need hardcoded checks for specific words like "attacked".
  • Foundational SRL frameworks like PropBank, which defines standard role sets for thousands of verbs.

2. Domain-Specific Event Extraction

Your problem falls under event extraction—identifying specific events (like "attack") and their key participants. This is more targeted than general SRL and works exceptionally well for niche tasks:

  • Start with rule-based event extraction: Define patterns for attack events (trigger words: "attack", "strike", "invade"; roles: Attacker, Victim) using spaCy’s Matcher or regex.
  • Move to machine learning approaches: Fine-tune pre-trained language models (BERT, RoBERTa) on event extraction datasets focused on conflict/security (e.g., the ACE Event Corpus, or a custom dataset you curate from news articles). These models can automatically learn to identify attackers and victims even in complex, ambiguous sentences.

3. Syntactic Normalization

Another angle is to normalize all sentences to a consistent structure (e.g., convert passive sentences to active voice) so you can reuse the same role-identification logic across all inputs. You can:

  • Use spaCy’s dependency tree to rewrite passive sentences into active form. For example, swap the nsubjpass and by-attached pobj, and adjust the verb tense to match.
  • Explore tools like the Stanford Parser’s sentence rewriting capabilities, or build a small transformer model dedicated to syntactic conversion.

4. Coreference Resolution (For Longer Texts)

As you work with extended texts, you’ll encounter pronouns (e.g., "They attacked them") that refer to entities mentioned earlier. Adding coreference resolution will let you link these pronouns back to their original attackers/victims. SpaCy has a coreference extension (spacy-coref), and BERT-based coref resolvers can handle this task with high accuracy.

Practical Next Steps

  • Start by expanding your rule-based code to cover more verbs (using lemmas instead of exact word matches) and edge cases (e.g., sentences with multiple participants).
  • Experiment with a pre-trained SRL model to see how it labels roles for your sentences—this will help you grasp how semantic roles abstract away syntactic differences.
  • If you want to dive into machine learning, try fine-tuning a small BERT model on a custom dataset of attack-related sentences (with labeled attackers and victims) using Hugging Face’s transformers library.

内容的提问来源于stack exchange,提问作者Luke Price

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 11:07:41