From fbe88f01b95fb125f1fe8d9e4f2ca35b46dbc757 Mon Sep 17 00:00:00 2001 From: nicode3141 Date: Wed, 11 Nov 2026 20:50:23 +0100 Subject: [PATCH] (plan): created hollow interfaces, including todos --- README.md | 13 ++- data/labels.csv | 107 ++++++++++-------- .../java/de/nicode3141/genredct/Detector.java | 46 ++++++++ .../de/nicode3141/genredct/GenreData.java | 33 ++++++ 4 files changed, 152 insertions(+), 47 deletions(-) create mode 100644 src/main/java/de/nicode3141/genredct/Detector.java create mode 100644 src/main/java/de/nicode3141/genredct/GenreData.java diff --git a/README.md b/README.md index d5c4f9f..139a342 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,13 @@ # Genre Detector -A simple genre detection tool for any given text +A genre detection tool that categorizes text using a perceptron-based machine model. + +This project was developed for a peer group and was inspired by Prof. Hummels MNIST Perceptron example. + +Genres: +- adventure +- horror +- mystery +- romance +- scifi + +Note: The text data used here was sourced from [Gutenberg](http://gutenberg.org/). \ No newline at end of file diff --git a/data/labels.csv b/data/labels.csv index d979cdc..7797755 100644 --- a/data/labels.csv +++ b/data/labels.csv @@ -1,46 +1,61 @@ -id,title,author,genre,split,chars,words,file -40840,"Ten Years Among the Mail Bags Or, Notes from the Diary of a Special Agent of the Post-Office Department","Holbrook, James, 1812-1864",mystery,test,830785,141311,data/test/mystery/40840_ten-years-among-the-mail-bags-or-notes-from-the-di.txt -73230,The thing on the door-step,"Lovecraft, H. P. (Howard Phillips), 1890-1937",horror,train,61778,10909,data/train/horror/73230_the-thing-on-the-door-step.txt -16959,"'Way Down East A Romance of New England Life","Grismer, Joseph Rhode, 1849-1922",romance,train,184693,33484,data/train/romance/16959_way-down-east-a-romance-of-new-england-life.txt -24124,"Down the Rhine; Or, Young America in Germany","Optic, Oliver, 1822-1897",adventure,train,498284,88201,data/train/adventure/24124_down-the-rhine-or-young-america-in-germany.txt -3071,"The Golden Slipper, and Other Problems for Violet Strange","Green, Anna Katharine, 1846-1935",mystery,train,500523,92708,data/train/mystery/3071_the-golden-slipper-and-other-problems-for-violet-s.txt -64152,The Strange Visitation,"Corelli, Marie, 1855-1924",horror,test,161428,29095,data/test/horror/64152_the-strange-visitation.txt -391,The Song of Roland,"Scott-Moncrieff, C. K. (Charles Kenneth), 1889-1930 [Translator]",romance,train,192262,32466,data/train/romance/391_the-song-of-roland.txt -29034,"Harper's Young People, July 13, 1880 An Illustrated Weekly",Various,adventure,train,96077,16490,data/train/adventure/29034_harper-s-young-people-july-13-1880-an-illustrated-.txt -79180,The range-boomer,"Tuttle, W. C. (Wilbur C.), 1883-1969",mystery,test,253150,47892,data/test/mystery/79180_the-range-boomer.txt -50775,Twenty Years' Experience as a Ghost Hunter,"O'Donnell, Elliott, 1872-1965",horror,train,407011,74166,data/train/horror/50775_twenty-years-experience-as-a-ghost-hunter.txt -56705,Mrs. Essington: The Romance of a House-party,"Chamberlain, Esther, -1908",romance,train,205565,36532,data/train/romance/56705_mrs-essington-the-romance-of-a-house-party.txt -24196,Victory,"Del Rey, Lester, 1915-1993",scifi,train,99837,17625,data/train/scifi/24196_victory.txt -21546,"Nero, the Circus Lion: His Many Adventures","Barnum, Richard",adventure,train,126730,24514,data/train/adventure/21546_nero-the-circus-lion-his-many-adventures.txt -42010,The Barrel Mystery,"Flynn, William J. (William James), 1867-1928",mystery,train,272354,49779,data/train/mystery/42010_the-barrel-mystery.txt -6995,Ghosts I Have Met and Some Others,"Bangs, John Kendrick, 1862-1922",horror,train,193947,36035,data/train/horror/6995_ghosts-i-have-met-and-some-others.txt -23745,Aladdin & Co.: A Romance of Yankee Magic,"Quick, Herbert, 1861-1925",romance,train,487474,87886,data/train/romance/23745_aladdin-co-a-romance-of-yankee-magic.txt -32128,The Enormous Room,"Gold, H. L. (Horace Leonard), 1914-1996",scifi,train,129246,23154,data/train/scifi/32128_the-enormous-room.txt -67135,"The Lives of Celebrated Travellers, Vol. 3 (of 3)","St. John, James Augustus, 1795-1875",adventure,train,741273,126459,data/train/adventure/67135_the-lives-of-celebrated-travellers-vol-3-of-3.txt -16692,Beyond The Rocks: A Love Story,"Glyn, Elinor, 1864-1943",romance,test,359192,65728,data/test/romance/16692_beyond-the-rocks-a-love-story.txt -51842,Beyond Bedlam,"Guin, Wyman, 1915-1989",scifi,train,124403,21719,data/train/scifi/51842_beyond-bedlam.txt -23036,"Dave Darrin After the Mine Layers; Or, Hitting the Enemy a Hard Naval Blow","Hancock, H. Irving (Harrie Irving), 1868-1922",adventure,train,292417,51301,data/train/adventure/23036_dave-darrin-after-the-mine-layers-or-hitting-the-e.txt -70996,The D'Arblay mystery,"Freeman, R. Austin (Richard Austin), 1862-1943",mystery,train,451536,83339,data/train/mystery/70996_the-d-arblay-mystery.txt -40823,Ghostly Phenomena,"O'Donnell, Elliott, 1872-1965",horror,train,205072,35689,data/train/horror/40823_ghostly-phenomena.txt -53157,Wood and Stone: A Romance,"Powys, John Cowper, 1872-1963",romance,train,1113358,198583,data/train/romance/53157_wood-and-stone-a-romance.txt -60632,"The Fortunate Island, and Other Stories","Clark, Charles Heber, 1841-1915",adventure,test,395265,70987,data/test/adventure/60632_the-fortunate-island-and-other-stories.txt -26651,The Flaming Jewel,"Chambers, Robert W. (Robert William), 1865-1933",mystery,train,378937,66643,data/train/mystery/26651_the-flaming-jewel.txt -16258,The Squire of Sandal-Side: A Pastoral Romance,"Barr, Amelia E., 1831-1919",romance,test,362113,66443,data/test/romance/16258_the-squire-of-sandal-side-a-pastoral-romance.txt -21449,With Axe and Rifle,"Kingston, William Henry Giles, 1814-1880",adventure,train,547218,101147,data/train/adventure/21449_with-axe-and-rifle.txt -17953,The Haunters & The Haunted Ghost Stories And Tales Of The Supernatural,"Rhys, Ernest, 1859-1946 [Editor]",horror,train,644570,116647,data/train/horror/17953_the-haunters-the-haunted-ghost-stories-and-tales-o.txt -35358,A Song of a Single Note: A Love Story,"Barr, Amelia E., 1831-1919",romance,test,494490,90388,data/test/romance/35358_a-song-of-a-single-note-a-love-story.txt -45802,Knock Three Times!,"Webb, Marion St. John, 1888-1930",adventure,train,287960,54159,data/train/adventure/45802_knock-three-times.txt -68140,"Nick Carter Stories No. 154, August 21, 1915: The mask of death; or, Nick Carter's curious case.","Carter, Nicholas (House name)",mystery,train,197157,34948,data/train/mystery/68140_nick-carter-stories-no-154-august-21-1915-the-mask.txt -39168,Sophia: A Romance,"Weyman, Stanley John, 1855-1928",romance,train,596891,109633,data/train/romance/39168_sophia-a-romance.txt -55950,A Madcap Cruise,"Bates, Oric, 1883-1918",adventure,train,372890,67011,data/train/adventure/55950_a-madcap-cruise.txt -4046,The Garden of Survival,"Blackwood, Algernon, 1869-1951",horror,train,126862,22438,data/train/horror/4046_the-garden-of-survival.txt -15738,Married life;,"Edginton, May, 1883-1957",romance,test,485353,88208,data/test/romance/15738_married-life.txt -51809,Survival Kit,"Pohl, Frederik, 1919-2013",scifi,train,66608,11872,data/train/scifi/51809_survival-kit.txt -16756,The Bobbsey Twins at the County Fair,"Hope, Laura Lee",adventure,test,192617,36060,data/test/adventure/16756_the-bobbsey-twins-at-the-county-fair.txt -40963,"Johnny Ludlow, Sixth Series","Wood, Henry, Mrs., 1814-1887",mystery,train,689942,127198,data/train/mystery/40963_johnny-ludlow-sixth-series.txt -54532,"The Night-Side of Nature; Or, Ghosts and Ghost-Seers","Crowe, Catherine, 1790-1876",horror,train,967242,170771,data/train/horror/54532_the-night-side-of-nature-or-ghosts-and-ghost-seers.txt -76361,The odyssey of a torpedoed tramp,"Larrouy, Maurice, 1882-1939",adventure,test,316253,60005,data/test/adventure/76361_the-odyssey-of-a-torpedoed-tramp.txt -78704,The bungalow mystery,"Keene, Carolyn",mystery,test,216367,39284,data/test/mystery/78704_the-bungalow-mystery.txt -7120,"Knock, Knock, Knock and Other Stories","Turgenev, Ivan Sergeevich, 1818-1883",horror,train,345932,63247,data/train/horror/7120_knock-knock-knock-and-other-stories.txt -44159,Penelope,"Scargill, William Pitt, 1787-1836",romance,train,304947,53003,data/train/romance/44159_penelope.txt -2154,Around the World in Eighty Days. Junior Deluxe Edition,"Verne, Jules, 1828-1905",adventure,train,367395,63562,data/train/adventure/2154_around-the-world-in-eighty-days-junior-deluxe-edit.txt +id,title,author,genre,split,chars,words,file +522200,The Detective Case No. 0,"Author, Demo 0",mystery,test,73320,17000,data/test/mystery/522200_the-detective-case-no-0.txt +4762500,The Horror Case No. 0,"Author, Demo 0",horror,test,71426,17000,data/test/horror/4762500_the-horror-case-no-0.txt +3541900,The Love Stories Case No. 0,"Author, Demo 0",romance,test,72275,17000,data/test/romance/3541900_the-love-stories-case-no-0.txt +6814100,The Science Fiction Case No. 0,"Author, Demo 0",scifi,train,71280,17000,data/train/scifi/6814100_the-science-fiction-case-no-0.txt +5278300,The Adventure Case No. 0,"Author, Demo 0",adventure,train,71619,17000,data/train/adventure/5278300_the-adventure-case-no-0.txt +4254500,The Mystery Case No. 0,"Author, Demo 0",mystery,test,73001,17000,data/test/mystery/4254500_the-mystery-case-no-0.txt +816100,The Ghost Case No. 0,"Author, Demo 0",horror,test,71806,17000,data/test/horror/816100_the-ghost-case-no-0.txt +4857600,The Romance Case No. 0,"Author, Demo 0",romance,test,72510,17000,data/test/romance/4857600_the-romance-case-no-0.txt +6814101,The Science Fiction Case No. 1,"Author, Demo 1",scifi,train,71462,17000,data/train/scifi/6814101_the-science-fiction-case-no-1.txt +5278301,The Adventure Case No. 1,"Author, Demo 1",adventure,train,71780,17000,data/train/adventure/5278301_the-adventure-case-no-1.txt +522201,The Detective Case No. 1,"Author, Demo 1",mystery,train,72727,17000,data/train/mystery/522201_the-detective-case-no-1.txt +4762501,The Horror Case No. 1,"Author, Demo 1",horror,train,71394,17000,data/train/horror/4762501_the-horror-case-no-1.txt +3541901,The Love Stories Case No. 1,"Author, Demo 1",romance,train,72493,17000,data/train/romance/3541901_the-love-stories-case-no-1.txt +6814102,The Science Fiction Case No. 2,"Author, Demo 2",scifi,train,71419,17000,data/train/scifi/6814102_the-science-fiction-case-no-2.txt +5278302,The Adventure Case No. 2,"Author, Demo 2",adventure,train,71828,17000,data/train/adventure/5278302_the-adventure-case-no-2.txt +4254501,The Mystery Case No. 1,"Author, Demo 1",mystery,train,73133,17000,data/train/mystery/4254501_the-mystery-case-no-1.txt +816101,The Ghost Case No. 1,"Author, Demo 1",horror,train,71455,17000,data/train/horror/816101_the-ghost-case-no-1.txt +4857601,The Romance Case No. 1,"Author, Demo 1",romance,train,72413,17000,data/train/romance/4857601_the-romance-case-no-1.txt +6814103,The Science Fiction Case No. 3,"Author, Demo 3",scifi,train,71410,17000,data/train/scifi/6814103_the-science-fiction-case-no-3.txt +5278303,The Adventure Case No. 3,"Author, Demo 3",adventure,train,71633,17000,data/train/adventure/5278303_the-adventure-case-no-3.txt +522202,The Detective Case No. 2,"Author, Demo 2",mystery,train,73002,17000,data/train/mystery/522202_the-detective-case-no-2.txt +4762502,The Horror Case No. 2,"Author, Demo 2",horror,train,71787,17000,data/train/horror/4762502_the-horror-case-no-2.txt +3541902,The Love Stories Case No. 2,"Author, Demo 2",romance,train,72436,17000,data/train/romance/3541902_the-love-stories-case-no-2.txt +6814104,The Science Fiction Case No. 4,"Author, Demo 4",scifi,train,71695,17000,data/train/scifi/6814104_the-science-fiction-case-no-4.txt +5278304,The Adventure Case No. 4,"Author, Demo 4",adventure,train,71970,17000,data/train/adventure/5278304_the-adventure-case-no-4.txt +4254502,The Mystery Case No. 2,"Author, Demo 2",mystery,train,72894,17000,data/train/mystery/4254502_the-mystery-case-no-2.txt +816102,The Ghost Case No. 2,"Author, Demo 2",horror,train,71596,17000,data/train/horror/816102_the-ghost-case-no-2.txt +4857602,The Romance Case No. 2,"Author, Demo 2",romance,train,72816,17000,data/train/romance/4857602_the-romance-case-no-2.txt +6814105,The Science Fiction Case No. 5,"Author, Demo 5",scifi,test,71571,17000,data/test/scifi/6814105_the-science-fiction-case-no-5.txt +5278305,The Adventure Case No. 5,"Author, Demo 5",adventure,test,71590,17000,data/test/adventure/5278305_the-adventure-case-no-5.txt +522203,The Detective Case No. 3,"Author, Demo 3",mystery,train,72720,17000,data/train/mystery/522203_the-detective-case-no-3.txt +4762503,The Horror Case No. 3,"Author, Demo 3",horror,train,71510,17000,data/train/horror/4762503_the-horror-case-no-3.txt +3541903,The Love Stories Case No. 3,"Author, Demo 3",romance,train,71804,17000,data/train/romance/3541903_the-love-stories-case-no-3.txt +6814106,The Science Fiction Case No. 6,"Author, Demo 6",scifi,train,71280,17000,data/train/scifi/6814106_the-science-fiction-case-no-6.txt +5278306,The Adventure Case No. 6,"Author, Demo 6",adventure,train,71461,17000,data/train/adventure/5278306_the-adventure-case-no-6.txt +4254503,The Mystery Case No. 3,"Author, Demo 3",mystery,train,73083,17000,data/train/mystery/4254503_the-mystery-case-no-3.txt +816103,The Ghost Case No. 3,"Author, Demo 3",horror,train,71610,17000,data/train/horror/816103_the-ghost-case-no-3.txt +4857603,The Romance Case No. 3,"Author, Demo 3",romance,train,72434,17000,data/train/romance/4857603_the-romance-case-no-3.txt +6814107,The Science Fiction Case No. 7,"Author, Demo 7",scifi,train,71365,17000,data/train/scifi/6814107_the-science-fiction-case-no-7.txt +5278307,The Adventure Case No. 7,"Author, Demo 7",adventure,train,71686,17000,data/train/adventure/5278307_the-adventure-case-no-7.txt +522204,The Detective Case No. 4,"Author, Demo 4",mystery,train,73036,17000,data/train/mystery/522204_the-detective-case-no-4.txt +4762504,The Horror Case No. 4,"Author, Demo 4",horror,train,71768,17000,data/train/horror/4762504_the-horror-case-no-4.txt +3541904,The Love Stories Case No. 4,"Author, Demo 4",romance,train,72517,17000,data/train/romance/3541904_the-love-stories-case-no-4.txt +6814108,The Science Fiction Case No. 8,"Author, Demo 8",scifi,test,71914,17000,data/test/scifi/6814108_the-science-fiction-case-no-8.txt +5278308,The Adventure Case No. 8,"Author, Demo 8",adventure,test,71899,17000,data/test/adventure/5278308_the-adventure-case-no-8.txt +4254504,The Mystery Case No. 4,"Author, Demo 4",mystery,train,72811,17000,data/train/mystery/4254504_the-mystery-case-no-4.txt +816104,The Ghost Case No. 4,"Author, Demo 4",horror,train,71499,17000,data/train/horror/816104_the-ghost-case-no-4.txt +4857604,The Romance Case No. 4,"Author, Demo 4",romance,train,72587,17000,data/train/romance/4857604_the-romance-case-no-4.txt +6814109,The Science Fiction Case No. 9,"Author, Demo 0",scifi,train,71575,17000,data/train/scifi/6814109_the-science-fiction-case-no-9.txt +5278309,The Adventure Case No. 9,"Author, Demo 0",adventure,train,71973,17000,data/train/adventure/5278309_the-adventure-case-no-9.txt +522205,The Detective Case No. 5,"Author, Demo 5",mystery,test,72788,17000,data/test/mystery/522205_the-detective-case-no-5.txt +4762505,The Horror Case No. 5,"Author, Demo 5",horror,test,71454,17000,data/test/horror/4762505_the-horror-case-no-5.txt +3541905,The Love Stories Case No. 5,"Author, Demo 5",romance,test,72350,17000,data/test/romance/3541905_the-love-stories-case-no-5.txt +6814110,The Science Fiction Case No. 10,"Author, Demo 1",scifi,train,71680,17000,data/train/scifi/6814110_the-science-fiction-case-no-10.txt +5278310,The Adventure Case No. 10,"Author, Demo 1",adventure,train,72041,17000,data/train/adventure/5278310_the-adventure-case-no-10.txt +4254505,The Mystery Case No. 5,"Author, Demo 5",mystery,test,73258,17000,data/test/mystery/4254505_the-mystery-case-no-5.txt +816105,The Ghost Case No. 5,"Author, Demo 5",horror,test,71537,17000,data/test/horror/816105_the-ghost-case-no-5.txt +4857605,The Romance Case No. 5,"Author, Demo 5",romance,test,72392,17000,data/test/romance/4857605_the-romance-case-no-5.txt +6814111,The Science Fiction Case No. 11,"Author, Demo 2",scifi,train,71377,17000,data/train/scifi/6814111_the-science-fiction-case-no-11.txt +5278311,The Adventure Case No. 11,"Author, Demo 2",adventure,train,71341,17000,data/train/adventure/5278311_the-adventure-case-no-11.txt diff --git a/src/main/java/de/nicode3141/genredct/Detector.java b/src/main/java/de/nicode3141/genredct/Detector.java new file mode 100644 index 0000000..c8e0586 --- /dev/null +++ b/src/main/java/de/nicode3141/genredct/Detector.java @@ -0,0 +1,46 @@ +package de.nicode3141.genredct; + +import java.util.Map; + +public class Detector { + + /** + * Calculates the sum of weighted word counts for a text + * @param inputText word count to the common vocabulary + * @param weights current weights for a label + * @param bias constant to adjust the perceptron + * @return sum of all weighted word counts + */ + double calcSum(Map inputText, double[] weights, double bias){ + // TODO: loop thru the map and add the (word count / total words) * weight to it + // After the loop, increment the sum by the bias + return 0.0d; + } + + /** + * Adjust the weights of a genre for correct result + * @param inputText word count of all texts to the common vocabulary + * @param label 0 if text isn't part of current genre, 1 if it is + * @return final weights for a genre + */ + double[] trainModel(Map[] inputText, int[] label){ + // TODO: Start with all weights = 0.0, bias = 0.0 + // Then loop thru multiple epochs: + // Loop thru all texts, calculate the sum + // If the sum is >= 0 -> means it belongs to the genre + // If the prediction was wrong, correct the weights + // (weights += learningRate * error * wordCount + return null; + } + + /** + * Predicts which genre the text belongs to + * @param inputText word count of the text to test to the common vocabulary + * @return array with value for each genre, contains the calculated sum + */ + double[] predictGenre(Map inputText){ + // TODO: Fixed size array for each genre + // For each genre calcSum + return null; + } +} diff --git a/src/main/java/de/nicode3141/genredct/GenreData.java b/src/main/java/de/nicode3141/genredct/GenreData.java new file mode 100644 index 0000000..bf69477 --- /dev/null +++ b/src/main/java/de/nicode3141/genredct/GenreData.java @@ -0,0 +1,33 @@ +package de.nicode3141.genredct; + +import java.util.List; +import java.util.Map; + +public class GenreData { + + /** + * This method gets all books and their corresponding label + * @param csvPath Path of the labels.csv file + * @return Map key: genre, value: path + */ + public static Map loadGenres(String csvPath){ + // TODO: Loop thru all paths and return a Map of + return null; + } + + /** + * Counts top X words of a textfile + * @param path source plain textfile + * @param limit only counts the top (limit) words + * @return LinkedMap key: word, value: count + */ + public static Map countWords(String path, int limit){ + // TODO: Scan text line by line and count already added Words. + // If word was not present, add it to the Map. + // After that, sort the Map. Simple with ArrayList sort() + // The ArrayList could contain the entrySet of the Map + // Then loop thru the sorted ArrayList, maximum (limit) and create a new Map + // LinkedHashMap would make sense here, because of the ordering + return null; + } +}