3阶马尔可夫链 自然语言处理python

一、简介:

      把每三个三个单词作为一个整体进行训练。

       举一个例子:

       input:   

      my dream is that I can be an engineer, so I design more applications for people to use.

      my dream is that I can be a bird, so I can fly to everywhere I want.

      it is also my dream that I can be a house, so I can warm you in the cold winter.

生成的马尔可夫链:

    

{'START': ['my dream is'], 'my dream is': ['that i can'], 'dream is that': ['i can be'], 'is that i': ['can be a'], 'that i can': ['be a house,'], 'i can be': ['a house, so'], 'can be an': ['engineer, so i'], 'be an engineer,': ['so i design'], 'an engineer, so': ['i design more'], 'engineer, so i': ['design more applications'], 'so i design': ['more applications for'], 'i design more': ['applications for people'], 'design more applications': ['for people to'], 'more applications for': ['people to use.\nmy'], 'applications for people': ['to use.\nmy dream'], 'for people to': ['use.\nmy dream is'], 'people to use.\nmy': ['dream is that'], 'to use.\nmy dream': ['is that i'], 'use.\nmy dream is': ['that i can'], 'can be a': ['house, so i'], 'be a bird,': ['so i can'], 'a bird, so': ['i can fly'], 'bird, so i': ['can fly to'], 'so i can': ['warm you in'], 'i can fly': ['to everywhere i'], 'can fly to': ['everywhere i want.\nit'], 'fly to everywhere': ['i want.\nit is'], 'to everywhere i': ['want.\nit is also'], 'everywhere i want.\nit': ['is also my'], 'i want.\nit is': ['also my dream'], 'want.\nit is also': ['my dream that'], 'is also my': ['dream that i'], 'also my dream': ['that i can'], 'my dream that': ['i can be'], 'dream that i': ['can be a'], 'be a house,': ['so i can'], 'a house, so': ['i can warm'], 'house, so i': ['can warm you'], 'i can warm': ['you in the'], 'can warm you': ['in the cold'], 'warm you in': ['the cold winter.'], 'you in the': ['cold winter.'], 'in the cold': ['winter.'], 'END': ['the cold winter.', 'winter.', 'cold winter.']}

生成的文本:

     my dream is that i can be a house, so i can warm you in the cold winter. 

代码:

 1 # I sperate the input.txt with space, and use dictionary to store the next three words after the current 3 words.
 2 # in the same time, store the first three word as the beginning, and the last three or two or one words as the end
 3 # how to generate output.txt: form the start, start to look for the next three words in ramdom, once meets the end, the geration is end.
 4 import random
 5 
 6 fhand=open("E:\\a2.txt",'r',encoding='UTF-8')
 7 dataset_file=fhand.read()
 8 
 9 
10 # dataset_file='my friend makes the best raspberry pies'
11 dataset_file=dataset_file.lower().split(' ')
12 model={}
13 
14 for i, word in enumerate(dataset_file):
15     if i == len(dataset_file) - 3:
16         model['END'] = model.get('END', []) + [dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]]
17         model['END'] = model.get('END', []) + [dataset_file[i + 2]]
18         model['END'] = model.get('END', []) + [dataset_file[i + 1] +" "+dataset_file[i + 2]]
19     elif i == 0:
20         model['START'] = model.get('START', []) + [dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]]
21         # model['START']=model.get('START',[])+[dataset_file[i]]
22         # model['START']=model.get('START',[])+[dataset_file[i]+" "+dataset_file[i+1]]
23         model[dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]] = model.get(word, []) + [
24             dataset_file[i + 3] + " " + dataset_file[i + 4] + " " + dataset_file[i + 5]]
25     elif i <= (len(dataset_file) - 6):
26         model[dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]] = model.get(word, []) + [
27             dataset_file[i + 3] + " " + dataset_file[i + 4] + " " + dataset_file[i + 5]]
28     elif i == (len(dataset_file) - 5):
29         model[dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]] = model.get(word, []) + [
30             dataset_file[i + 3] + " " + dataset_file[i + 4]]
31     elif i == (len(dataset_file) - 4):
32         model[dataset_file[i] + " " + dataset_file[i + 1] + " " + dataset_file[i + 2]] = model.get(word, []) + [
33             dataset_file[i + 3]]
34 print(model)
35 
36 generated = []
37 while True:
38     if not generated:
39         words = model['START']
40     elif generated[-1] in model['END']:
41         break
42     else:
43         words = model[generated[-1]]
44     generated.append(random.choice(words))
45 
46 fhand=open("E:\output.txt",'a')
47 for word in generated:
48     fhand.write(word+" ")
49 
50     print(word,end=' ')

猜你喜欢

转载自www.cnblogs.com/cysisu/p/10776062.html