简体   繁体   中英

write function which takes input parameters sentence and n (type integer), and returns a list that contains N-gram generated from the given sentence

write a function construct_ngrams(sentence, n) which takes input parameters sentence (type string) and n (type integer), and returns a list that contains N-gram generated from the given sentence. If no such N-gram could be generated (think about the cases), then it simply returns an empty list.

I have this so far

def construct_ngrams(sentence, n):
    """Returns a list that counts N-gram generated from the given sentence"""
    words = sentence.split()
    
    if n == 0 or n > len(words) -1:
        return []
    ngram = []
    
    for i in range(n):
       ngram.append(words[i:i+n])
    
   return ngram

however this does not pass the following test:

ngrams = construct_ngrams('this is another long sentence for testing', 6)
print(ngrams)

it gives: [['this', 'is', 'another', 'long', 'sentence', 'for'], ['is', 'another', 'long', 'sentence', 'for', 'testing'], ['another', 'long', 'sentence', 'for', 'testing'], ['long', 'sentence', 'for', 'testing'], ['sentence', 'for', 'testing'], ['for', 'testing']]

rather than: [['this', 'is', 'another', 'long', 'sentence', 'for'], ['is', 'another', 'long', 'sentence', 'for', 'testing']]

any one be able to help me fix this?

I found several mistakes in your code, first "return" can be only used at the end of a function. You can put ngram=[] outside the function instead of in the if statement. Here is the revised code, hope it can help you.

def construct_ngrams(sentence, n):
    words = sentence.split()
    ngram=[]
    if n == 0 or n > len(words) -1:
        pass
    else:
        for i in range(len(words)-n+1):
            ngram.append(words[i:i+n])
    return ngram

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM