簡體   English   中英

Python:使用 Spacy 等對名詞短語(例如介詞)進行分塊

[英]Python: Chunking others than noun phrases (e.g. prepositional) using Spacy, etc

自從有人告訴我 Spacy 是用於自然語音處理的如此強大的 Python 模塊,我現在正在拼命尋找一種方法來將單詞組合在一起,而不僅僅是名詞短語,最重要的是,介詞短語。 我懷疑是否有一個 Spacy 函數,但我猜這將是最簡單的方法(SpacySpaCy 導入已在我的項目中實現)。 盡管如此,我對短語識別/分塊的任何可能性持開放態度。

這是獲得PP的解決方案。 通常,您可以使用subtree獲取短語。

def get_pps(doc):
    "Function to get PPs from a parsed document."
    pps = []
    for token in doc:
        # Try this with other parts of speech for different subtrees.
        if token.pos_ == 'ADP':
            pp = ' '.join([tok.orth_ for tok in token.subtree])
            pps.append(pp)
    return pps

用法:

import spacy

nlp = spacy.load('en_core_web_sm')
ex = 'A short man in blue jeans is working in the kitchen.'
doc = nlp(ex)

print(get_pps(doc))

這打印:

['in blue jeans', 'in the kitchen']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM