wldmr commited on
Commit
f7c77c5
·
1 Parent(s): 7fcaa54
Files changed (3) hide show
  1. app.py +41 -0
  2. lexrank.py +39 -0
  3. metrics.py +60 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import lexrank as lr
4
+ import nltk
5
+ import metrics
6
+ import pandas as pd
7
+
8
+
9
+ def summarize(in_text):
10
+
11
+ if len(in_text)==0:
12
+ return 'Error: No text provided', None
13
+
14
+ nltk_file = '/Users/hujo/nltk_data/tokenizers/punkt.zip'
15
+ #nltk_file = '/home/user/nltk_data/tokenizers/punkt.zip'
16
+ if os.path.exists(nltk_file):
17
+ print('nltk punkt file exists in ', nltk_file)
18
+ else:
19
+ nltk.download('punkt')
20
+
21
+ out_text = lr.get_Summary(in_text)
22
+ n_words = metrics.num_words(out_text)
23
+ n_sents = metrics.num_sentences(out_text)
24
+ n_chars = metrics.num_chars(out_text)
25
+ n_tokens= metrics.num_tokens(out_text)
26
+
27
+ return out_text, n_words, n_sents, n_chars, n_tokens
28
+
29
+
30
+ demo = gr.Interface(summarize,
31
+ inputs=["text"] ,
32
+ outputs=[gr.Textbox(label="Extractive Summary"),
33
+ gr.Number(label="Number of Words"),
34
+ gr.Number(label="Number of Sentences"),
35
+ gr.Number(label="Number of Characters"),
36
+ gr.Number(label="Number of Tokens")],
37
+ allow_flagging="never")
38
+
39
+
40
+ if __name__ == "__main__":
41
+ demo.launch()
lexrank.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #import nltk
2
+ #nltk.download('punkt')
3
+
4
+ from sumy.parsers.html import HtmlParser
5
+ from sumy.parsers.plaintext import PlaintextParser
6
+ from sumy.nlp.tokenizers import Tokenizer
7
+ from sumy.summarizers.lex_rank import LexRankSummarizer
8
+ from sumy.nlp.stemmers import Stemmer
9
+ from sumy.utils import get_stop_words
10
+
11
+ def get_Summary(in_text):
12
+
13
+ sentences = in_text.split('. ')
14
+ # summarize small part of the text
15
+ nr_sentences = 3 #len(sentences)
16
+ print('nr_sentences: '+str(nr_sentences))
17
+
18
+ if nr_sentences == 0:
19
+ return 'Error: No sentences available', None
20
+ list_summary = get_Lexrank(in_text,nr_sentences)
21
+ # it can happen that for lexrank a sentence consists of multiple actual sentences,
22
+ # that are separated with full stops. Then the correspoinding timestamp cannot be found
23
+ # all items from the lexrank summary must be concatinated and split up by full stops.
24
+ concat_list_summary = '. '.join([str(item).replace('.','') for item in list_summary])#.split('. ')
25
+
26
+ return concat_list_summary
27
+
28
+ def get_Lexrank(text, nr_sentences):
29
+ summary=[]
30
+ LANGUAGE = "english"
31
+ SENTENCES_COUNT = nr_sentences
32
+ parser = PlaintextParser.from_string(text, Tokenizer(LANGUAGE))
33
+ stemmer = Stemmer(LANGUAGE)
34
+ summarizer = LexRankSummarizer(stemmer)
35
+ summarizer.stop_words = get_stop_words(LANGUAGE)
36
+ for sentence in summarizer(parser.document, SENTENCES_COUNT):
37
+ summary.append(sentence)
38
+
39
+ return summary
metrics.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import nltk library for natural language processing
2
+ import nltk
3
+
4
+
5
+ # Define a function that takes some text as input and returns the number of tokens
6
+ def token_count(text):
7
+ # Import the Encoder class from bpe
8
+ from bpe import Encoder
9
+ # Create an encoder object with a vocabulary size of 10
10
+ encoder = Encoder(vocab_size=14735746)
11
+
12
+ # Train the encoder on the text
13
+ encoder.fit(text.split())
14
+
15
+ # Encode the text into tokens
16
+ tokens = encoder.tokenize(text)
17
+
18
+ # Return the number of tokens
19
+ return tokens
20
+
21
+ def num_tokens(text):
22
+ from transformers import AutoTokenizer
23
+
24
+ tokenizer = AutoTokenizer.from_pretrained("gpt2")
25
+
26
+ token_ids = tokenizer.encode(text)
27
+
28
+ token_size = len(token_ids)
29
+
30
+ return token_size
31
+
32
+ def num_words(text):
33
+ sentences = nltk.sent_tokenize(text)
34
+ # Tokenize each sentence into words using nltk.word_tokenize()
35
+ words = []
36
+ for sentence in sentences:
37
+ words.extend(nltk.word_tokenize(sentence))
38
+
39
+ num_words = len(words)
40
+
41
+ return num_words
42
+
43
+ def num_sentences(text):
44
+ # Tokenize the text into sentences using nltk.sent_tokenize()
45
+ sentences = nltk.sent_tokenize(text)
46
+ num_sentences = len(sentences)
47
+ return num_sentences
48
+
49
+
50
+ def num_chars(text):
51
+ num_characters = len(text)
52
+ return num_characters
53
+
54
+
55
+ # Print out the results
56
+ # print(f"Number of sentences: {num_sentences}")
57
+ # print(f"Number of words: {num_words}")
58
+ # print(f"Number of tokens: {num_tokens}")
59
+ # print(f"Number of trans_tokens: {trans_tokens}")
60
+ # print(f"Number of characters: {num_characters}")