python英文题03

本文提供了一系列Python函数的实现,包括读取文件内容,分割标签和单词,创建标签字典,获取唯一排序单词列表,查找词与标签的对应关系,生成标签频率统计以及打印词组组合。这些函数涉及文本处理和数据整理的核心技术。
摘要由CSDN通过智能技术生成

01.Write a function named read_content(filename) which takes a filename as a parameter and reads the contents of the input text file. The function should return a list of non-empty strings. You can assume that the input file is not an empty file and the content of the file may contain one or more new line characters.

def read_content(x):
    with open (x,'r') as txt:
        content=txt.read()
        lstout=content.split(sep='\n')
    return lstout

02.Write a function named get_tag_words(line) which takes a string as a parameter and separates the tag and a group of words. The tag and the group of words are separated by a ":" in the parameter string and the group of words are separated by white-space. The function should return the tag and a list of words as a tuple. Note: the returned list of words must be in ascending order. Note: you can assume that there is only one single ':' in the parameter string.

def get_tag_words(line):
    lst_pre=line.split(sep=':')
    word=lst_pre[0]
    lst_out=lst_pre[1].split(sep=' ')
    lst_out.sort()
    return (word,lst_out)

03.Write a function named create_tags_dictionary(filename) which takes a filename as a parameter and creates a tags dictionary using the content from the text file. You must call the functions defined in the previous two questions so please copy those two functions into the answer box. You can assume that each tag in the file will be unique.

def read_content(x):
    with open (x,'r') as txt:
        content=txt.read()
        lstout=content.split(sep='\n')
    return lstout

#quest2
def get_tag_words(line):
    lst_pre=line.split(sep=':')
    word=lst_pre[0]
    lst_out=lst_pre[1].split(sep=' ')
    lst_out.sort()
    return (word,lst_out)

#quest3part
def create_tags_dictionary(v):
    lst_mid=read_content(v)
    dictout={}
    for j in lst_mid:
        kv=get_tag_words(j)
        dictout[kv[0]]=kv[1]
    return dictout

04.Write a function named get_sorted_unique_words_list(sentence) which takes a string as a parameter and returns a sorted and unique lowercase words list.

def get_sorted_unique_words_list(a):
    b=a.lower()
    lst_pre=b.split(sep=' ')
    lst_out=[]
    for i in lst_pre:
        if i not in lst_out:
            lst_out.append(i)
    lst_out.sort()
    return lst_out

05.Write a function named get_word_tag_tuple(tags_dictionary, search_word) which takes a tags dictionary and a lowercase word as parameters. The key of the dictionary is a tag and the value is a list of words belonging to that tag. 

e.g. 'NN': ['dreamer', 'father', 'fun', 'grass', 'mother', 'odense', 'rain', 'shoemaker', 'spring', 'summer', 'tortoise', 'toy', 'washerwoman']

The function should return a word-tag tuple. The tuple should contain the search_word and the corresponding tag, e.g ('father', 'NN'). Note: You can assume that the search_word will be in one (and only one) of the dictionary items.

def get_word_tag_tuple(a,b):
    lst=a.items()
    for i in lst:
        if b in i[1]:
            return (b,i[0])

06.Write a function named get_tag_tuple_list(tags_dictionary, sentence) which takes a tags dictionary and a string as parameters. The key of the dictionary is a tag and the value is a list of words belonging to that tag. The function should return a sorted list of word-tag tuples. Each tuple should contain a single word and the corresponding tag, e.g ('summer', 'NN'). You must call the functions defined in the previous two questions so please copy those two functions into the answer box. 

def get_sorted_unique_words_list(a):
    b=a.lower()
    lst_pre=b.split(sep=' ')
    lst_out=[]
    for i in lst_pre:
        if i not in lst_out:
            lst_out.append(i)
    lst_out.sort()
    return lst_out


#quest5
def get_word_tag_tuple(a,b):
    lst=a.items()
    for i in lst:
        if b in i[1]:
            return (b,i[0])

'''dict={'NN': ['dreamer', 'father', 'fun', 'grass', 'mother', 'odense', 'rain', 'shoemaker', 'spring', 'summer', 'tortoise', 'toy', 'washerwoman']}
print(get_word_tag_tuple(dict,'father'))'''

def get_tag_tuple_list(m,n):
    prepar=get_sorted_unique_words_list(n)
    lst_fina_out=[]
    for j in prepar:
        if get_word_tag_tuple(m,j)!=None:
            lst_fina_out.append(get_word_tag_tuple(m,j))


    return lst_fina_out
 

07.Write a function named get_tags_frequency(list_of_tuples) which takes a list of word-tag tuples. e.g.[('summer', 'NN'), ('is', 'VBZ'), ('over', 'IN')] as a parameter. The function returns a dictionary containing a frequency count of each tag in the given list. Note: you can assume that the parameter list_of_tuples is not an empty list.

def get_tags_frequency(a):
    lstall=[]
    dictout={}
    for i in a:
        if i[1] not in lstall:
            lstall.append(i[1])
    for j in lstall:
        times=0
        for k in a:
            if j in k:
                times+=1
        dictout[j]=times
    return dictout

08.Write a function named print_dictionary(tags_dictionary) which takes a tags dictionary as a parameter and prints the content of the dictionary. The function should print the keys in sorted alphabetical order. You can assume that the dictionary is not empty.

def print_dictionary(a):
    kys=a.keys()
    lst=[]
    for i in kys:
        lst.append(i)
    lst.sort()
    for i in lst:
        print(i,a.get(i))

09.Write a function named print_all_phrases(tags_dictionary) which takes a tags dictionary as a parameter and generates and prints ALL possible combinations of phrases using the content of the dictionary. Note: We only use three tags  ("DT", "JJ" and "NN") in the dictionary.

We can generate a meaningful phrase if we start a phrase with a word which is classified as an article (i.e. a word with a 'DT' tag) followed by an adjective (i.e. a word with a 'JJ' tag) and ends with a noun (i.e. a word with an 'NN' tag). For example: if the tags dictionary contains the following tag-words_list pairs:

def print_all_phrases(tags_dictionary):
    for i in tags_dictionary['DT']:          
        for j in tags_dictionary['JJ']:              
            for k in tags_dictionary['NN']:          
                print(i,j,k)

10.Write a function named print_random_phrase(tags_dictionary) which takes a tags dictionary as a parameter and prints a random phrase using the content of the dictionary. Note: we only use three tags ("DT", "JJ" and "NN") in the dictionary.

We can generate a meaningful phrase if we start a phrase with a word which is classified as an article (i.e. a word with a 'DT' tag) followed by an adjective (i.e. a word with a 'JJ' tag) and ends with a noun (i.e. a word with an 'NN' tag). For example: if the tags dictionary contains the following tag-words_list pairs:

def print_random_phrase(tags):
    import random
    fst=random.choice(tags['DT'])
    sec=random.choice(tags['JJ'])
    trd=random.choice(tags['NN'])
    return print(fst+' '+sec+' '+trd)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值