File size: 3,257 Bytes
b2fdf59
 
 
5bead87
 
 
 
 
 
b2fdf59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bead87
 
b2fdf59
 
 
 
 
 
 
5bead87
b2fdf59
5bead87
 
b2fdf59
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from transformers import BigBirdPegasusForConditionalGeneration, AutoTokenizer

# by default encoder-attention is `block_sparse` with num_random_blocks=3, block_size=64

# TODO: add pre-trained summarizer models
# Placeholder text for testing input
test_text = """
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
"""
summary_options = ["Abstract", "Background", "Claims"]

def get_word_index(s, idx):
    words = re.findall(r'\s*\S+\s*', s)
    return sum(map(len, words[:idx])) + len(words[idx]) - len(words[idx].lstrip())


class PatentSummarizer():
    def __init__(self, base_model_name="google/bigbird-pegasus-large-bigpatent"):
        # Possible to tweak other summaries with different models in the future
        self.model = dict()
        self.tokenizer = dict()

        self.base_model = BigBirdPegasusForConditionalGeneration.from_pretrained(base_model_name)
        self.base_tokenizer = AutoTokenizer.from_pretrained(base_model_name)

        self.max_word_input = 1000


    def pipeline(patent_information, summaries_generated, min_char_abs, min_char_bg, min_char_claims):
        # TODO: add checker if valid patent info, return None if invalid
        # TODO: add scraper to get document

        # TODO: add parser to get the following info from the base document:
        abstract, background, claims = None, None, None

        summaries = list()
        if "Abstract" in summaries_generated:
            abstract_summary = summarizer.generate_abs_summary(abstract, min_char_abs)
            summaries.append(abstract_summary)
        else:
            summaries.append(None)

        if "Background" in summaries_generated:
            background_summary = summarizer.generate_bg_summary(background, min_char_bg)
            summaries.append(background_summary)
        else:
            summaries.append(None)

        if "Claims" in summaries_generated:
            claims_summary = summarizer.generate_claims_summary(claims, min_char_claims)
            summaries.append(claims_summary)
        else:
            summaries.append(None)

        return summaries


    def generate_abs_summary(abstract, min_char_abs):
        return "Abstract" + test_text


    def generate_bg_summary(background, min_char_bg):
        stop_idx = get_word_index(background, self.max_word_input)
        inputs = self.base_tokenizer(background[0:stop_idx], 
                                return_tensors='pt')
        prediction = self.base_model.generate(**inputs)
        bg_summary = self.base_tokenizer.batch_decode(prediction)
        bg_summary = textproc.clean_text(bg_summary[0])

        return bg_summary


    def generate_claims_summary(claims, min_char_claims):
        return "Claims" + test_text