Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. import spacy;
  2. import re;
  3.  
  4. # Text Preprocessing Pkg
  5. from spacy.lang.en.stop_words import STOP_WORDS
  6. from string import punctuation
  7. punc = list(punctuation)
  8. nlp = spacy.load('en_core_web_sm')
  9.  
  10. # Build a List of Stopwords
  11. stopwords = list(STOP_WORDS)
  12. whitelist = {};
  13.  
  14. internalScaleFactor = 1;
  15. externalScaleFactor = 2;
  16.  
  17. def cleanStringPunc(string):
  18. for i in punc:
  19. string=string.replace(i,'')
  20. return string;
  21.  
  22. def scale(dict, proportion):
  23. maximum_frequency = max(dict.values())
  24. for word in dict.keys():
  25. dict[word] = (dict[word]/maximum_frequency*proportion)
  26.  
  27. #Merge dict2 into dict1
  28. def mergeDict(dict1, dict2):
  29. for i in dict2.keys():
  30. word = i.lower();
  31. if word not in dict1.keys():
  32. dict1.set(word, dict2.get(i));
  33. else:
  34. dict1[word] += dict2[i]
  35.  
  36. #document ="""Machine learning (ML) is the scientific study of algorithms and statistical models that computer systems use to progressively improve their performance on a specific task. Machine learning algorithms build a mathematical model of sample data, known as "training data", in order to make predictions or decisions without being explicitly programmed to perform the task. Machine learning algorithms are used in the applications of email filtering, detection of network intruders, and computer vision, where it is infeasible to develop an algorithm of specific instructions for performing the task. Machine learning is closely related to computational statistics, which focuses on making predictions using computers. The study of mathematical optimization delivers methods, theory and application domains to the field of machine learning. Data mining is a field of study within machine learning, and focuses on exploratory data analysis through unsupervised learning.In its application across business problems, machine learning is also referred to as predictive analytics."""
  37.  
  38. doc = """The University of California, Los Angeles (UCLA)[1] is a public research university in Los Angeles. UCLA traces its early origins back to 1882 as the southern branch of the California State Normal School (now San Jose State University). It became the Southern Branch of the University of California in 1919, making it the fourth-oldest (after UC Berkeley, UC San Francisco, and UC Davis) of the 10-campus University of California system and oldest of the campuses in Southern California.[11] It offers 337 undergraduate and graduate degree programs in a wide range of disciplines.[12] UCLA enrolls about 31,500 undergraduate and 12,800 graduate students[7] and had 119,000 applicants for Fall 2016, including transfer applicants, making the school the most applied-to of any American university.[13]
  39. The university is organized into six undergraduate colleges, seven professional schools, and four professional health science schools. The undergraduate colleges are the College of Letters and Science; Samueli School of Engineering; School of the Arts and Architecture; Herb Alpert School of Music; School of Theater, Film and Television; and School of Nursing.
  40. As of 2017, 24 Nobel laureates, three Fields Medalists, and five Turing Award winners, and two Chief Scientists of the U.S. Air Force have been affiliated with UCLA as faculty, researchers, or alumni.[14][15][16] Among the current faculty members, 55 have been elected to the National Academy of Sciences, 28 to the National Academy of Engineering, 39 to the Institute of Medicine, and 124 to the American Academy of Arts and Sciences.[17] The university was elected to the Association of American Universities in 1974.[18]
  41.  
  42. UCLA is considered one of the country's Public Ivy universities, meaning that it is a public university thought to provide a quality of education comparable with that of the Ivy League. US News & World Report named UCLA the best public university in the United States for 2019.[19]
  43.  
  44. UCLA student-athletes compete as the Bruins in the Pac-12 Conference. The Bruins have won 129 national championships, including 118 NCAA team championships, more than any other university except Stanford University, whose athletes have won 126.[20][21][22] UCLA student-athletes, coaches and staff won 251 Olympic medals: 126 gold, 65 silver, and 60 bronze.[23] UCLA student-athletes competed in every Olympics since 1920 with one exception (1924) and won a gold medal in every Olympics the U.S. participated in since 1932"""
  45.  
  46. def summarize(document,wlistadd,keywords):
  47. # Build an NLP Object
  48. docx = nlp(document)
  49. #stripdoc = nlp(re.sub(r'[\(\)\[\]]', '', document).lower())
  50. stripdoc = nlp(cleanStringPunc(document).lower());
  51.  
  52. # Tokenization of Text
  53. mytokens = [token.text for token in docx]
  54.  
  55. # Build Word Frequency
  56. # word.text is tokenization in spacy
  57. word_frequencies = {}
  58. for word in stripdoc:
  59. if word.text not in stopwords:
  60. if word.text not in word_frequencies.keys():
  61. word_frequencies[word.text] = 1
  62. else:
  63. word_frequencies[word.text] += 1
  64.  
  65. #print(word_frequencies);
  66.  
  67. # Maximum Word Frequency
  68. scale(word_frequencies, internalScaleFactor)
  69. scale(keywords,externalScaleFactor)
  70. mergeDict(word_frequencies,keywords)
  71. # Frequency Table
  72. print(sorted(word_frequencies, key=word_frequencies.get, reverse=True))
  73.  
  74. # Sentence Tokens
  75. sentence_list = [ sentence for sentence in docx.sents ]
  76. #print(sentence_list)
  77.  
  78. # Sentence Score via comparing each word with sentence
  79. sentence_scores = {}
  80. for sent in sentence_list:
  81. for word in sent:
  82. if word.text.lower() in word_frequencies.keys():
  83. if sent not in sentence_scores.keys():
  84. sentence_scores[sent] = word_frequencies[word.text.lower()]
  85. else:
  86. sentence_scores[sent] += word_frequencies[word.text.lower()]
  87. print(sentence_scores);
  88.  
  89. # Import Heapq
  90. from heapq import nlargest
  91. summarized_sentences = nlargest(10, sentence_scores, key=sentence_scores.get)
  92. #print(summarized_sentences)
  93. final_sentences = [ w.text for w in summarized_sentences ]
  94. summary = '\n '.join(final_sentences)
  95. print("==========\nSummary:")
  96. print(summary)
  97.  
  98. return summary;
  99.  
  100. summarize(doc,{},{"nobel": 10,"offers": 20})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement