{"id":18523194,"url":"https://github.com/iglee/hmms-and-pcfg","last_synced_at":"2025-07-25T02:38:55.863Z","repository":{"id":90698311,"uuid":"238833382","full_name":"iglee/HMMs-and-PCFG","owner":"iglee","description":"POS tagging by using ngram based hidden markov models.","archived":false,"fork":false,"pushed_at":"2020-02-17T18:57:55.000Z","size":36,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-14T18:54:18.773Z","etag":null,"topics":["bigrams","hmm","hmm-viterbi-algorithm","ngrams","nlp","pos","trigrams"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iglee.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-02-07T03:09:38.000Z","updated_at":"2020-02-17T18:57:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"925fd697-bd57-4582-853c-51a53328695d","html_url":"https://github.com/iglee/HMMs-and-PCFG","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/iglee/HMMs-and-PCFG","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iglee%2FHMMs-and-PCFG","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iglee%2FHMMs-and-PCFG/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iglee%2FHMMs-and-PCFG/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iglee%2FHMMs-and-PCFG/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iglee","download_url":"https://codeload.github.com/iglee/HMMs-and-PCFG/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iglee%2FHMMs-and-PCFG/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266945063,"owners_count":24010492,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bigrams","hmm","hmm-viterbi-algorithm","ngrams","nlp","pos","trigrams"],"created_at":"2024-11-06T17:34:32.139Z","updated_at":"2025-07-25T02:38:55.812Z","avatar_url":"https://github.com/iglee.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## HMMs\n### Deliverables - \n1. Bigram HMMs\n2. Trigram HMMs\n3. Evaluations\n\n### Assumptions Made\n1. Tokens with less than 1 count is converted to \\\u003cUNK\\\u003e.  The POS tag for those tokens was kept the same.\n2. Smoothing- trying out k-smoothing and linear interpolation\n3. emojis, urls, hashtags, mentions, numbers, dates, etc. were converted to a special token.\n  \n### How to run this\nFirst, we need to load the corpus and tokenize.  This can be done using `TextProcess.py`\n```\nFrom TextProcess import ProcessedCorpus\ncorpus = ProcessedCorpus(\"CSE517_HW_HMM_Data/twt.bonus.json\",\\\n                         \"CSE517_HW_HMM_Data/twt.dev.json\",\\\n                         \"CSE517_HW_HMM_Data/twt.test.json\",1)\n```\n\nThen the processed corpus is loaded to a language model using `LanguageModel.py`\n```\nFrom LanguageModel import LanguageModel\nlm = LanguageModel(corpus)\n```\n\n`LanguageModel` class contains unigram, bigram, and trigram probabilities and count dictionaries for HMMs to use.  The smoothing options available are `add-k` and linear interpolation.  The smoothing parameters are updated and the necessary calculation updates to probabilities can be done as below:\n\n```\nlm.update(0.3,(0.00001,0.99999),(0.001,0.001,0.998))\n```\nThe Bigram and Trigram HMMs are implemented in `BigramHMM.py` and `TrigramHMM.py`, and these can run as follows:\n```\nbhmm = BigramHMM(corpus, lm)\npis, preds = bhmm.test_viterbi(bhmm.dev, bhmm.dev_emission_probabilities)\naccuracy, y_true, y_pred, confusion_matrix_array, normalized= bhmm.analyze_results(bhmm.dev, preds)\n```\n```\nthmm = TrigramHMM(corpus, lm)\npis, preds = thmm.test_trigram_viterbi(thmm.dev, thmm.dev_emission_probabilities)\naccuracy, y_true, y_pred, confusion_matrix_array, normalized= thmm.analyze_results(thmm.dev, preds)\n```\n\nThe confusion matrix can be plotted from above output, `normalized` and `confusion_matrix_array`.\n```\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sn\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\n\ndf_cm = pd.DataFrame(normalized, index = list(bhmm.tags),\n                  columns = list(bhmm.tags))\n\nplt.figure(figsize = (40,40))\n\nax = sn.heatmap(df_cm, annot=True, cbar=False)\nbottom, top = ax.get_ylim()\nax.set_ylim(bottom + 0.5, top - 0.5)\nsn.set(font_scale=1.9)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figlee%2Fhmms-and-pcfg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Figlee%2Fhmms-and-pcfg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Figlee%2Fhmms-and-pcfg/lists"}