[Python] compute the word coverage for the given fileid associated with text corpus inagurual...

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I Have tried the below code.#!/bin/python3

    import math
    import os
    import random
    import re
    import sys
    import zipfile
    import nltk


    #
    # Complete the 'accessTextCorpora' function below.
    #
    # The function accepts following parameters:
    # 1. STRING fileid
    # 2. STRING word
    #
    from nltk.corpus import inaugural
    def accessTextCorpora(fileid, word):
    for fileid in inaugural.fileids():
    total_words = len(inaugural.words(fileid))
    total_unique_words = len(set(inaugural.words(fileid)))
    wordcoverage = int(total_words / total_unique_words)
    ed_words = [word for word in set(inaugural.words(fileid)) if word.endswith('ed')]
    textfreq2 = [word.lower() for word in inaugural.words(fileid) if word.isalpha()]
    textfreq = nltk.FreqDist(textfreq2)
    wordfreq = textfreq[word]
    return wordcoverage, ed_words, wordfreq


    It is passing only one test case. Remaining all test cases showing the same result. Could you please suggest me what i need to change.

    These are the outputs.

    TestCase 0:

    Input (stdin)
    Run as Custom Input
    |
    Download
    1789-Washington.txt
    government


    Your Output (stdout) 2 ['United', 'accomplished', 'addressed', 'advanced', 'affected', 'allotted', 'assembled', 'awakened', 'called', 'committed', 'compared', 'considered', 'consulted', 'contemplated', 'delegated', 'departed', 'derived', 'distinguished', 'employed', 'enlarged', 'entrusted', 'established', 'exemplified', 'expected', 'experienced', 'filled', 'forced', 'fortified', 'guided', 'honored', 'imparted', 'included', 'instituted', 'judged', 'limited', 'ordained', 'originated', 'palliated', 'persuaded', 'placed', 'pleased', 'produced', 'promoted', 'received', 'rendered', 'repaired', 'required', 'resulted', 'sacred', 'selected', 'staked', 'submitted', 'summoned', 'suppressed', 'swayed', 'transmitted', 'united', 'unparalleled', 'unpracticed', 'untried', 'urged'] 8

    Expected Output Download 2 ['United', 'accomplished', 'addressed', 'advanced', 'affected', 'allotted', 'assembled', 'awakened', 'called', 'committed', 'compared', 'considered', 'consulted', 'contemplated', 'delegated', 'departed', 'derived', 'distinguished', 'employed', 'enlarged', 'entrusted', 'established', 'exemplified', 'expected', 'experienced', 'filled', 'forced', 'fortified', 'guided', 'honored', 'imparted', 'included', 'instituted', 'judged', 'limited', 'ordained', 'originated', 'palliated', 'persuaded', 'placed', 'pleased', 'produced', 'promoted', 'received', 'rendered', 'repaired', 'required', 'resulted', 'sacred', 'selected', 'staked', 'submitted', 'summoned', 'suppressed', 'swayed', 'transmitted', 'united', 'unparalleled', 'unpracticed', 'untried', 'urged'] 8

    TestCase1:

    Input (stdin) Run as Custom Input |

    1793-Washington.txt i

    Your Output (stdout) 2 ['United', 'accomplished', 'addressed', 'advanced', 'affected', 'allotted', 'assembled', 'awakened', 'called', 'committed', 'compared', 'considered', 'consulted', 'contemplated', 'delegated', 'departed', 'derived', 'distinguished', 'employed', 'enlarged', 'entrusted', 'established', 'exemplified', 'expected', 'experienced', 'filled', 'forced', 'fortified', 'guided', 'honored', 'imparted', 'included', 'instituted', 'judged', 'limited', 'ordained', 'originated', 'palliated', 'persuaded', 'placed', 'pleased', 'produced', 'promoted', 'received', 'rendered', 'repaired', 'required', 'resulted', 'sacred', 'selected', 'staked', 'submitted', 'summoned', 'suppressed', 'swayed', 'transmitted', 'united', 'unparalleled', 'unpracticed', 'untried', 'urged'] 23

    Expected Output

    1 ['called', 'distinguished', 'reposed', 'united', 'violated'] 6

    Continue reading...

Compartilhe esta Página