thesis-prediction-ml/building-features-from-text-data/03-VectorizeTextAsABagOfNGrams_CountVectorizer_Nltk.ipynb
2023-06-18 14:45:35 +02:00

2116 lines
83 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Vectorize text as a bag-of-n-grams"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.feature_extraction.text import CountVectorizer"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"train_text = [\"A bird in hand is worth two in the bush.\",\n",
" \"Good things come to those who wait.\",\n",
" \"These watches cost $1500! \",\n",
" \"There are other fish in the sea.\",\n",
" \"The ball is in your court.\",\n",
" \"Mr. Smith Goes to Washington \",\n",
" \"Doogie Howser M.D.\"]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"n_gram_vectorizer = CountVectorizer(ngram_range=(2, 2))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"transformed_vector = n_gram_vectorizer.fit_transform(train_text)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'bird in': 2,\n",
" 'in hand': 10,\n",
" 'hand is': 9,\n",
" 'is worth': 14,\n",
" 'worth two': 30,\n",
" 'two in': 27,\n",
" 'in the': 11,\n",
" 'the bush': 19,\n",
" 'good things': 8,\n",
" 'things come': 23,\n",
" 'come to': 3,\n",
" 'to those': 25,\n",
" 'those who': 24,\n",
" 'who wait': 29,\n",
" 'these watches': 22,\n",
" 'watches cost': 28,\n",
" 'cost 1500': 4,\n",
" 'there are': 21,\n",
" 'are other': 0,\n",
" 'other fish': 16,\n",
" 'fish in': 6,\n",
" 'the sea': 20,\n",
" 'the ball': 18,\n",
" 'ball is': 1,\n",
" 'is in': 13,\n",
" 'in your': 12,\n",
" 'your court': 31,\n",
" 'mr smith': 15,\n",
" 'smith goes': 17,\n",
" 'goes to': 7,\n",
" 'to washington': 26,\n",
" 'doogie howser': 5}"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n_gram_vectorizer.vocabulary_"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,\n",
" 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],\n",
" [0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 1, 1, 1, 0, 0, 0, 1, 0, 0],\n",
" [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 1, 0, 0, 0, 0, 0, 1, 0, 0, 0],\n",
" [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
" [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int64)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"transformed_vector.toarray()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'bird': 5,\n",
" 'in': 24,\n",
" 'hand': 21,\n",
" 'is': 28,\n",
" 'worth': 61,\n",
" 'two': 53,\n",
" 'the': 38,\n",
" 'bush': 7,\n",
" 'bird in': 6,\n",
" 'in hand': 25,\n",
" 'hand is': 22,\n",
" 'is worth': 30,\n",
" 'worth two': 62,\n",
" 'two in': 54,\n",
" 'in the': 26,\n",
" 'the bush': 40,\n",
" 'good': 19,\n",
" 'things': 46,\n",
" 'come': 8,\n",
" 'to': 50,\n",
" 'those': 48,\n",
" 'who': 59,\n",
" 'wait': 55,\n",
" 'good things': 20,\n",
" 'things come': 47,\n",
" 'come to': 9,\n",
" 'to those': 51,\n",
" 'those who': 49,\n",
" 'who wait': 60,\n",
" 'these': 44,\n",
" 'watches': 57,\n",
" 'cost': 10,\n",
" '1500': 0,\n",
" 'these watches': 45,\n",
" 'watches cost': 58,\n",
" 'cost 1500': 11,\n",
" 'there': 42,\n",
" 'are': 1,\n",
" 'other': 33,\n",
" 'fish': 15,\n",
" 'sea': 35,\n",
" 'there are': 43,\n",
" 'are other': 2,\n",
" 'other fish': 34,\n",
" 'fish in': 16,\n",
" 'the sea': 41,\n",
" 'ball': 3,\n",
" 'your': 63,\n",
" 'court': 12,\n",
" 'the ball': 39,\n",
" 'ball is': 4,\n",
" 'is in': 29,\n",
" 'in your': 27,\n",
" 'your court': 64,\n",
" 'mr': 31,\n",
" 'smith': 36,\n",
" 'goes': 17,\n",
" 'washington': 56,\n",
" 'mr smith': 32,\n",
" 'smith goes': 37,\n",
" 'goes to': 18,\n",
" 'to washington': 52,\n",
" 'doogie': 13,\n",
" 'howser': 23,\n",
" 'doogie howser': 14}"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n_gram_vectorizer = CountVectorizer(ngram_range=(1, 2))\n",
"\n",
"transformed_vector = n_gram_vectorizer.fit_transform(train_text)\n",
"\n",
"n_gram_vectorizer.vocabulary_"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,\n",
" 1, 0, 2, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0],\n",
" [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],\n",
" [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,\n",
" 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],\n",
" dtype=int64)"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"transformed_vector.toarray()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Bigram and Trigram"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Marie Curie was a Polish-born physicist and chemist and one of the most famous scientists of her time.\n",
"Together with her husband Pierre, she was awarded the Nobel Prize in 1903, and she went on to win another in 1911.\n",
"Marie Sklodowska was born in Warsaw on 7 November 1867, the daughter of a teacher.\n",
"In 1891, she went to Paris to study physics and mathematics at the Sorbonne where she met Pierre Curie, professor of the School of Physics.\n",
"They were married in 1895.\n",
"The Curies worked together investigating radioactivity, building on the work of the German physicist Roentgen and the French physicist Becquerel.\n",
"In July 1898, the Curies announced the discovery of a new chemical element, polonium.\n",
"At the end of the year, they announced the discovery of another, radium.\n",
"The Curies, along with Becquerel, were awarded the Nobel Prize for Physics in 1903.\n",
"Pierre's life was cut short in 1906 when he was knocked down and killed by a carriage.\n",
"Marie took over his teaching post, becoming the first woman to teach at the Sorbonne, and devoted herself to continuing the work that they had begun together.\n",
"She received a second Nobel Prize, for Chemistry, in 1911.\n",
"The Curie's research was crucial in the development of x-rays in surgery.\n",
"During World War One Curie helped to equip ambulances with x-ray equipment, which she herself drove to the front lines.\n",
"The International Red Cross made her head of its radiological service and she held training courses for medical orderlies and doctors in the new techniques.\n",
"Despite her success, Marie continued to face great opposition from male scientists in France, and she never received significant financial benefits from her work.\n",
"By the late 1920s her health was beginning to deteriorate.\n",
"She died on 4 July 1934 from leukaemia, caused by exposure to high-energy radiation from her research.\n",
"The Curies' eldest daughter Irene was herself a scientist and winner of the Nobel Prize for Chemistry.\n"
]
}
],
"source": [
"with open('./datasets/biography.txt', 'r') as f:\n",
" file_contents = f.read()\n",
"\n",
"print(file_contents)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Marie Curie was a Polish-born physicist and chemist and one of the most famous scientists of her time.', 'Together with her husband Pierre, she was awarded the Nobel Prize in 1903, and she went on to win another in 1911.', 'Marie Sklodowska was born in Warsaw on 7 November 1867, the daughter of a teacher.', 'In 1891, she went to Paris to study physics and mathematics at the Sorbonne where she met Pierre Curie, professor of the School of Physics.', 'They were married in 1895.', 'The Curies worked together investigating radioactivity, building on the work of the German physicist Roentgen and the French physicist Becquerel.', 'In July 1898, the Curies announced the discovery of a new chemical element, polonium.', 'At the end of the year, they announced the discovery of another, radium.', 'The Curies, along with Becquerel, were awarded the Nobel Prize for Physics in 1903.', \"Pierre's life was cut short in 1906 when he was knocked down and killed by a carriage.\", 'Marie took over his teaching post, becoming the first woman to teach at the Sorbonne, and devoted herself to continuing the work that they had begun together.', 'She received a second Nobel Prize, for Chemistry, in 1911.', \"The Curie's research was crucial in the development of x-rays in surgery.\", 'During World War One Curie helped to equip ambulances with x-ray equipment, which she herself drove to the front lines.', 'The International Red Cross made her head of its radiological service and she held training courses for medical orderlies and doctors in the new techniques.', 'Despite her success, Marie continued to face great opposition from male scientists in France, and she never received significant financial benefits from her work.', 'By the late 1920s her health was beginning to deteriorate.', 'She died on 4 July 1934 from leukaemia, caused by exposure to high-energy radiation from her research.', \"The Curies' eldest daughter Irene was herself a scientist and winner of the Nobel Prize for Chemistry.\"]\n"
]
}
],
"source": [
"sentences = file_contents.split('\\n')\n",
" \n",
"print(sentences)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"n_gram_vectorizer = CountVectorizer(ngram_range=(2, 3))"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"transformed_vector = n_gram_vectorizer.fit_transform(sentences)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'marie curie': 251,\n",
" 'curie was': 92,\n",
" 'was polish': 504,\n",
" 'polish born': 330,\n",
" 'born physicist': 59,\n",
" 'physicist and': 315,\n",
" 'and chemist': 18,\n",
" 'chemist and': 72,\n",
" 'and one': 28,\n",
" 'one of': 305,\n",
" 'of the': 289,\n",
" 'the most': 437,\n",
" 'most famous': 265,\n",
" 'famous scientists': 142,\n",
" 'scientists of': 367,\n",
" 'of her': 279,\n",
" 'her time': 191,\n",
" 'marie curie was': 252,\n",
" 'curie was polish': 93,\n",
" 'was polish born': 505,\n",
" 'polish born physicist': 331,\n",
" 'born physicist and': 60,\n",
" 'physicist and chemist': 316,\n",
" 'and chemist and': 19,\n",
" 'chemist and one': 73,\n",
" 'and one of': 29,\n",
" 'one of the': 306,\n",
" 'of the most': 291,\n",
" 'the most famous': 438,\n",
" 'most famous scientists': 266,\n",
" 'famous scientists of': 143,\n",
" 'scientists of her': 368,\n",
" 'of her time': 280,\n",
" 'together with': 480,\n",
" 'with her': 526,\n",
" 'her husband': 186,\n",
" 'husband pierre': 203,\n",
" 'pierre she': 328,\n",
" 'she was': 385,\n",
" 'was awarded': 490,\n",
" 'awarded the': 46,\n",
" 'the nobel': 441,\n",
" 'nobel prize': 272,\n",
" 'prize in': 337,\n",
" 'in 1903': 208,\n",
" '1903 and': 6,\n",
" 'and she': 30,\n",
" 'she went': 387,\n",
" 'went on': 506,\n",
" 'on to': 301,\n",
" 'to win': 476,\n",
" 'win another': 520,\n",
" 'another in': 40,\n",
" 'in 1911': 212,\n",
" 'together with her': 481,\n",
" 'with her husband': 527,\n",
" 'her husband pierre': 187,\n",
" 'husband pierre she': 204,\n",
" 'pierre she was': 329,\n",
" 'she was awarded': 386,\n",
" 'was awarded the': 491,\n",
" 'awarded the nobel': 47,\n",
" 'the nobel prize': 442,\n",
" 'nobel prize in': 274,\n",
" 'prize in 1903': 338,\n",
" 'in 1903 and': 209,\n",
" '1903 and she': 7,\n",
" 'and she went': 33,\n",
" 'she went on': 388,\n",
" 'went on to': 507,\n",
" 'on to win': 302,\n",
" 'to win another': 477,\n",
" 'win another in': 521,\n",
" 'another in 1911': 41,\n",
" 'marie sklodowska': 253,\n",
" 'sklodowska was': 394,\n",
" 'was born': 494,\n",
" 'born in': 57,\n",
" 'in warsaw': 221,\n",
" 'warsaw on': 488,\n",
" 'on november': 297,\n",
" 'november 1867': 275,\n",
" '1867 the': 0,\n",
" 'the daughter': 417,\n",
" 'daughter of': 106,\n",
" 'of teacher': 288,\n",
" 'marie sklodowska was': 254,\n",
" 'sklodowska was born': 395,\n",
" 'was born in': 495,\n",
" 'born in warsaw': 58,\n",
" 'in warsaw on': 222,\n",
" 'warsaw on november': 489,\n",
" 'on november 1867': 298,\n",
" 'november 1867 the': 276,\n",
" '1867 the daughter': 1,\n",
" 'the daughter of': 418,\n",
" 'daughter of teacher': 107,\n",
" 'in 1891': 205,\n",
" '1891 she': 2,\n",
" 'went to': 508,\n",
" 'to paris': 468,\n",
" 'paris to': 313,\n",
" 'to study': 470,\n",
" 'study physics': 400,\n",
" 'physics and': 320,\n",
" 'and mathematics': 26,\n",
" 'mathematics at': 259,\n",
" 'at the': 43,\n",
" 'the sorbonne': 445,\n",
" 'sorbonne where': 398,\n",
" 'where she': 516,\n",
" 'she met': 379,\n",
" 'met pierre': 263,\n",
" 'pierre curie': 324,\n",
" 'curie professor': 88,\n",
" 'professor of': 339,\n",
" 'the school': 443,\n",
" 'school of': 361,\n",
" 'of physics': 285,\n",
" 'in 1891 she': 206,\n",
" '1891 she went': 3,\n",
" 'she went to': 389,\n",
" 'went to paris': 509,\n",
" 'to paris to': 469,\n",
" 'paris to study': 314,\n",
" 'to study physics': 471,\n",
" 'study physics and': 401,\n",
" 'physics and mathematics': 321,\n",
" 'and mathematics at': 27,\n",
" 'mathematics at the': 260,\n",
" 'at the sorbonne': 45,\n",
" 'the sorbonne where': 447,\n",
" 'sorbonne where she': 399,\n",
" 'where she met': 517,\n",
" 'she met pierre': 380,\n",
" 'met pierre curie': 264,\n",
" 'pierre curie professor': 325,\n",
" 'curie professor of': 89,\n",
" 'professor of the': 340,\n",
" 'of the school': 293,\n",
" 'the school of': 444,\n",
" 'school of physics': 362,\n",
" 'they were': 457,\n",
" 'were married': 512,\n",
" 'married in': 257,\n",
" 'in 1895': 207,\n",
" 'they were married': 458,\n",
" 'were married in': 513,\n",
" 'married in 1895': 258,\n",
" 'the curies': 412,\n",
" 'curies worked': 100,\n",
" 'worked together': 536,\n",
" 'together investigating': 478,\n",
" 'investigating radioactivity': 225,\n",
" 'radioactivity building': 343,\n",
" 'building on': 61,\n",
" 'on the': 299,\n",
" 'the work': 448,\n",
" 'work of': 532,\n",
" 'the german': 431,\n",
" 'german physicist': 166,\n",
" 'physicist roentgen': 318,\n",
" 'roentgen and': 359,\n",
" 'and the': 34,\n",
" 'the french': 427,\n",
" 'french physicist': 156,\n",
" 'physicist becquerel': 317,\n",
" 'the curies worked': 416,\n",
" 'curies worked together': 101,\n",
" 'worked together investigating': 537,\n",
" 'together investigating radioactivity': 479,\n",
" 'investigating radioactivity building': 226,\n",
" 'radioactivity building on': 344,\n",
" 'building on the': 62,\n",
" 'on the work': 300,\n",
" 'the work of': 449,\n",
" 'work of the': 533,\n",
" 'of the german': 290,\n",
" 'the german physicist': 432,\n",
" 'german physicist roentgen': 167,\n",
" 'physicist roentgen and': 319,\n",
" 'roentgen and the': 360,\n",
" 'and the french': 35,\n",
" 'the french physicist': 428,\n",
" 'french physicist becquerel': 157,\n",
" 'in july': 215,\n",
" 'july 1898': 231,\n",
" '1898 the': 4,\n",
" 'curies announced': 96,\n",
" 'announced the': 38,\n",
" 'the discovery': 421,\n",
" 'discovery of': 116,\n",
" 'of new': 283,\n",
" 'new chemical': 269,\n",
" 'chemical element': 70,\n",
" 'element polonium': 129,\n",
" 'in july 1898': 216,\n",
" 'july 1898 the': 232,\n",
" '1898 the curies': 5,\n",
" 'the curies announced': 414,\n",
" 'curies announced the': 97,\n",
" 'announced the discovery': 39,\n",
" 'the discovery of': 422,\n",
" 'discovery of new': 118,\n",
" 'of new chemical': 284,\n",
" 'new chemical element': 270,\n",
" 'chemical element polonium': 71,\n",
" 'the end': 423,\n",
" 'end of': 130,\n",
" 'the year': 451,\n",
" 'year they': 540,\n",
" 'they announced': 453,\n",
" 'of another': 277,\n",
" 'another radium': 42,\n",
" 'at the end': 44,\n",
" 'the end of': 424,\n",
" 'end of the': 131,\n",
" 'of the year': 294,\n",
" 'the year they': 452,\n",
" 'year they announced': 541,\n",
" 'they announced the': 454,\n",
" 'discovery of another': 117,\n",
" 'of another radium': 278,\n",
" 'curies along': 94,\n",
" 'along with': 14,\n",
" 'with becquerel': 524,\n",
" 'becquerel were': 50,\n",
" 'were awarded': 510,\n",
" 'prize for': 334,\n",
" 'for physics': 152,\n",
" 'physics in': 322,\n",
" 'the curies along': 413,\n",
" 'curies along with': 95,\n",
" 'along with becquerel': 15,\n",
" 'with becquerel were': 525,\n",
" 'becquerel were awarded': 51,\n",
" 'were awarded the': 511,\n",
" 'nobel prize for': 273,\n",
" 'prize for physics': 336,\n",
" 'for physics in': 153,\n",
" 'physics in 1903': 323,\n",
" 'pierre life': 326,\n",
" 'life was': 243,\n",
" 'was cut': 498,\n",
" 'cut short': 102,\n",
" 'short in': 390,\n",
" 'in 1906': 210,\n",
" '1906 when': 8,\n",
" 'when he': 514,\n",
" 'he was': 172,\n",
" 'was knocked': 502,\n",
" 'knocked down': 237,\n",
" 'down and': 121,\n",
" 'and killed': 24,\n",
" 'killed by': 235,\n",
" 'by carriage': 63,\n",
" 'pierre life was': 327,\n",
" 'life was cut': 244,\n",
" 'was cut short': 499,\n",
" 'cut short in': 103,\n",
" 'short in 1906': 391,\n",
" 'in 1906 when': 211,\n",
" '1906 when he': 9,\n",
" 'when he was': 515,\n",
" 'he was knocked': 173,\n",
" 'was knocked down': 503,\n",
" 'knocked down and': 238,\n",
" 'down and killed': 122,\n",
" 'and killed by': 25,\n",
" 'killed by carriage': 236,\n",
" 'marie took': 255,\n",
" 'took over': 482,\n",
" 'over his': 311,\n",
" 'his teaching': 201,\n",
" 'teaching post': 406,\n",
" 'post becoming': 332,\n",
" 'becoming the': 48,\n",
" 'the first': 425,\n",
" 'first woman': 146,\n",
" 'woman to': 530,\n",
" 'to teach': 472,\n",
" 'teach at': 404,\n",
" 'sorbonne and': 396,\n",
" 'and devoted': 20,\n",
" 'devoted herself': 112,\n",
" 'herself to': 197,\n",
" 'to continuing': 459,\n",
" 'continuing the': 78,\n",
" 'work that': 534,\n",
" 'that they': 408,\n",
" 'they had': 455,\n",
" 'had begun': 170,\n",
" 'begun together': 54,\n",
" 'marie took over': 256,\n",
" 'took over his': 483,\n",
" 'over his teaching': 312,\n",
" 'his teaching post': 202,\n",
" 'teaching post becoming': 407,\n",
" 'post becoming the': 333,\n",
" 'becoming the first': 49,\n",
" 'the first woman': 426,\n",
" 'first woman to': 147,\n",
" 'woman to teach': 531,\n",
" 'to teach at': 473,\n",
" 'teach at the': 405,\n",
" 'the sorbonne and': 446,\n",
" 'sorbonne and devoted': 397,\n",
" 'and devoted herself': 21,\n",
" 'devoted herself to': 113,\n",
" 'herself to continuing': 198,\n",
" 'to continuing the': 460,\n",
" 'continuing the work': 79,\n",
" 'the work that': 450,\n",
" 'work that they': 535,\n",
" 'that they had': 409,\n",
" 'they had begun': 456,\n",
" 'had begun together': 171,\n",
" 'she received': 383,\n",
" 'received second': 351,\n",
" 'second nobel': 369,\n",
" 'for chemistry': 148,\n",
" 'chemistry in': 74,\n",
" 'she received second': 384,\n",
" 'received second nobel': 352,\n",
" 'second nobel prize': 370,\n",
" 'prize for chemistry': 335,\n",
" 'for chemistry in': 149,\n",
" 'chemistry in 1911': 75,\n",
" 'the curie': 410,\n",
" 'curie research': 90,\n",
" 'research was': 357,\n",
" 'was crucial': 496,\n",
" 'crucial in': 84,\n",
" 'in the': 218,\n",
" 'the development': 419,\n",
" 'development of': 110,\n",
" 'of rays': 286,\n",
" 'rays in': 349,\n",
" 'in surgery': 217,\n",
" 'the curie research': 411,\n",
" 'curie research was': 91,\n",
" 'research was crucial': 358,\n",
" 'was crucial in': 497,\n",
" 'crucial in the': 85,\n",
" 'in the development': 219,\n",
" 'the development of': 420,\n",
" 'development of rays': 111,\n",
" 'of rays in': 287,\n",
" 'rays in surgery': 350,\n",
" 'during world': 125,\n",
" 'world war': 538,\n",
" 'war one': 486,\n",
" 'one curie': 303,\n",
" 'curie helped': 86,\n",
" 'helped to': 180,\n",
" 'to equip': 462,\n",
" 'equip ambulances': 134,\n",
" 'ambulances with': 16,\n",
" 'with ray': 528,\n",
" 'ray equipment': 347,\n",
" 'equipment which': 136,\n",
" 'which she': 518,\n",
" 'she herself': 377,\n",
" 'herself drove': 193,\n",
" 'drove to': 123,\n",
" 'to the': 474,\n",
" 'the front': 429,\n",
" 'front lines': 165,\n",
" 'during world war': 126,\n",
" 'world war one': 539,\n",
" 'war one curie': 487,\n",
" 'one curie helped': 304,\n",
" 'curie helped to': 87,\n",
" 'helped to equip': 181,\n",
" 'to equip ambulances': 463,\n",
" 'equip ambulances with': 135,\n",
" 'ambulances with ray': 17,\n",
" 'with ray equipment': 529,\n",
" 'ray equipment which': 348,\n",
" 'equipment which she': 137,\n",
" 'which she herself': 519,\n",
" 'she herself drove': 378,\n",
" 'herself drove to': 194,\n",
" 'drove to the': 124,\n",
" 'to the front': 475,\n",
" 'the front lines': 430,\n",
" 'the international': 433,\n",
" 'international red': 223,\n",
" 'red cross': 355,\n",
" 'cross made': 82,\n",
" 'made her': 245,\n",
" 'her head': 182,\n",
" 'head of': 174,\n",
" 'of its': 281,\n",
" 'its radiological': 229,\n",
" 'radiological service': 345,\n",
" 'service and': 371,\n",
" 'she held': 375,\n",
" 'held training': 178,\n",
" 'training courses': 484,\n",
" 'courses for': 80,\n",
" 'for medical': 150,\n",
" 'medical orderlies': 261,\n",
" 'orderlies and': 309,\n",
" 'and doctors': 22,\n",
" 'doctors in': 119,\n",
" 'the new': 439,\n",
" 'new techniques': 271,\n",
" 'the international red': 434,\n",
" 'international red cross': 224,\n",
" 'red cross made': 356,\n",
" 'cross made her': 83,\n",
" 'made her head': 246,\n",
" 'her head of': 183,\n",
" 'head of its': 175,\n",
" 'of its radiological': 282,\n",
" 'its radiological service': 230,\n",
" 'radiological service and': 346,\n",
" 'service and she': 372,\n",
" 'and she held': 31,\n",
" 'she held training': 376,\n",
" 'held training courses': 179,\n",
" 'training courses for': 485,\n",
" 'courses for medical': 81,\n",
" 'for medical orderlies': 151,\n",
" 'medical orderlies and': 262,\n",
" 'orderlies and doctors': 310,\n",
" 'and doctors in': 23,\n",
" 'doctors in the': 120,\n",
" 'in the new': 220,\n",
" 'the new techniques': 440,\n",
" 'despite her': 108,\n",
" 'her success': 189,\n",
" 'success marie': 402,\n",
" 'marie continued': 249,\n",
" 'continued to': 76,\n",
" 'to face': 464,\n",
" 'face great': 140,\n",
" 'great opposition': 168,\n",
" 'opposition from': 307,\n",
" 'from male': 163,\n",
" 'male scientists': 247,\n",
" 'scientists in': 365,\n",
" 'in france': 213,\n",
" 'france and': 154,\n",
" 'she never': 381,\n",
" 'never received': 267,\n",
" 'received significant': 353,\n",
" 'significant financial': 392,\n",
" 'financial benefits': 144,\n",
" 'benefits from': 55,\n",
" 'from her': 158,\n",
" 'her work': 192,\n",
" 'despite her success': 109,\n",
" 'her success marie': 190,\n",
" 'success marie continued': 403,\n",
" 'marie continued to': 250,\n",
" 'continued to face': 77,\n",
" 'to face great': 465,\n",
" 'face great opposition': 141,\n",
" 'great opposition from': 169,\n",
" 'opposition from male': 308,\n",
" 'from male scientists': 164,\n",
" 'male scientists in': 248,\n",
" 'scientists in france': 366,\n",
" 'in france and': 214,\n",
" 'france and she': 155,\n",
" 'and she never': 32,\n",
" 'she never received': 382,\n",
" 'never received significant': 268,\n",
" 'received significant financial': 354,\n",
" 'significant financial benefits': 393,\n",
" 'financial benefits from': 145,\n",
" 'benefits from her': 56,\n",
" 'from her work': 160,\n",
" 'by the': 66,\n",
" 'the late': 435,\n",
" 'late 1920s': 239,\n",
" '1920s her': 10,\n",
" 'her health': 184,\n",
" 'health was': 176,\n",
" 'was beginning': 492,\n",
" 'beginning to': 52,\n",
" 'to deteriorate': 461,\n",
" 'by the late': 67,\n",
" 'the late 1920s': 436,\n",
" 'late 1920s her': 240,\n",
" '1920s her health': 11,\n",
" 'her health was': 185,\n",
" 'health was beginning': 177,\n",
" 'was beginning to': 493,\n",
" 'beginning to deteriorate': 53,\n",
" 'she died': 373,\n",
" 'died on': 114,\n",
" 'on july': 295,\n",
" 'july 1934': 233,\n",
" '1934 from': 12,\n",
" 'from leukaemia': 161,\n",
" 'leukaemia caused': 241,\n",
" 'caused by': 68,\n",
" 'by exposure': 64,\n",
" 'exposure to': 138,\n",
" 'to high': 466,\n",
" 'high energy': 199,\n",
" 'energy radiation': 132,\n",
" 'radiation from': 341,\n",
" 'her research': 188,\n",
" 'she died on': 374,\n",
" 'died on july': 115,\n",
" 'on july 1934': 296,\n",
" 'july 1934 from': 234,\n",
" '1934 from leukaemia': 13,\n",
" 'from leukaemia caused': 162,\n",
" 'leukaemia caused by': 242,\n",
" 'caused by exposure': 69,\n",
" 'by exposure to': 65,\n",
" 'exposure to high': 139,\n",
" 'to high energy': 467,\n",
" 'high energy radiation': 200,\n",
" 'energy radiation from': 133,\n",
" 'radiation from her': 342,\n",
" 'from her research': 159,\n",
" 'curies eldest': 98,\n",
" 'eldest daughter': 127,\n",
" 'daughter irene': 104,\n",
" 'irene was': 227,\n",
" 'was herself': 500,\n",
" 'herself scientist': 195,\n",
" 'scientist and': 363,\n",
" 'and winner': 36,\n",
" 'winner of': 522,\n",
" 'the curies eldest': 415,\n",
" 'curies eldest daughter': 99,\n",
" 'eldest daughter irene': 128,\n",
" 'daughter irene was': 105,\n",
" 'irene was herself': 228,\n",
" 'was herself scientist': 501,\n",
" 'herself scientist and': 196,\n",
" 'scientist and winner': 364,\n",
" 'and winner of': 37,\n",
" 'winner of the': 523,\n",
" 'of the nobel': 292}"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vocabulary = n_gram_vectorizer.vocabulary_\n",
"\n",
"vocabulary"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"251"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n_gram_vectorizer.vocabulary_.get('marie curie')"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"279"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n_gram_vectorizer.vocabulary_.get('of her')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* https://stackoverflow.com/questions/11763613/python-list-of-ngrams-with-frequencies\n",
"* https://medium.com/@cristhianboujon/how-to-list-the-most-common-words-from-text-corpus-using-scikit-learn-dad4d0cab41d"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"word_count is a vector that contains the sum of each word occurrence in all texts in the corpus. In other words, we are adding the elements for each column of vector matrix."
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int64)"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"transformed_vector.toarray()[:1]"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 3,\n",
" 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 4, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 3, 3, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
" 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int64)"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"word_count = transformed_vector.toarray().sum(axis=0)\n",
"\n",
"word_count"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_items([('marie curie', 251), ('curie was', 92), ('was polish', 504), ('polish born', 330), ('born physicist', 59), ('physicist and', 315), ('and chemist', 18), ('chemist and', 72), ('and one', 28), ('one of', 305), ('of the', 289), ('the most', 437), ('most famous', 265), ('famous scientists', 142), ('scientists of', 367), ('of her', 279), ('her time', 191), ('marie curie was', 252), ('curie was polish', 93), ('was polish born', 505), ('polish born physicist', 331), ('born physicist and', 60), ('physicist and chemist', 316), ('and chemist and', 19), ('chemist and one', 73), ('and one of', 29), ('one of the', 306), ('of the most', 291), ('the most famous', 438), ('most famous scientists', 266), ('famous scientists of', 143), ('scientists of her', 368), ('of her time', 280), ('together with', 480), ('with her', 526), ('her husband', 186), ('husband pierre', 203), ('pierre she', 328), ('she was', 385), ('was awarded', 490), ('awarded the', 46), ('the nobel', 441), ('nobel prize', 272), ('prize in', 337), ('in 1903', 208), ('1903 and', 6), ('and she', 30), ('she went', 387), ('went on', 506), ('on to', 301), ('to win', 476), ('win another', 520), ('another in', 40), ('in 1911', 212), ('together with her', 481), ('with her husband', 527), ('her husband pierre', 187), ('husband pierre she', 204), ('pierre she was', 329), ('she was awarded', 386), ('was awarded the', 491), ('awarded the nobel', 47), ('the nobel prize', 442), ('nobel prize in', 274), ('prize in 1903', 338), ('in 1903 and', 209), ('1903 and she', 7), ('and she went', 33), ('she went on', 388), ('went on to', 507), ('on to win', 302), ('to win another', 477), ('win another in', 521), ('another in 1911', 41), ('marie sklodowska', 253), ('sklodowska was', 394), ('was born', 494), ('born in', 57), ('in warsaw', 221), ('warsaw on', 488), ('on november', 297), ('november 1867', 275), ('1867 the', 0), ('the daughter', 417), ('daughter of', 106), ('of teacher', 288), ('marie sklodowska was', 254), ('sklodowska was born', 395), ('was born in', 495), ('born in warsaw', 58), ('in warsaw on', 222), ('warsaw on november', 489), ('on november 1867', 298), ('november 1867 the', 276), ('1867 the daughter', 1), ('the daughter of', 418), ('daughter of teacher', 107), ('in 1891', 205), ('1891 she', 2), ('went to', 508), ('to paris', 468), ('paris to', 313), ('to study', 470), ('study physics', 400), ('physics and', 320), ('and mathematics', 26), ('mathematics at', 259), ('at the', 43), ('the sorbonne', 445), ('sorbonne where', 398), ('where she', 516), ('she met', 379), ('met pierre', 263), ('pierre curie', 324), ('curie professor', 88), ('professor of', 339), ('the school', 443), ('school of', 361), ('of physics', 285), ('in 1891 she', 206), ('1891 she went', 3), ('she went to', 389), ('went to paris', 509), ('to paris to', 469), ('paris to study', 314), ('to study physics', 471), ('study physics and', 401), ('physics and mathematics', 321), ('and mathematics at', 27), ('mathematics at the', 260), ('at the sorbonne', 45), ('the sorbonne where', 447), ('sorbonne where she', 399), ('where she met', 517), ('she met pierre', 380), ('met pierre curie', 264), ('pierre curie professor', 325), ('curie professor of', 89), ('professor of the', 340), ('of the school', 293), ('the school of', 444), ('school of physics', 362), ('they were', 457), ('were married', 512), ('married in', 257), ('in 1895', 207), ('they were married', 458), ('were married in', 513), ('married in 1895', 258), ('the curies', 412), ('curies worked', 100), ('worked together', 536), ('together investigating', 478), ('investigating radioactivity', 225), ('radioactivity building', 343), ('building on', 61), ('on the', 299), ('the work', 448), ('work of', 532), ('the german', 431), ('german physicist', 166), ('physicist roentgen', 318), ('roentgen and', 359), ('and the', 34), ('the french', 427), ('french physicist', 156), ('physicist becquerel', 317), ('the curies worked', 416), ('curies worked together', 101), ('worked together investigating', 537), ('together investigating radioactivity', 479), ('investigating radioactivity building', 226), ('radioactivity building on', 344), ('building on the', 62), ('on the work', 300), ('the work of', 449), ('work of the', 533), ('of the german', 290), ('the german physicist', 432), ('german physicist roentgen', 167), ('physicist roentgen and', 319), ('roentgen and the', 360), ('and the french', 35), ('the french physicist', 428), ('french physicist becquerel', 157), ('in july', 215), ('july 1898', 231), ('1898 the', 4), ('curies announced', 96), ('announced the', 38), ('the discovery', 421), ('discovery of', 116), ('of new', 283), ('new chemical', 269), ('chemical element', 70), ('element polonium', 129), ('in july 1898', 216), ('july 1898 the', 232), ('1898 the curies', 5), ('the curies announced', 414), ('curies announced the', 97), ('announced the discovery', 39), ('the discovery of', 422), ('discovery of new', 118), ('of new chemical', 284), ('new chemical element', 270), ('chemical element polonium', 71), ('the end', 423), ('end of', 130), ('the year', 451), ('year they', 540), ('they announced', 453), ('of another', 277), ('another radium', 42), ('at the end', 44), ('the end of', 424), ('end of the', 131), ('of the year', 294), ('the year they', 452), ('year they announced', 541), ('they announced the', 454), ('discovery of another', 117), ('of another radium', 278), ('curies along', 94), ('along with', 14), ('with becquerel', 524), ('becquerel were', 50), ('were awarded', 510), ('prize for', 334), ('for physics', 152), ('physics in', 322), ('the curies along', 413), ('curies along with', 95), ('along with becquerel', 15), ('with becquerel were', 525), ('becquerel were awarded', 51), ('were awarded the', 511), ('nobel prize for', 273), ('prize for physics', 336), ('for physics in', 153), ('physics in 1903', 323), ('pierre life', 326), ('life was', 243), ('was cut', 498), ('cut short', 102), ('short in', 390), ('in 1906', 210), ('1906 when', 8), ('when he', 514), ('he was', 172), ('was knocked', 502), ('knocked down', 237), ('down and', 121), ('and killed', 24), ('killed by', 235), ('by carriage', 63), ('pierre life was', 327), ('life was cut', 244), ('was cut short', 499), ('cut short in', 103), ('short in 1906', 391), ('in 1906 when', 211), ('1906 when he', 9), ('when he was', 515), ('he was knocked', 173), ('was knocked down', 503), ('knocked down and', 238), ('down and killed', 122), ('and killed by', 25), ('killed by carriage', 236), ('marie took', 255), ('took over', 482), ('over his', 311), ('his teaching', 201), ('teaching post', 406), ('post becoming', 332), ('becoming the', 48), ('the first', 425), ('first woman', 146), ('woman to', 530), ('to teach', 472), ('teach at', 404), ('sorbonne and', 396), ('and devoted', 20), ('devoted herself', 112), ('herself to', 197), ('to continuing', 459), ('continuing the', 78), ('work that', 534), ('that they', 408), ('they had', 455), ('had begun', 170), ('begun together', 54), ('marie took over', 256), ('took over his', 483), ('over his teaching', 312), ('his teaching post', 202), ('teaching post becoming', 407), ('post becoming the', 333), ('becoming the first', 49), ('the first woman', 426), ('first woman to', 147), ('woman to teach', 531), ('to teach at', 473), ('teach at the', 405), ('the sorbonne and', 446), ('sorbonne and devoted', 397), ('and devoted herself', 21), ('devoted herself to', 113), ('herself to continuing', 198), ('to continuing the', 460), ('continuing the work', 79), ('the work that', 450), ('work that they', 535), ('that they had', 409), ('they had begun', 456), ('had begun together', 171), ('she received', 383), ('received second', 351), ('second nobel', 369), ('for chemistry', 148), ('chemistry in', 74), ('she received second', 384), ('received second nobel', 352), ('second nobel prize', 370), ('prize for chemistry', 335), ('for chemistry in', 149), ('chemistry in 1911', 75), ('the curie', 410), ('curie research', 90), ('research was', 357), ('was crucial', 496), ('crucial in', 84), ('in the', 218), ('the development', 419), ('development of', 110), ('of rays', 286), ('rays in', 349), ('in surgery', 217), ('the curie research', 411), ('curie research was', 91), ('research was crucial', 358), ('was crucial in', 497), ('crucial in the', 85), ('in the development', 219), ('the development of', 420), ('development of rays', 111), ('of rays in', 287), ('rays in surgery', 350), ('during world', 125), ('world war', 538), ('war one', 486), ('one curie', 303), ('curie helped', 86), ('helped to', 180), ('to equip', 462), ('equip ambulances', 134), ('ambulances with', 16), ('with ray', 528), ('ray equipment', 347), ('equipment which', 136), ('which she', 518), ('she herself', 377), ('herself drove', 193), ('drove to', 123), ('to the', 474), ('the front', 429), ('front lines', 165), ('during world war', 126), ('world war one', 539), ('war one curie', 487), ('one curie helped', 304), ('curie helped to', 87), ('helped to equip', 181), ('to equip ambulances', 463), ('equip ambulances with', 135), ('ambulances with ray', 17), ('with ray equipment', 529), ('ray equipment which', 348), ('equipment which she', 137), ('which she herself', 519), ('she herself drove', 378), ('herself drove to', 194), ('drove to the', 124), ('to the front', 475), ('the front lines', 430), ('the international', 433), ('international red', 223), ('red cross', 355), ('cross made', 82), ('made her', 245), ('her head', 182), ('head of', 174), ('of its', 281), ('its radiological', 229), ('radiological service', 345), ('service and', 371), ('she held', 375), ('held training', 178), ('training courses', 484), ('courses for', 80), ('for medical', 150), ('medical orderlies', 261), ('orderlies and', 309), ('and doctors', 22), ('doctors in', 119), ('the new', 439), ('new techniques', 271), ('the international red', 434), ('international red cross', 224), ('red cross made', 356), ('cross made her', 83), ('made her head', 246), ('her head of', 183), ('head of its', 175), ('of its radiological', 282), ('its radiological service', 230), ('radiological service and', 346), ('service and she', 372), ('and she held', 31), ('she held training', 376), ('held training courses', 179), ('training courses for', 485), ('courses for medical', 81), ('for medical orderlies', 151), ('medical orderlies and', 262), ('orderlies and doctors', 310), ('and doctors in', 23), ('doctors in the', 120), ('in the new', 220), ('the new techniques', 440), ('despite her', 108), ('her success', 189), ('success marie', 402), ('marie continued', 249), ('continued to', 76), ('to face', 464), ('face great', 140), ('great opposition', 168), ('opposition from', 307), ('from male', 163), ('male scientists', 247), ('scientists in', 365), ('in france', 213), ('france and', 154), ('she never', 381), ('never received', 267), ('received significant', 353), ('significant financial', 392), ('financial benefits', 144), ('benefits from', 55), ('from her', 158), ('her work', 192), ('despite her success', 109), ('her success marie', 190), ('success marie continued', 403), ('marie continued to', 250), ('continued to face', 77), ('to face great', 465), ('face great opposition', 141), ('great opposition from', 169), ('opposition from male', 308), ('from male scientists', 164), ('male scientists in', 248), ('scientists in france', 366), ('in france and', 214), ('france and she', 155), ('and she never', 32), ('she never received', 382), ('never received significant', 268), ('received significant financial', 354), ('significant financial benefits', 393), ('financial benefits from', 145), ('benefits from her', 56), ('from her work', 160), ('by the', 66), ('the late', 435), ('late 1920s', 239), ('1920s her', 10), ('her health', 184), ('health was', 176), ('was beginning', 492), ('beginning to', 52), ('to deteriorate', 461), ('by the late', 67), ('the late 1920s', 436), ('late 1920s her', 240), ('1920s her health', 11), ('her health was', 185), ('health was beginning', 177), ('was beginning to', 493), ('beginning to deteriorate', 53), ('she died', 373), ('died on', 114), ('on july', 295), ('july 1934', 233), ('1934 from', 12), ('from leukaemia', 161), ('leukaemia caused', 241), ('caused by', 68), ('by exposure', 64), ('exposure to', 138), ('to high', 466), ('high energy', 199), ('energy radiation', 132), ('radiation from', 341), ('her research', 188), ('she died on', 374), ('died on july', 115), ('on july 1934', 296), ('july 1934 from', 234), ('1934 from leukaemia', 13), ('from leukaemia caused', 162), ('leukaemia caused by', 242), ('caused by exposure', 69), ('by exposure to', 65), ('exposure to high', 139), ('to high energy', 467), ('high energy radiation', 200), ('energy radiation from', 133), ('radiation from her', 342), ('from her research', 159), ('curies eldest', 98), ('eldest daughter', 127), ('daughter irene', 104), ('irene was', 227), ('was herself', 500), ('herself scientist', 195), ('scientist and', 363), ('and winner', 36), ('winner of', 522), ('the curies eldest', 415), ('curies eldest daughter', 99), ('eldest daughter irene', 128), ('daughter irene was', 105), ('irene was herself', 228), ('was herself scientist', 501), ('herself scientist and', 196), ('scientist and winner', 364), ('and winner of', 37), ('winner of the', 523), ('of the nobel', 292)])"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vocabulary.items()"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(5, 'of the'),\n",
" (4, 'the curies'),\n",
" (4, 'nobel prize'),\n",
" (3, 'the nobel prize'),\n",
" (3, 'the nobel'),\n",
" (3, 'prize for'),\n",
" (3, 'nobel prize for'),\n",
" (3, 'at the'),\n",
" (3, 'and she'),\n",
" (2, 'the work'),\n",
" (2, 'the sorbonne'),\n",
" (2, 'the discovery of'),\n",
" (2, 'the discovery'),\n",
" (2, 'she went'),\n",
" (2, 'prize for chemistry'),\n",
" (2, 'in the'),\n",
" (2, 'in 1911'),\n",
" (2, 'in 1903'),\n",
" (2, 'from her'),\n",
" (2, 'for chemistry'),\n",
" (2, 'discovery of'),\n",
" (2, 'awarded the nobel'),\n",
" (2, 'awarded the'),\n",
" (2, 'at the sorbonne'),\n",
" (2, 'announced the discovery'),\n",
" (2, 'announced the'),\n",
" (1, 'year they announced'),\n",
" (1, 'year they'),\n",
" (1, 'world war one'),\n",
" (1, 'world war'),\n",
" (1, 'worked together investigating'),\n",
" (1, 'worked together'),\n",
" (1, 'work that they'),\n",
" (1, 'work that'),\n",
" (1, 'work of the'),\n",
" (1, 'work of'),\n",
" (1, 'woman to teach'),\n",
" (1, 'woman to'),\n",
" (1, 'with ray equipment'),\n",
" (1, 'with ray'),\n",
" (1, 'with her husband'),\n",
" (1, 'with her'),\n",
" (1, 'with becquerel were'),\n",
" (1, 'with becquerel'),\n",
" (1, 'winner of the'),\n",
" (1, 'winner of'),\n",
" (1, 'win another in'),\n",
" (1, 'win another'),\n",
" (1, 'which she herself'),\n",
" (1, 'which she'),\n",
" (1, 'where she met'),\n",
" (1, 'where she'),\n",
" (1, 'when he was'),\n",
" (1, 'when he'),\n",
" (1, 'were married in'),\n",
" (1, 'were married'),\n",
" (1, 'were awarded the'),\n",
" (1, 'were awarded'),\n",
" (1, 'went to paris'),\n",
" (1, 'went to'),\n",
" (1, 'went on to'),\n",
" (1, 'went on'),\n",
" (1, 'was polish born'),\n",
" (1, 'was polish'),\n",
" (1, 'was knocked down'),\n",
" (1, 'was knocked'),\n",
" (1, 'was herself scientist'),\n",
" (1, 'was herself'),\n",
" (1, 'was cut short'),\n",
" (1, 'was cut'),\n",
" (1, 'was crucial in'),\n",
" (1, 'was crucial'),\n",
" (1, 'was born in'),\n",
" (1, 'was born'),\n",
" (1, 'was beginning to'),\n",
" (1, 'was beginning'),\n",
" (1, 'was awarded the'),\n",
" (1, 'was awarded'),\n",
" (1, 'warsaw on november'),\n",
" (1, 'warsaw on'),\n",
" (1, 'war one curie'),\n",
" (1, 'war one'),\n",
" (1, 'training courses for'),\n",
" (1, 'training courses'),\n",
" (1, 'took over his'),\n",
" (1, 'took over'),\n",
" (1, 'together with her'),\n",
" (1, 'together with'),\n",
" (1, 'together investigating radioactivity'),\n",
" (1, 'together investigating'),\n",
" (1, 'to win another'),\n",
" (1, 'to win'),\n",
" (1, 'to the front'),\n",
" (1, 'to the'),\n",
" (1, 'to teach at'),\n",
" (1, 'to teach'),\n",
" (1, 'to study physics'),\n",
" (1, 'to study'),\n",
" (1, 'to paris to'),\n",
" (1, 'to paris'),\n",
" (1, 'to high energy'),\n",
" (1, 'to high'),\n",
" (1, 'to face great'),\n",
" (1, 'to face'),\n",
" (1, 'to equip ambulances'),\n",
" (1, 'to equip'),\n",
" (1, 'to deteriorate'),\n",
" (1, 'to continuing the'),\n",
" (1, 'to continuing'),\n",
" (1, 'they were married'),\n",
" (1, 'they were'),\n",
" (1, 'they had begun'),\n",
" (1, 'they had'),\n",
" (1, 'they announced the'),\n",
" (1, 'they announced'),\n",
" (1, 'the year they'),\n",
" (1, 'the year'),\n",
" (1, 'the work that'),\n",
" (1, 'the work of'),\n",
" (1, 'the sorbonne where'),\n",
" (1, 'the sorbonne and'),\n",
" (1, 'the school of'),\n",
" (1, 'the school'),\n",
" (1, 'the new techniques'),\n",
" (1, 'the new'),\n",
" (1, 'the most famous'),\n",
" (1, 'the most'),\n",
" (1, 'the late 1920s'),\n",
" (1, 'the late'),\n",
" (1, 'the international red'),\n",
" (1, 'the international'),\n",
" (1, 'the german physicist'),\n",
" (1, 'the german'),\n",
" (1, 'the front lines'),\n",
" (1, 'the front'),\n",
" (1, 'the french physicist'),\n",
" (1, 'the french'),\n",
" (1, 'the first woman'),\n",
" (1, 'the first'),\n",
" (1, 'the end of'),\n",
" (1, 'the end'),\n",
" (1, 'the development of'),\n",
" (1, 'the development'),\n",
" (1, 'the daughter of'),\n",
" (1, 'the daughter'),\n",
" (1, 'the curies worked'),\n",
" (1, 'the curies eldest'),\n",
" (1, 'the curies announced'),\n",
" (1, 'the curies along'),\n",
" (1, 'the curie research'),\n",
" (1, 'the curie'),\n",
" (1, 'that they had'),\n",
" (1, 'that they'),\n",
" (1, 'teaching post becoming'),\n",
" (1, 'teaching post'),\n",
" (1, 'teach at the'),\n",
" (1, 'teach at'),\n",
" (1, 'success marie continued'),\n",
" (1, 'success marie'),\n",
" (1, 'study physics and'),\n",
" (1, 'study physics'),\n",
" (1, 'sorbonne where she'),\n",
" (1, 'sorbonne where'),\n",
" (1, 'sorbonne and devoted'),\n",
" (1, 'sorbonne and'),\n",
" (1, 'sklodowska was born'),\n",
" (1, 'sklodowska was'),\n",
" (1, 'significant financial benefits'),\n",
" (1, 'significant financial'),\n",
" (1, 'short in 1906'),\n",
" (1, 'short in'),\n",
" (1, 'she went to'),\n",
" (1, 'she went on'),\n",
" (1, 'she was awarded'),\n",
" (1, 'she was'),\n",
" (1, 'she received second'),\n",
" (1, 'she received'),\n",
" (1, 'she never received'),\n",
" (1, 'she never'),\n",
" (1, 'she met pierre'),\n",
" (1, 'she met'),\n",
" (1, 'she herself drove'),\n",
" (1, 'she herself'),\n",
" (1, 'she held training'),\n",
" (1, 'she held'),\n",
" (1, 'she died on'),\n",
" (1, 'she died'),\n",
" (1, 'service and she'),\n",
" (1, 'service and'),\n",
" (1, 'second nobel prize'),\n",
" (1, 'second nobel'),\n",
" (1, 'scientists of her'),\n",
" (1, 'scientists of'),\n",
" (1, 'scientists in france'),\n",
" (1, 'scientists in'),\n",
" (1, 'scientist and winner'),\n",
" (1, 'scientist and'),\n",
" (1, 'school of physics'),\n",
" (1, 'school of'),\n",
" (1, 'roentgen and the'),\n",
" (1, 'roentgen and'),\n",
" (1, 'research was crucial'),\n",
" (1, 'research was'),\n",
" (1, 'red cross made'),\n",
" (1, 'red cross'),\n",
" (1, 'received significant financial'),\n",
" (1, 'received significant'),\n",
" (1, 'received second nobel'),\n",
" (1, 'received second'),\n",
" (1, 'rays in surgery'),\n",
" (1, 'rays in'),\n",
" (1, 'ray equipment which'),\n",
" (1, 'ray equipment'),\n",
" (1, 'radiological service and'),\n",
" (1, 'radiological service'),\n",
" (1, 'radioactivity building on'),\n",
" (1, 'radioactivity building'),\n",
" (1, 'radiation from her'),\n",
" (1, 'radiation from'),\n",
" (1, 'professor of the'),\n",
" (1, 'professor of'),\n",
" (1, 'prize in 1903'),\n",
" (1, 'prize in'),\n",
" (1, 'prize for physics'),\n",
" (1, 'post becoming the'),\n",
" (1, 'post becoming'),\n",
" (1, 'polish born physicist'),\n",
" (1, 'polish born'),\n",
" (1, 'pierre she was'),\n",
" (1, 'pierre she'),\n",
" (1, 'pierre life was'),\n",
" (1, 'pierre life'),\n",
" (1, 'pierre curie professor'),\n",
" (1, 'pierre curie'),\n",
" (1, 'physics in 1903'),\n",
" (1, 'physics in'),\n",
" (1, 'physics and mathematics'),\n",
" (1, 'physics and'),\n",
" (1, 'physicist roentgen and'),\n",
" (1, 'physicist roentgen'),\n",
" (1, 'physicist becquerel'),\n",
" (1, 'physicist and chemist'),\n",
" (1, 'physicist and'),\n",
" (1, 'paris to study'),\n",
" (1, 'paris to'),\n",
" (1, 'over his teaching'),\n",
" (1, 'over his'),\n",
" (1, 'orderlies and doctors'),\n",
" (1, 'orderlies and'),\n",
" (1, 'opposition from male'),\n",
" (1, 'opposition from'),\n",
" (1, 'one of the'),\n",
" (1, 'one of'),\n",
" (1, 'one curie helped'),\n",
" (1, 'one curie'),\n",
" (1, 'on to win'),\n",
" (1, 'on to'),\n",
" (1, 'on the work'),\n",
" (1, 'on the'),\n",
" (1, 'on november 1867'),\n",
" (1, 'on november'),\n",
" (1, 'on july 1934'),\n",
" (1, 'on july'),\n",
" (1, 'of the year'),\n",
" (1, 'of the school'),\n",
" (1, 'of the nobel'),\n",
" (1, 'of the most'),\n",
" (1, 'of the german'),\n",
" (1, 'of teacher'),\n",
" (1, 'of rays in'),\n",
" (1, 'of rays'),\n",
" (1, 'of physics'),\n",
" (1, 'of new chemical'),\n",
" (1, 'of new'),\n",
" (1, 'of its radiological'),\n",
" (1, 'of its'),\n",
" (1, 'of her time'),\n",
" (1, 'of her'),\n",
" (1, 'of another radium'),\n",
" (1, 'of another'),\n",
" (1, 'november 1867 the'),\n",
" (1, 'november 1867'),\n",
" (1, 'nobel prize in'),\n",
" (1, 'new techniques'),\n",
" (1, 'new chemical element'),\n",
" (1, 'new chemical'),\n",
" (1, 'never received significant'),\n",
" (1, 'never received'),\n",
" (1, 'most famous scientists'),\n",
" (1, 'most famous'),\n",
" (1, 'met pierre curie'),\n",
" (1, 'met pierre'),\n",
" (1, 'medical orderlies and'),\n",
" (1, 'medical orderlies'),\n",
" (1, 'mathematics at the'),\n",
" (1, 'mathematics at'),\n",
" (1, 'married in 1895'),\n",
" (1, 'married in'),\n",
" (1, 'marie took over'),\n",
" (1, 'marie took'),\n",
" (1, 'marie sklodowska was'),\n",
" (1, 'marie sklodowska'),\n",
" (1, 'marie curie was'),\n",
" (1, 'marie curie'),\n",
" (1, 'marie continued to'),\n",
" (1, 'marie continued'),\n",
" (1, 'male scientists in'),\n",
" (1, 'male scientists'),\n",
" (1, 'made her head'),\n",
" (1, 'made her'),\n",
" (1, 'life was cut'),\n",
" (1, 'life was'),\n",
" (1, 'leukaemia caused by'),\n",
" (1, 'leukaemia caused'),\n",
" (1, 'late 1920s her'),\n",
" (1, 'late 1920s'),\n",
" (1, 'knocked down and'),\n",
" (1, 'knocked down'),\n",
" (1, 'killed by carriage'),\n",
" (1, 'killed by'),\n",
" (1, 'july 1934 from'),\n",
" (1, 'july 1934'),\n",
" (1, 'july 1898 the'),\n",
" (1, 'july 1898'),\n",
" (1, 'its radiological service'),\n",
" (1, 'its radiological'),\n",
" (1, 'irene was herself'),\n",
" (1, 'irene was'),\n",
" (1, 'investigating radioactivity building'),\n",
" (1, 'investigating radioactivity'),\n",
" (1, 'international red cross'),\n",
" (1, 'international red'),\n",
" (1, 'in warsaw on'),\n",
" (1, 'in warsaw'),\n",
" (1, 'in the new'),\n",
" (1, 'in the development'),\n",
" (1, 'in surgery'),\n",
" (1, 'in july 1898'),\n",
" (1, 'in july'),\n",
" (1, 'in france and'),\n",
" (1, 'in france'),\n",
" (1, 'in 1906 when'),\n",
" (1, 'in 1906'),\n",
" (1, 'in 1903 and'),\n",
" (1, 'in 1895'),\n",
" (1, 'in 1891 she'),\n",
" (1, 'in 1891'),\n",
" (1, 'husband pierre she'),\n",
" (1, 'husband pierre'),\n",
" (1, 'his teaching post'),\n",
" (1, 'his teaching'),\n",
" (1, 'high energy radiation'),\n",
" (1, 'high energy'),\n",
" (1, 'herself to continuing'),\n",
" (1, 'herself to'),\n",
" (1, 'herself scientist and'),\n",
" (1, 'herself scientist'),\n",
" (1, 'herself drove to'),\n",
" (1, 'herself drove'),\n",
" (1, 'her work'),\n",
" (1, 'her time'),\n",
" (1, 'her success marie'),\n",
" (1, 'her success'),\n",
" (1, 'her research'),\n",
" (1, 'her husband pierre'),\n",
" (1, 'her husband'),\n",
" (1, 'her health was'),\n",
" (1, 'her health'),\n",
" (1, 'her head of'),\n",
" (1, 'her head'),\n",
" (1, 'helped to equip'),\n",
" (1, 'helped to'),\n",
" (1, 'held training courses'),\n",
" (1, 'held training'),\n",
" (1, 'health was beginning'),\n",
" (1, 'health was'),\n",
" (1, 'head of its'),\n",
" (1, 'head of'),\n",
" (1, 'he was knocked'),\n",
" (1, 'he was'),\n",
" (1, 'had begun together'),\n",
" (1, 'had begun'),\n",
" (1, 'great opposition from'),\n",
" (1, 'great opposition'),\n",
" (1, 'german physicist roentgen'),\n",
" (1, 'german physicist'),\n",
" (1, 'front lines'),\n",
" (1, 'from male scientists'),\n",
" (1, 'from male'),\n",
" (1, 'from leukaemia caused'),\n",
" (1, 'from leukaemia'),\n",
" (1, 'from her work'),\n",
" (1, 'from her research'),\n",
" (1, 'french physicist becquerel'),\n",
" (1, 'french physicist'),\n",
" (1, 'france and she'),\n",
" (1, 'france and'),\n",
" (1, 'for physics in'),\n",
" (1, 'for physics'),\n",
" (1, 'for medical orderlies'),\n",
" (1, 'for medical'),\n",
" (1, 'for chemistry in'),\n",
" (1, 'first woman to'),\n",
" (1, 'first woman'),\n",
" (1, 'financial benefits from'),\n",
" (1, 'financial benefits'),\n",
" (1, 'famous scientists of'),\n",
" (1, 'famous scientists'),\n",
" (1, 'face great opposition'),\n",
" (1, 'face great'),\n",
" (1, 'exposure to high'),\n",
" (1, 'exposure to'),\n",
" (1, 'equipment which she'),\n",
" (1, 'equipment which'),\n",
" (1, 'equip ambulances with'),\n",
" (1, 'equip ambulances'),\n",
" (1, 'energy radiation from'),\n",
" (1, 'energy radiation'),\n",
" (1, 'end of the'),\n",
" (1, 'end of'),\n",
" (1, 'element polonium'),\n",
" (1, 'eldest daughter irene'),\n",
" (1, 'eldest daughter'),\n",
" (1, 'during world war'),\n",
" (1, 'during world'),\n",
" (1, 'drove to the'),\n",
" (1, 'drove to'),\n",
" (1, 'down and killed'),\n",
" (1, 'down and'),\n",
" (1, 'doctors in the'),\n",
" (1, 'doctors in'),\n",
" (1, 'discovery of new'),\n",
" (1, 'discovery of another'),\n",
" (1, 'died on july'),\n",
" (1, 'died on'),\n",
" (1, 'devoted herself to'),\n",
" (1, 'devoted herself'),\n",
" (1, 'development of rays'),\n",
" (1, 'development of'),\n",
" (1, 'despite her success'),\n",
" (1, 'despite her'),\n",
" (1, 'daughter of teacher'),\n",
" (1, 'daughter of'),\n",
" (1, 'daughter irene was'),\n",
" (1, 'daughter irene'),\n",
" (1, 'cut short in'),\n",
" (1, 'cut short'),\n",
" (1, 'curies worked together'),\n",
" (1, 'curies worked'),\n",
" (1, 'curies eldest daughter'),\n",
" (1, 'curies eldest'),\n",
" (1, 'curies announced the'),\n",
" (1, 'curies announced'),\n",
" (1, 'curies along with'),\n",
" (1, 'curies along'),\n",
" (1, 'curie was polish'),\n",
" (1, 'curie was'),\n",
" (1, 'curie research was'),\n",
" (1, 'curie research'),\n",
" (1, 'curie professor of'),\n",
" (1, 'curie professor'),\n",
" (1, 'curie helped to'),\n",
" (1, 'curie helped'),\n",
" (1, 'crucial in the'),\n",
" (1, 'crucial in'),\n",
" (1, 'cross made her'),\n",
" (1, 'cross made'),\n",
" (1, 'courses for medical'),\n",
" (1, 'courses for'),\n",
" (1, 'continuing the work'),\n",
" (1, 'continuing the'),\n",
" (1, 'continued to face'),\n",
" (1, 'continued to'),\n",
" (1, 'chemistry in 1911'),\n",
" (1, 'chemistry in'),\n",
" (1, 'chemist and one'),\n",
" (1, 'chemist and'),\n",
" (1, 'chemical element polonium'),\n",
" (1, 'chemical element'),\n",
" (1, 'caused by exposure'),\n",
" (1, 'caused by'),\n",
" (1, 'by the late'),\n",
" (1, 'by the'),\n",
" (1, 'by exposure to'),\n",
" (1, 'by exposure'),\n",
" (1, 'by carriage'),\n",
" (1, 'building on the'),\n",
" (1, 'building on'),\n",
" (1, 'born physicist and'),\n",
" (1, 'born physicist'),\n",
" (1, 'born in warsaw'),\n",
" (1, 'born in'),\n",
" (1, 'benefits from her'),\n",
" (1, 'benefits from'),\n",
" (1, 'begun together'),\n",
" (1, 'beginning to deteriorate'),\n",
" (1, 'beginning to'),\n",
" (1, 'becquerel were awarded'),\n",
" (1, 'becquerel were'),\n",
" (1, 'becoming the first'),\n",
" (1, 'becoming the'),\n",
" (1, 'at the end'),\n",
" (1, 'another radium'),\n",
" (1, 'another in 1911'),\n",
" (1, 'another in'),\n",
" (1, 'and winner of'),\n",
" (1, 'and winner'),\n",
" (1, 'and the french'),\n",
" (1, 'and the'),\n",
" (1, 'and she went'),\n",
" (1, 'and she never'),\n",
" (1, 'and she held'),\n",
" (1, 'and one of'),\n",
" (1, 'and one'),\n",
" (1, 'and mathematics at'),\n",
" (1, 'and mathematics'),\n",
" (1, 'and killed by'),\n",
" (1, 'and killed'),\n",
" (1, 'and doctors in'),\n",
" (1, 'and doctors'),\n",
" (1, 'and devoted herself'),\n",
" (1, 'and devoted'),\n",
" (1, 'and chemist and'),\n",
" (1, 'and chemist'),\n",
" (1, 'ambulances with ray'),\n",
" (1, 'ambulances with'),\n",
" (1, 'along with becquerel'),\n",
" (1, 'along with'),\n",
" (1, '1934 from leukaemia'),\n",
" (1, '1934 from'),\n",
" (1, '1920s her health'),\n",
" (1, '1920s her'),\n",
" (1, '1906 when he'),\n",
" (1, '1906 when'),\n",
" (1, '1903 and she'),\n",
" (1, '1903 and'),\n",
" (1, '1898 the curies'),\n",
" (1, '1898 the'),\n",
" (1, '1891 she went'),\n",
" (1, '1891 she'),\n",
" (1, '1867 the daughter'),\n",
" (1, '1867 the')]"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted_word_list = sorted([(word_count[i], n_gram) for n_gram, i in vocabulary.items()], reverse=True)\n",
"\n",
"sorted_word_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using nltk to find ngrams"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['A bird in hand is worth two in the bush.',\n",
" 'Good things come to those who wait.',\n",
" 'These watches cost $1500! ',\n",
" 'There are other fish in the sea.',\n",
" 'The ball is in your court.',\n",
" 'Mr. Smith Goes to Washington ',\n",
" 'Doogie Howser M.D.']"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_text"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"from nltk import bigrams\n",
"from nltk import trigrams\n",
"\n",
"from nltk.tokenize import word_tokenize"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['A',\n",
" 'bird',\n",
" 'in',\n",
" 'hand',\n",
" 'is',\n",
" 'worth',\n",
" 'two',\n",
" 'in',\n",
" 'the',\n",
" 'bush',\n",
" '.',\n",
" 'Good',\n",
" 'things',\n",
" 'come',\n",
" 'to',\n",
" 'those',\n",
" 'who',\n",
" 'wait',\n",
" '.',\n",
" 'These',\n",
" 'watches',\n",
" 'cost',\n",
" '$',\n",
" '1500',\n",
" '!',\n",
" 'There',\n",
" 'are',\n",
" 'other',\n",
" 'fish',\n",
" 'in',\n",
" 'the',\n",
" 'sea',\n",
" '.',\n",
" 'The',\n",
" 'ball',\n",
" 'is',\n",
" 'in',\n",
" 'your',\n",
" 'court',\n",
" '.',\n",
" 'Mr.',\n",
" 'Smith',\n",
" 'Goes',\n",
" 'to',\n",
" 'Washington',\n",
" 'Doogie',\n",
" 'Howser',\n",
" 'M.D',\n",
" '.']"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"word_tokens = word_tokenize(\" \".join(train_text))\n",
"\n",
"word_tokens"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('A', 'bird'),\n",
" ('bird', 'in'),\n",
" ('in', 'hand'),\n",
" ('hand', 'is'),\n",
" ('is', 'worth'),\n",
" ('worth', 'two'),\n",
" ('two', 'in'),\n",
" ('in', 'the'),\n",
" ('the', 'bush'),\n",
" ('bush', '.'),\n",
" ('.', 'Good'),\n",
" ('Good', 'things'),\n",
" ('things', 'come'),\n",
" ('come', 'to'),\n",
" ('to', 'those'),\n",
" ('those', 'who'),\n",
" ('who', 'wait'),\n",
" ('wait', '.'),\n",
" ('.', 'These'),\n",
" ('These', 'watches'),\n",
" ('watches', 'cost'),\n",
" ('cost', '$'),\n",
" ('$', '1500'),\n",
" ('1500', '!'),\n",
" ('!', 'There'),\n",
" ('There', 'are'),\n",
" ('are', 'other'),\n",
" ('other', 'fish'),\n",
" ('fish', 'in'),\n",
" ('in', 'the'),\n",
" ('the', 'sea'),\n",
" ('sea', '.'),\n",
" ('.', 'The'),\n",
" ('The', 'ball'),\n",
" ('ball', 'is'),\n",
" ('is', 'in'),\n",
" ('in', 'your'),\n",
" ('your', 'court'),\n",
" ('court', '.'),\n",
" ('.', 'Mr.'),\n",
" ('Mr.', 'Smith'),\n",
" ('Smith', 'Goes'),\n",
" ('Goes', 'to'),\n",
" ('to', 'Washington'),\n",
" ('Washington', 'Doogie'),\n",
" ('Doogie', 'Howser'),\n",
" ('Howser', 'M.D'),\n",
" ('M.D', '.')]"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nltk_bigrams = bigrams(word_tokens)\n",
"\n",
"list(nltk_bigrams)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('A', 'bird', 'in'),\n",
" ('bird', 'in', 'hand'),\n",
" ('in', 'hand', 'is'),\n",
" ('hand', 'is', 'worth'),\n",
" ('is', 'worth', 'two'),\n",
" ('worth', 'two', 'in'),\n",
" ('two', 'in', 'the'),\n",
" ('in', 'the', 'bush'),\n",
" ('the', 'bush', '.'),\n",
" ('bush', '.', 'Good'),\n",
" ('.', 'Good', 'things'),\n",
" ('Good', 'things', 'come'),\n",
" ('things', 'come', 'to'),\n",
" ('come', 'to', 'those'),\n",
" ('to', 'those', 'who'),\n",
" ('those', 'who', 'wait'),\n",
" ('who', 'wait', '.'),\n",
" ('wait', '.', 'These'),\n",
" ('.', 'These', 'watches'),\n",
" ('These', 'watches', 'cost'),\n",
" ('watches', 'cost', '$'),\n",
" ('cost', '$', '1500'),\n",
" ('$', '1500', '!'),\n",
" ('1500', '!', 'There'),\n",
" ('!', 'There', 'are'),\n",
" ('There', 'are', 'other'),\n",
" ('are', 'other', 'fish'),\n",
" ('other', 'fish', 'in'),\n",
" ('fish', 'in', 'the'),\n",
" ('in', 'the', 'sea'),\n",
" ('the', 'sea', '.'),\n",
" ('sea', '.', 'The'),\n",
" ('.', 'The', 'ball'),\n",
" ('The', 'ball', 'is'),\n",
" ('ball', 'is', 'in'),\n",
" ('is', 'in', 'your'),\n",
" ('in', 'your', 'court'),\n",
" ('your', 'court', '.'),\n",
" ('court', '.', 'Mr.'),\n",
" ('.', 'Mr.', 'Smith'),\n",
" ('Mr.', 'Smith', 'Goes'),\n",
" ('Smith', 'Goes', 'to'),\n",
" ('Goes', 'to', 'Washington'),\n",
" ('to', 'Washington', 'Doogie'),\n",
" ('Washington', 'Doogie', 'Howser'),\n",
" ('Doogie', 'Howser', 'M.D'),\n",
" ('Howser', 'M.D', '.')]"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nltk_trigrams = trigrams(word_tokens)\n",
"\n",
"list(nltk_trigrams)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('A', 'bird', 'in', 'hand', 'is')\n",
"('bird', 'in', 'hand', 'is', 'worth')\n",
"('in', 'hand', 'is', 'worth', 'two')\n",
"('hand', 'is', 'worth', 'two', 'in')\n",
"('is', 'worth', 'two', 'in', 'the')\n",
"('worth', 'two', 'in', 'the', 'bush')\n",
"('two', 'in', 'the', 'bush', '.')\n",
"('in', 'the', 'bush', '.', 'Good')\n",
"('the', 'bush', '.', 'Good', 'things')\n",
"('bush', '.', 'Good', 'things', 'come')\n",
"('.', 'Good', 'things', 'come', 'to')\n",
"('Good', 'things', 'come', 'to', 'those')\n",
"('things', 'come', 'to', 'those', 'who')\n",
"('come', 'to', 'those', 'who', 'wait')\n",
"('to', 'those', 'who', 'wait', '.')\n",
"('those', 'who', 'wait', '.', 'These')\n",
"('who', 'wait', '.', 'These', 'watches')\n",
"('wait', '.', 'These', 'watches', 'cost')\n",
"('.', 'These', 'watches', 'cost', '$')\n",
"('These', 'watches', 'cost', '$', '1500')\n",
"('watches', 'cost', '$', '1500', '!')\n",
"('cost', '$', '1500', '!', 'There')\n",
"('$', '1500', '!', 'There', 'are')\n",
"('1500', '!', 'There', 'are', 'other')\n",
"('!', 'There', 'are', 'other', 'fish')\n",
"('There', 'are', 'other', 'fish', 'in')\n",
"('are', 'other', 'fish', 'in', 'the')\n",
"('other', 'fish', 'in', 'the', 'sea')\n",
"('fish', 'in', 'the', 'sea', '.')\n",
"('in', 'the', 'sea', '.', 'The')\n",
"('the', 'sea', '.', 'The', 'ball')\n",
"('sea', '.', 'The', 'ball', 'is')\n",
"('.', 'The', 'ball', 'is', 'in')\n",
"('The', 'ball', 'is', 'in', 'your')\n",
"('ball', 'is', 'in', 'your', 'court')\n",
"('is', 'in', 'your', 'court', '.')\n",
"('in', 'your', 'court', '.', 'Mr.')\n",
"('your', 'court', '.', 'Mr.', 'Smith')\n",
"('court', '.', 'Mr.', 'Smith', 'Goes')\n",
"('.', 'Mr.', 'Smith', 'Goes', 'to')\n",
"('Mr.', 'Smith', 'Goes', 'to', 'Washington')\n",
"('Smith', 'Goes', 'to', 'Washington', 'Doogie')\n",
"('Goes', 'to', 'Washington', 'Doogie', 'Howser')\n",
"('to', 'Washington', 'Doogie', 'Howser', 'M.D')\n",
"('Washington', 'Doogie', 'Howser', 'M.D', '.')\n"
]
}
],
"source": [
"from nltk import ngrams\n",
"\n",
"fivegrams = ngrams(word_tokens, 5)\n",
"\n",
"for grams in fivegrams:\n",
" print(grams)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* https://tedboy.github.io/nlps/generated/generated/nltk.BigramAssocMeasures\n",
"* http://www.nltk.org/_modules/nltk/collocations.html#BigramCollocationFinder"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"from nltk.collocations import BigramAssocMeasures\n",
"from nltk.collocations import BigramCollocationFinder"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"word_tokens = word_tokenize(file_contents)"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('.', 'The'),\n",
" ('of', 'the'),\n",
" ('Nobel', 'Prize'),\n",
" (',', 'and'),\n",
" ('The', 'Curies'),\n",
" ('and', 'she'),\n",
" ('the', 'Nobel')]"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bigram_measures = BigramAssocMeasures()\n",
"\n",
"finder = BigramCollocationFinder.from_words(word_tokens)\n",
"\n",
"finder.apply_freq_filter(3)\n",
"\n",
"matches = finder.nbest(bigram_measures.raw_freq, 15)\n",
"\n",
"matches"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('.', 'The'),\n",
" ('of', 'the'),\n",
" ('Nobel', 'Prize'),\n",
" (',', 'and'),\n",
" ('The', 'Curies'),\n",
" ('and', 'she'),\n",
" ('the', 'Nobel'),\n",
" (',', 'she'),\n",
" (',', 'the'),\n",
" ('.', 'In'),\n",
" ('.', 'Marie'),\n",
" ('.', 'She'),\n",
" ('1911', '.'),\n",
" ('Prize', 'for'),\n",
" ('announced', 'the')]"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bigram_measures = BigramAssocMeasures()\n",
"\n",
"finder = BigramCollocationFinder.from_words(word_tokens)\n",
"\n",
"finder.apply_freq_filter(2)\n",
"\n",
"matches = finder.nbest(bigram_measures.raw_freq, 15)\n",
"\n",
"matches"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}