OSDN Git Service

e8ae1a79f5391c5ef960baa6103921e492bc5a5c
[rec10/rec10-git.git] / rec10 / trunk / src / guess.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5
6 import os
7 import re
8 import time
9 import os
10 import datetime
11 import n_gram
12 import recdblist
13 import zenhan
14 import auto_move
15
16 def detName(title,path):
17     tt=detNameType(title, path)
18     if tt['type']=="C"or tt['type']=="D":
19         tt['num']=detNum(tt['title'],tt['folder'])
20 def detNum(title,movepath):
21     """
22         #番組の話数を推測する。
23     """
24     files=os.listdir(movepath)
25     ff=[]
26     maxnum=0
27     for file in files:
28         recdblist.printutf8(file)
29         file=os.path.join(movepath,file)
30         if os.path.isfile(file):
31             name=os.path.splitext(os.path.split(file)[1])
32             name=name[0]
33             name=zenhan.toHankaku(unicode(name))
34             p1=detNameType(name,movepath)
35             recdblist.printutf8(p1['title']+" "+str(p1['num']))
36             time1=time.localtime(os.path.getmtime(file))
37             time1=datetime.datetime.fromtimestamp(os.path.getmtime(file))
38             if p1['num']!=-1:
39                 ff.append([p1['num'],p1['title'],time1])
40                 if maxnum<p1['num']+1:
41                     maxnum=p1['num']+1
42     detMultiDeltaDays(ff)
43 def detMultiDeltaDays(num_with_title_with_dates):
44     maxnum=0
45     for ft in num_with_title_with_dates:
46         if maxnum<ft[0]+1:
47             maxnum=ft[0]+1
48     ff=num_with_title_with_dates
49     f3=[None]*maxnum
50     fret={}
51     for i in range(0, maxnum, 1):
52         f3[i]=[]
53     for f2 in ff:
54         f3[f2[0]].append(f2)
55     for i in range(maxnum):
56         for j in range(i+1,maxnum,1):
57             for ft3 in f3[i]:
58                 for ft4 in f3[j]:
59                     for gdd in detSingleDeltaDay(i, ft3[2], j, ft4[2]):
60                         #print gdd
61                         if fret.get(gdd[0])!=None:
62                             #print fret
63                             fret[gdd[0]]=fret[gdd[0]]+gdd[1]
64                         else:
65                             fret[gdd[0]]=gdd[1]
66     #print fret
67     maxk=0
68     maxp=0
69     for i in range(maxnum):
70         if fret.get(i)!=None:
71             if maxp<fret[i]:
72                 maxk=i
73                 maxp=fret[i]
74     #print maxk
75     #print maxp
76     return maxk
77 def detSingleDeltaDay(num1,date1,num2,date2):
78     dd=date1-date2
79     if date1<date2:
80         dd=date2-date1
81     dn=dd.days
82     d=num2-num1
83     if d<0:
84         d=-1*d
85     dp=dd+datetime.timedelta(hours=6)
86     dp=dp.days
87     dm=dd-datetime.timedelta(hours=6)
88     dm=dm.days
89     ret=[]
90     if dn%d*2>d:
91         dn=dn+d
92     if dm%d*2>d:
93         dm=dm+d
94     if dp%d*2>d:
95         dp=dp+d
96     recdblist.printutf8(str(d)+":"+str(dn))
97     if dp!=dn:
98         ret.append([dn/d,60])
99         ret.append([dp/d,40])
100     elif dm!=dn:
101         ret.append([dn/d,60])
102         ret.append([dm/d,40])
103     else:
104         ret.append([dn/d,100])
105     return ret
106 def detNameType(title,path):
107     """
108     type A ---title#<number>
109     type B ---title#<number>subtitle
110     type C ---title subtitle
111     type D ---title(without number)
112     type Aj ---title第<number>話
113     path --search reflexively
114     """
115     recdblist.printutf8(title)
116     #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
117     rA=re.compile("(.+)#(\d*)\s*\Z")
118     tA=rA.match(title)
119     rB=re.compile("(.+)#(\d*)\s*(\D*)")
120     tB=rB.match(title)
121     rAj=re.compile("(.+)第(\d*)話\s*\Z")
122     tAj=rAj.match(title)
123     ret={'title':"",'type':"",'num':0,'subtitle':"",'folder':""}
124     if tA:
125         recdblist.printutf8("typeA")
126         recdblist.printutf8("title="+tA.group(1))
127         recdblist.printutf8("num="+tA.group(2))
128         ret['type']="A"
129         ret['title']=tA.group(1)
130         ret['num']=int(tA.group(2))
131         ret['folder']=searchFolder(tA.group(1),path)
132     if tAj:
133         recdblist.printutf8("typeA")
134         recdblist.printutf8("title="+tAj.group(1))
135         recdblist.printutf8("num="+tAj.group(2))
136         ret['type']="Aj"
137         ret['title']=tAj.group(1)
138         ret['num']=int(tAj.group(2))
139         ret['folder']=searchFolder(tAj.group(1),path)
140     elif tB:
141         recdblist.printutf8("typeB")
142         recdblist.printutf8("title="+tB.group(1))
143         recdblist.printutf8("num="+tB.group(2))
144         recdblist.printutf8("subtitle="+tB.group(3))
145         ret['type']="B"
146         ret['title']=tB.group(1)
147         ret['num']=int(tB.group(2))
148         ret['folder']=searchFolder(tB.group(1),path)
149         ret['subtitle']=tB.group(3)
150     else:#type C or type D
151         #fold=searchFolder(title, path)
152         ts=title.split(" ")
153         tt=""
154         rt=["",0,""]
155         for t in ts:
156             tt=tt+" "+t
157             ft1=searchFolder(tt,path)
158             recdblist.printutf8(tt)
159             #print ft1
160             if ft1!="":
161                 #recdblist.printutf8(rt)
162                 #recdblist.printutf8(ft1[0]+" : "+str(ft1[1]))
163                 if ft1[1]>rt[1]:
164                     rt[0]=tt
165                     rt[1]=ft1[1]
166                     rt[2]=ft1[0]
167                     #recdblist.printutf8(rt)
168         #recdblist.printutf8("title="+rt[0][1:]+"/")
169         #recdblist.printutf8("subtitle = "+title.replace(rt[0][1:],"")[1:])
170         ret['title']=rt[0][1:]
171         ret['num']=-1
172         ret['folder']=rt[2]
173         ret['subtitle']=title.replace(rt[0][1:],"")[1:]
174         if ret['subtitle'].replace(" ","")=="":
175             ret['type']="D"
176         else:
177             ret['type']="C"
178     return ret
179 def searchFolder(title,path,threshold=500):
180     """
181     titleからフォルダーを探す
182     """
183     folderpath=os.listdir(path)
184     lfpath=[]
185     ngram=[]
186     for ft in folderpath:
187         fullpath=os.path.join(path, ft)
188         if os.path.isdir(fullpath):
189             lfpath.append(fullpath)
190             ftt=os.listdir(fullpath)
191             if len(ftt)>0:
192                 for ft2 in ftt:
193                     folderpath.append(os.path.join(fullpath, ft2))
194         else:
195             lfpath.append(fullpath)
196     for dirp in lfpath:
197         cmpp=""
198         appp=""
199         if os.path.isdir(dirp):
200             cmpp=os.path.dirname(dirp)
201             appp=dirp
202         else:
203             cmpp=os.path.basename(dirp)
204             appp=os.path.dirname(dirp)
205         ntitle=auto_move.getTitle(title)
206         ncmpp=auto_move.getTitle(cmpp)
207         p=n_gram.trigram(ntitle,ncmpp)
208         if p>0:
209             ngram.append((p,appp))
210     ngram=list(set(ngram))
211     ngram.sort()
212     ngram.reverse()
213     if len(ngram)>0:
214         #recdblist.printutf8(title + ngram[0][1] + " : "+str(ngram[0][0]))
215         if ngram[0][0]>threshold:
216             return ngram[0][1]
217         else:
218             return ""
219     else:
220         return ""