Extract information from a text using SpaCy

Viewed 91

I want to build a model that extracts personal data collected by a website.

The first step, I scrapped the privacy policy of a website, then I split it into sentences and put them on a dataframe as shown in the image below:

image

From those sentences, I only want to extract those that contains the words "personal information"

I used the code below but I don't get the result I want:

def find_names(text):

    names = []

    # spacy doc
    doc = nlp(text)

    # pattern
    pattern = [{'LOWER':'personal'},
          {'LOWER':'data'}]
            
    # Matcher class object 
    matcher = Matcher(nlp.vocab) 
    matcher.add("names", [pattern]) 

    matches = matcher(doc)


    # finding patterns in the text
    for i in range(0,len(matches)):
    
        # match: id, start, end
        token = doc[matches[i][1]:matches[i][2]]
        # append token to list
        names.append(str(token))


    return names

# apply function
df2['PM_Names'] = df2['Sent'].apply(find_names)

The output:

image

I want to extract the whole sentence with the words "personal information" only.

0 Answers
Related