<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Simple Tools from Extracting Quantities from Strings
Suppose we have a report and we want to find the sentences that are talking about numerical things....
Originally inspired by When you get data in sentences
Step1:  quantulum3
quantulum3 is a Python package "for information extraction of quantities from unstructured text".
Step2:  Finding quantity statements in large texts
If we have a large block of text, we might want to quickly skim it for quantity containing sentences, we can do something like the following...
Step3:  Annotating a dataset
Can we extract numbers from sentences in a CSV file? Yes we can...
Step4:  We could then do something to split multiple amounts into multiple rows or columns...
Parsing Semi-Structured Sentences
The sentencing sentences look to have a reasonable degree of structure to them (or at least, there are some commenalities in the way some of them are structured).
We can exploit this structure by writing some more specific pattern matches to pull out even more information.
<ASSISTANT_TASK:>
Python Code:
sentences = [
    '4 years and 6 months’ imprisonment with a licence extension of 2 years and 6 months',
    'No quantities here',
    'I measured it as 2 meters and 30 centimeters.',
    "four years and six months' imprisonment with a licence extension of 2 years and 6 months",
    'it cost £250... bargain...',
    'it weighs four hundred kilograms.',
    'It weighs 400kg.',
    'three million, two hundred & forty, you say?',
    'it weighs four hundred and twenty kilograms.'
    
]
Explanation: Simple Tools from Extracting Quantities from Strings
Suppose we have a report and we want to find the sentences that are talking about numerical things....
Originally inspired by When you get data in sentences: how to use a spreadsheet to extract numbers from phrases, Paul Bradshaw, Online Journalism blog, from which some of the example sentences (sic!) are taken.
Distribution: https://twitter.com/paulbradshaw/status/1158752556958519297
Potentially Useful Python Packages
quantulum: extract quantities from natural language text;
ctparse: extract time / date related quantities from natural language text;
r1chardj0n3s/parse: easy scrape / regex extraction from semi-structred text using format() like patterns; example use here;
dateparser [docs]: "easily parse localized dates in almost any string formats commonly found on web pages" (includes foreign language detection);
invoice2data:
Example Sentences
Make a start on some sample test sentences...
End of explanation
#!pip3 install quantulum3
from quantulum3 import parser
for sent in sentences:
    print(sent)
    p = parser.parse(sent)
    if p:
        print('\tSpoken:',parser.inline_parse_and_expand(sent))
        print('\tNumeric elements:')
        for q in p:
            display(q)
            print('\t\t{} :: {}'.format(q.surface, q))
    print('\n---------\n')
Explanation: quantulum3
quantulum3 is a Python package "for information extraction of quantities from unstructured text".
End of explanation
import spacy
nlp = spacy.load('en_core_web_lg', disable = ['ner'])
text = '''
Once upon a time, there was a thing. The thing weighed forty kilogrammes and cost £250. 
It was blue. It took forty five minutes to get it home. 
What a day that was. I didn't get back until 2.15pm. Then I had some cake for tea.
'''
doc = nlp(text)
for sent in doc.sents:
    print(sent)
for sent in doc.sents:
    sent = sent.text
    p = parser.parse(sent)
    if p:
        print('\tSpoken:',parser.inline_parse_and_expand(sent))
        print('\tNumeric elements:')
        for q in p:
            display(q)
            print('\t\t{} :: {}'.format(q.surface, q))
    print('\n---------\n')
Explanation: Finding quantity statements in large texts
If we have a large block of text, we might want to quickly skim it for quantity containing sentences, we can do something like the following...
End of explanation
url = 'https://raw.githubusercontent.com/BBC-Data-Unit/unduly-lenient-sentences/master/ULS%20for%20Sankey.csv'
import pandas as pd
df = pd.read_csv(url)
df.head()
#get a row
df.iloc[1]
#and a, erm. sentence...
df.iloc[1]['Original sentence (refined)']
parser.parse(df.iloc[1]['Original sentence (refined)'])
def amountify(txt):
    #txt may be some flavout of nan...
    #handle scruffily for now...
    try:
        if txt:
            p = parser.parse(txt)
            x=[]
            for q in p:
                x.append( '{} {}'.format(q.value, q.unit.name))
            return '::'.join(x)
        return ''
    except:
        return
df['amounts'] = df['Original sentence (refined)'].apply(amountify)
df.head()
Explanation: Annotating a dataset
Can we extract numbers from sentences in a CSV file? Yes we can...
End of explanation
df['Original sentence (refined)'][:20].apply(print);
Explanation: We could then do something to split multiple amounts into multiple rows or columns...
Parsing Semi-Structured Sentences
The sentencing sentences look to have a reasonable degree of structure to them (or at least, there are some commenalities in the way some of them are structured).
We can exploit this structure by writing some more specific pattern matches to pull out even more information.
End of explanation
<END_TASK>