【问题标题】:Extract two consecutive nouns using spaCy使用 spaCy 提取两个连续名词
【发布时间】:2021-09-06 17:25:21
【问题描述】:

这是一个简单的数据集:

import pandas as pd
product = ['knife', 'box set', 'beautiful jewellery set on sale', 'green']
df = pd.DataFrame(product, columns = ['product_name'])
df

输出如下:

product_name
0 knife
1 box set
2 beautiful jewellery set on sale
3 green

如果需要,我想通过提取两个连续的名词来对这些产品进行分类。到目前为止,我有以下内容,但在所有情况下,类别仅由一个名词表示:

!pip install -q --upgrade spacy

import spacy
nlp = spacy.load('en_core_web_sm')

category=[]
for i in df['product_name'].tolist():
    doc = nlp(i)
    for t in doc:
      if t.pos_ in ['NOUN']:
        category.append(f'{t}')
        break
    if t.pos_ not in ['NOUN']:
      category.append('NaN')

df1 = pd.DataFrame(category, columns =['product_category'])
df1

我的输出:

product_category
0 knife
1 set
2 jewellery
3 NaN

预期输出:

product_category
0 knife
1 box set
2 jewellery set
3 NaN

如果两个名词一个接一个,是否可以在代码中引入一些附加条件来提取两个名词?

【问题讨论】:

  • matcher与相应的模式一起使用。
  • @WiktorStribiżew 非常感谢 Wiktor 的帮助。我会开始学习你展示的方法。
  • 你能澄清一下你想要什么输出吗?我在上面的链接中显示的那个?
  • @WiktorStribiżew 我已经更新了问题并包含了预期的输出。

标签: python pandas spacy


【解决方案1】:

你可以使用

import spacy
import pandas as pd
import numpy as np

product = ['knife', 'box set', 'beautiful jewellery set on sale', 'green']
df = pd.DataFrame(product, columns = ['product_name'])

nlp = spacy.load('en_core_web_sm')
matcher = spacy.matcher.Matcher(nlp.vocab)
pattern = [{'POS': 'NOUN'},{'POS': 'NOUN','OP':'?'}]
matcher.add('NOUN_PATTERN', [pattern])

def get_two_nouns(x):
    doc = nlp(x)
    results = []
    for match_id, start, end in matcher(doc):
        span = doc[start:end]
        results.append(span.text)
    return max(results, key = lambda x: len(x.split()), default=np.nan)

df['product_name'].apply(get_two_nouns)

输出:

0            knife
1          box set
2    jewellery set
3              NaN
Name: product_name, dtype: object

pattern = [{'POS': 'NOUN'},{'POS': 'NOUN','OP':'?'}] 模式匹配(组合)都是NOUNs 的标记。第二个是可选的,因为 OP 运算符设置为 ?

return max(results, key = lambda x: len(x.split()), default=np.nan) 部分返回长度最长的项目(此处以空格分隔的标记计数测量长度)。

【讨论】:

    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多