OSDN Git Service

implement guess function(alpha).
[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 Yukikaze
5
6 import os
7 import re
8 import time
9 import os
10 import datetime
11 import n_gram
12
13 def detName(title,path):
14     tt=detNameType(title, path)
15     if tt['type']=="C"or tt['type']=="D":
16         tt['num']=detNum(tt['title'],tt['folder'])
17 def detNum(title,movepath):
18     """
19         #
20     """
21     files=os.listdir(movepath)
22     ff=[]
23     maxnum=0
24     for file in files:
25         if os.path.isfile(file):
26             name=os.path.splitext(os.path.split(file)[1])
27             p1=detNameType(name,movepath)
28             print p1['title']+" "+str(p1['num'])
29             time1=time.localtime(os.path.getmtime(file))
30             ff.append([p1['num'],p1['title'],time1])
31             if maxnum<p1['num']+1:
32                 maxnum=p1['num']+1
33     f3=[maxnum]
34     for i in range(0, maxnum, 1):
35         f3[i]=[]
36     for f2 in ff:
37         f3[f2[0]].append(f2)
38     for i in range(maxnum):
39         for j in range(i+1,maxnum,1):
40
41 def guessDeltaDay(num1,date1,num2,date2):
42     dd=date1-date2
43     if date1<date2:
44         dd=date2-date1
45     d1=dd.days
46     dd=dd+datetime.timedelta(hours=6)
47     if dd!=d1:
48         
49 def detNameType(title,path):
50     """
51     type A ---title#<number>
52     type B ---title#<number>subtitle
53     type C ---title subtitle
54     type D ---title(without number)
55     path --search reflexively
56     """
57     #print title
58     #rA=re.compile(".+(?P<title>)#\d(?P<num>)\s[0,10]\z")
59     rA=re.compile("(.+)#(\d*)\s*\Z")
60     tA=rA.match(title)
61     rB=re.compile("(.+)#(\d*)\s*(\D*)")
62     tB=rB.match(title)
63     ret={'title':"",'type':"",'num':0,'subtitle':"",'folder':""}
64     if tA:
65         print "typeA"
66         print "title="+tA.group(1)
67         print "num="+tA.group(2)
68         ret['type']="A"
69         ret['title']=tA.group(1)
70         ret['num']=int(tA.group(2))
71         ret['folder']=searchFolder(tA.group(1),path)
72     elif tB:
73         print "typeB"
74         print "title="+tB.group(1)
75         print "num="+tB.group(2)
76         print "subtitle="+tB.group(3)
77         ret['type']="B"
78         ret['title']=tB.group(1)
79         ret['num']=int(tB.group(2))
80         ret['folder']=searchFolder(tB.group(1),path)
81         ret['subtitle']=tB.group(3)
82     else:#type C or type D
83         #fold=searchFolder(title, path)
84         ts=title.split(" ")
85         tt=""
86         rt=["",0,""]
87         for t in ts:
88             tt=tt+" "+t
89             ft1=searchFolder(tt,path)
90             print tt
91             print ft1
92             if ft1!="":
93                 #print rt
94                 #print ft1[0]+" : "+str(ft1[1])
95                 if ft1[1]>rt[1]:
96                     rt[0]=tt
97                     rt[1]=ft1[1]
98                     rt[2]=ft1[0]
99                     #print rt
100         #print "title="+rt[0][1:]+"/"
101         #print "subtitle = "+title.replace(rt[0][1:],"")[1:]
102         ret['title']=rt[0][1:]
103         ret['num']=""
104         ret['folder']=rt[2]
105         ret['subtitle']=title.replace(rt[0][1:],"")[1:]
106         if ret['subtitle'].replace(" ","")=="":
107             ret['type']="D"
108         else:
109             ret['type']="C"
110     return ret
111 def searchFolder(title,path):
112     """
113     titleからフォルダーを探す
114     """
115     folderpath=os.listdir(path)
116     lfpath=[]
117     ngram=[]
118     for ft in folderpath:
119         fullpath=os.path.join(path, ft)
120         if os.path.isdir(fullpath):
121             lfpath.append(fullpath)
122             ftt=os.listdir(fullpath)
123             if len(ftt)>0:
124                 for ft2 in ftt:
125                     folderpath.append(os.path.join(fullpath, ft2))
126         else:
127             lfpath.append(fullpath)
128     for dirp in lfpath:
129         cmpp=""
130         appp=""
131         if os.path.isdir(dirp):
132             cmpp=os.path.dirname(dirp)
133             appp=dirp
134         else:
135             cmpp=os.path.basename(dirp)
136             appp=os.path.dirname(dirp)
137         p=n_gram.trigram(title.decode("utf-8"),cmpp.decode("utf-8"))
138         if p>0:
139             ngram.append((p,appp))
140     ngram=list(set(ngram))
141     ngram.sort()
142     ngram.reverse()
143     if len(ngram)>0:
144         #print title + ngram[0][1] + " : "+str(ngram[0][0])
145         if ngram[0][0]>300:
146             return [ngram[0][1],ngram[0][0]]
147         else:
148             return ""
149     else:
150         return ""