def keyword_title(title_corpus):

    ## here we need NLTK stopwords and punkt, will storaged in /usr/share/nltk_data
    # uncomment to download

    #nltk.download('stopwords')
    nltk.download('punkt')

    title_dict = {}
    for t in title_corpus:
        key = (t[3], t[4])
        if key in title_dict:
            title_dict[key].append(t[1])
        else:
            title_dict[key] = []
            title_dict[key].append(t[1])

    # extract keywords with year span
    title_years = {}
    for k, v in title_dict.items():
        key = (k[0], )  # year index
        if key in title_years.keys():
            title_years[key].append(v)
        else:
            title_years[key] = []
            title_years[key].append(v)

    for k, v in title_years.items():
        r = Rake()
        vs = [item.rstrip('\n') for sublist in v for item in sublist]
        # a list of strings where each string is a sentence
        #r.extract_keywords_from_sentences(vs)
        #print('The keywords for year:{}'.format(str(k[0])))
        #print(r.get_ranked_phrases_with_scores()[0:10])

        title_txt = '''.'''.join(vs)
        title_txt.strip('\n')
        r.extract_keywords_from_text(title_txt)
        print('The keywords for year:{}'.format(str(k[0])))
        # to get keyword phrases ranked from hightest to lowest with scores
        print(r.get_ranked_phrases_with_scores()[0:10])
Example #2
0
#given the list of stopwords from nltk.corpus.stopwords.words and also added some more based on the taken text
stopwords=['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't",
 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't",'across',
 'needs','called','together','creates','tells','yet','1996','shows','following','discussed']

#given the list of punctuations based on string.punctuations and also added some more based on the text
punctuations=['!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>','."',';}', '(...);',
 '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~', '<>', '[]', '()', '/*', '*/', '("', '")', ');', '//', '...','!"','•',"''",'",','""','','[])','".',
 '<<','>>','<<<','!)','(/*','*)','().','();','==']


file = open("jbn.txt","r") #text taken as data
text = file.read()
r = Rake(punctuations=punctuations,stopwords=stopwords,ranking_metric=2)
r.extract_keywords_from_text(text)

#if words are to be stored in a text file
# file1 = open("keywords.txt","w")
# for i in range(len(r.rank_list)//2): #half of the list of phrases found are considered to be as keywords 
# 	file1.write(str(r.rank_list[i][0])+"  "+r.rank_list[i][1])
# 	file1.write("\n")
# file1.close()

#if words are to be stored in an excel sheet

keywords = []
weights = []
for i in range(len(r.rank_list)//2): #half of the list of phrases found are considered to be as keywords 
	keywords.append(r.rank_list[i][1])
	weights.append(r.rank_list[i][0])