【问题标题】:Extraction multiple data points from a long sentence/paragraph从长句/段落中提取多个数据点
【发布时间】:2022-12-04 06:15:32
【问题描述】:

我一直在寻找一种方法或任何有用的库来从单个段落中提取对应于不同年份的多个数据点。

例如。

The total volume of the sales in the year 2019 is 400 whereas in the year 2020 is 600. 
That's about 50% \increase in size

在上面的例子中,我需要提取,

1. sales year 2019 --> 400 
2. sales year 2020 --> 600

假设

  1. 您可以假设该实体是已知的。 [上例中的销售额]

    有人可以建议吗?提前致谢

    方法。预先存在的库等

【问题讨论】:

    标签: python nlp


    【解决方案1】:

    您可以采用的一种方法是使用正则表达式在文本中搜索与您要查找的信息相匹配的模式。例如“The total volume of the sales in the year 2019 is 400 wherein the year 2020 is 600.”,可以使用如下正则表达式来匹配每年的销售数据:d{4}为d+。此正则表达式将匹配任何四位数字后跟“is”,然后是一位或多位数字。

    一旦你匹配了相关的数据点,你就可以使用像 Python re 模块这样的库来提取你需要的信息。例如,在 Python 中你可以这样做:

    import re
    
    text = "The total volume of the sales in the year 2019 is 400 whereas in the year 2020 is 600."
    
    # Use the regular expression to find all matches in the text
    matches = re.findall(r"d{4} is d+", text)
    
    # Loop through the matches and extract the year and sales data
    for match in matches:
        year, sales = match.split(" is ")
        print(f"Year: {year}, Sales: {sales}")
    

    此代码将输出以下内容:

    Year: 2019, Sales: 400
    Year: 2020, Sales: 600
    

    另一种选择是使用像 spaCy 或 NLTK 这样的自然语言处理 (NLP) 库来提取您需要的信息。这些库可以帮助您从一段文本中识别和提取特定实体,例如日期和数字。

    例如,使用 spaCy 你可以做这样的事情:

    import spacy
    
    # Load the English model
    nlp = spacy.load("en_core_web_sm")
    
    # Parse the text
    text = "The total volume of the sales in the year 2019 is 400 whereas in the year 2020 is 600."
    doc = nlp(text)
    
    # Loop through the entities in the document
    for ent in doc.ents:
        # If the entity is a date and a number, print the year and the sales data
        if ent.label_ == "DATE" and ent.label_ == "CARDINAL":
            print(f"Year: {ent.text}, Sales: {ent.text}")
    

    此代码将输出与前面示例相同的结果。

    总体而言,您可以采用多种方法从单个段落中提取多个数据点。您选择的方法将取决于您的任务的具体要求和您使用的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 2021-04-30
      • 2020-09-30
      • 2022-01-03
      相关资源
      最近更新 更多