Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from heapq import nlargest
|
| 2 |
+
import spacy
|
| 3 |
+
from spacy.lang.en.stop_words import STOP_WORDS
|
| 4 |
+
from string import punctuation
|
| 5 |
+
|
| 6 |
+
stopwords = list(STOP_WORDS)
|
| 7 |
+
nlp = spacy.load('en_core_web_sm')
|
| 8 |
+
punctuation = punctuation + '\n'
|
| 9 |
+
import spacy
|
| 10 |
+
from spacy.lang.en.stop_words import STOP_WORDS
|
| 11 |
+
from string import punctuation
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def prediction(text):
|
| 15 |
+
doc = nlp(text)
|
| 16 |
+
tokens = [token.text for token in doc]
|
| 17 |
+
word_frequencies = {}
|
| 18 |
+
for word in doc:
|
| 19 |
+
if word.text.lower() not in stopwords:
|
| 20 |
+
if word.text.lower() not in punctuation:
|
| 21 |
+
if word.text not in word_frequencies.keys():
|
| 22 |
+
word_frequencies[word.text] = 1
|
| 23 |
+
else:
|
| 24 |
+
word_frequencies[word.text] += 1
|
| 25 |
+
max_frequency = max(word_frequencies.values())
|
| 26 |
+
for word in word_frequencies.keys():
|
| 27 |
+
word_frequencies[word] = word_frequencies[word]/max_frequency
|
| 28 |
+
sentence_tokens = [sent for sent in doc.sents]
|
| 29 |
+
sentence_scores = {}
|
| 30 |
+
for sent in sentence_tokens:
|
| 31 |
+
for word in sent:
|
| 32 |
+
if word.text.lower() in word_frequencies.keys():
|
| 33 |
+
if sent not in sentence_scores.keys():
|
| 34 |
+
sentence_scores[sent] = word_frequencies[word.text.lower()]
|
| 35 |
+
else:
|
| 36 |
+
sentence_scores[sent] += word_frequencies[word.text.lower()]
|
| 37 |
+
select_length = int(len(sentence_tokens)*0.3)
|
| 38 |
+
summary = nlargest(select_length, sentence_scores, key = sentence_scores.get)
|
| 39 |
+
return summary
|
| 40 |
+
|
| 41 |
+
text = """
|
| 42 |
+
Maria Sharapova has basically no friends as tennis players on the WTA Tour. The Russian player has no problems in openly speaking about it and in a recent interview she said: 'I don't really hide any feelings too much.
|
| 43 |
+
I think everyone knows this is my job here. When I'm on the courts or when I'm on the court playing, I'm a competitor and I want to beat every single person whether they're in the locker room or across the net.
|
| 44 |
+
So I'm not the one to strike up a conversation about the weather and know that in the next few minutes I have to go and try to win a tennis match.
|
| 45 |
+
I'm a pretty competitive girl. I say my hellos, but I'm not sending any players flowers as well. Uhm, I'm not really friendly or close to many players.
|
| 46 |
+
I have not a lot of friends away from the courts.' When she said she is not really close to a lot of players, is that something strategic that she is doing? Is it different on the men's tour than the women's tour? 'No, not at all.
|
| 47 |
+
I think just because you're in the same sport doesn't mean that you have to be friends with everyone just because you're categorized, you're a tennis player, so you're going to get along with tennis players.
|
| 48 |
+
I think every person has different interests. I have friends that have completely different jobs and interests, and I've met them in very different parts of my life.
|
| 49 |
+
I think everyone just thinks because we're tennis players we should be the greatest of friends. But ultimately tennis is just a very small part of what we do.
|
| 50 |
+
There are so many other things that we're interested in, that we do.'
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
summary = prediction(text)
|
| 54 |
+
print(summary)
|