1
|
1 def removeStopWords(self,xo):
|
|
2 """remove stop words from xo"""
|
|
3 if not hasattr(self,'_v_stopWords'):
|
|
4 self._v_stopWords=self.stopwords_en.data.split("\n")
|
|
5
|
|
6 x=str(xo)
|
|
7
|
|
8 strx=x.split(" ")
|
|
9
|
|
10 for tmp in strx:
|
|
11
|
|
12 if tmp.lower() in self._v_stopWords:
|
|
13 del strx[strx.index(tmp)]
|
|
14
|
|
15 return " ".join(strx)
|
|
16
|
|
17
|
|
18 def getGetNeighbourhood(self,obj, wordStr, length=100,tagging=True):
|
|
19 """finde umgebung um die worte in wordStr, zurueckgegeben wird eine Array mit den Umgebungen von Fundstellen der Worte
|
|
20 alle Tags werden entfernt, die Fundstellen werden mit <span class="found">XX</span> getaggt, die Umgebungen werden
|
|
21 case insensitive gesucht
|
|
22 @param wordStr: string mit Worten getrennt durch Leerzeichen, Phrasen sind mit " gekennzeichnet
|
|
23 "eine phrase", "*" bezeichnet wildcards und wird ignoriert"
|
|
24 @param length: optional, default wert 100, 2*length ist die groesse der Umgebung
|
|
25 @param tagging: optional default wert true, kein span tag wird erzweugt falls tag=false
|
|
26 """
|
|
27
|
|
28 ret=[] # nimmt das Array auf, dass spaeter zurueckgegeben wird
|
|
29 ranges=[] #Array mit tupeln x,y wobei x die Position des Anfang und y des Endes der i-ten Umgebung angiebt
|
|
30
|
|
31 wordStr=wordStr.lstrip().rstrip()
|
|
32
|
|
33 def isInRanges(nr,length):
|
|
34 """test ob eine gegeben Position nr schon irgendwo in einer Umgebung ist, gibt den Index des ersten Wertes aus ranges zurueck,
|
|
35 -1, wenn kein Treffer
|
|
36
|
|
37 @param nr: Position die geprueft werden soll
|
|
38 @param length: Laenge des Wortes das geprueft werden soll
|
|
39 """
|
|
40 for x in ranges:
|
|
41 if (x[0]<=nr) and (nr < (x[1]-length)):
|
|
42 return ranges.index(x)
|
|
43 return -1
|
|
44
|
|
45 # deal with phrases, in Phrasen werden die Leerzeichen durch "_" ersetzt.
|
|
46 def rep_empty(str):
|
|
47 x= re.sub(" ","_",str.group(0))
|
|
48 return re.sub("\"","",x)
|
|
49
|
|
50 wordStr=re.sub("\".*?\"", rep_empty,wordStr)#ersetze leerzeichen in " " durch "_" und loesche "
|
|
51
|
|
52 #deal with wildcards, for our purposes it is enough to delete the wildcard
|
|
53 wordStr=wordStr.replace("*","")
|
|
54
|
|
55 words=wordStr.split(" ")
|
|
56 #if not words is ListType:
|
|
57 # words=[words]
|
|
58
|
|
59
|
|
60 txtCache = self.en.getHarvestCache();
|
|
61 txt= txtCache.get(obj.absolute_url(),None)
|
|
62
|
|
63 if txt==None:
|
|
64
|
|
65 logging.debug("NO CACHE for: "+obj.absolute_url())
|
|
66 txt=obj.harvest_page(mode="slim")
|
|
67
|
|
68
|
|
69 if not txt:
|
|
70 return ret
|
|
71
|
|
72 soup = BeautifulSoup(txt)
|
|
73
|
|
74 comments = soup.findAll(text=lambda text:isinstance(text, Comment))
|
|
75 [comment.extract() for comment in comments]
|
|
76
|
|
77 txt = ''.join(soup.findAll(text=True))
|
|
78
|
|
79
|
|
80 #txt=re.sub("<.*?>", "", txt) # loesche alle Tags
|
|
81 for word in words:
|
|
82 word=re.sub("_"," ",word) # ersetze zurueck "_" durch " "
|
|
83 pos=0
|
|
84
|
|
85 n=txt.lower().count(word.lower()) # wie oft tritt das Wort auf
|
|
86
|
|
87 for i in range(n):
|
|
88 pos=txt.lower().find(word.lower(),pos)
|
|
89
|
|
90 if pos > 0:
|
|
91 x=max(0,pos-length)
|
|
92 y=min(len(txt),pos+length)
|
|
93
|
|
94
|
|
95 #is word already in one of the results
|
|
96 nr=isInRanges(pos,len(word))
|
|
97 if nr >=0:# word ist in einer schon gefunden Umgebung, dann vergroessere diese
|
|
98 x=min(ranges[nr][0],x)
|
|
99 y=max(ranges[nr][1],y)
|
|
100
|
|
101 str=txt[x:y]
|
|
102 if x!=0: #add dots if in the middle of text
|
|
103 str="..."+str
|
|
104
|
|
105 if y!=len(txt): #add dots if in the middle of text
|
|
106 str=str+"..."
|
|
107
|
|
108
|
|
109
|
|
110 if nr >=0: # word ist in einer schon gefunden Umgebung
|
|
111 ranges[nr]=(x,y) # neue Position der Umgebung
|
|
112
|
|
113 ret[nr]=str # neue Umgebung
|
|
114 else: # andernfalls neue Umgebung hinzufuegen
|
|
115 ranges.append((x,y))
|
|
116
|
|
117 ret.append(str)
|
|
118
|
|
119 pos=pos+len(word)
|
|
120 else:
|
|
121 break;
|
|
122
|
|
123 # now highlight everything
|
|
124 if tagging:
|
|
125 for x in range(len(ret)):
|
|
126 for word in words:
|
|
127 repl=re.compile(word,re.IGNORECASE)
|
|
128 ret[x]=repl.sub(""" <span class="found">%s</span>"""%word.upper(),ret[x])
|
|
129
|
|
130 return ret
|
|
131 def copyAllImagesToMargin(self):
|
|
132 """tranformiere alle Bilder in die Margins"""
|
|
133 projects=self.getTree()
|
|
134 ret=""
|
|
135 for project in projects:
|
|
136 proj=project[3]
|
|
137 try:
|
|
138 persons=proj.copyImageToMargin();
|
|
139 except:
|
|
140 logging.error("Cannnot do: %s"%repr(project))
|
|
141
|
|
142 def transformProjectsToId(self):
|
|
143 """trnasformiere zu ID, Hilfsfunktion die die alten Templates analysiert und mit der neuen Liste
|
|
144 verantwortlicher Personen versieht"""
|
|
145 projects=self.getTree()
|
|
146 ret=""
|
|
147 for project in projects:
|
|
148
|
|
149 proj=project[3]
|
|
150 persons=proj.identifyNames(proj.getContent('xdata_01'))
|
|
151 if not hasattr(proj,'responsibleScientistsList'):
|
|
152 proj.responsibleScientistsList=[]
|
|
153
|
|
154 for person in persons.items():
|
|
155
|
|
156 if len(person[1]) >1: #nicht eindeutig
|
|
157 ret+="nicht eindeutig --- %s: %s\n"%(proj.getId(),person[0])
|
|
158
|
|
159 elif len(person[1]) ==0: #kein eintrage
|
|
160 ret+="kein eintrag--- %s: %s\n"%(proj.getId(),person[0])
|
|
161 proj.responsibleScientistsList.append((person[0],""))
|
|
162 else:
|
|
163 proj.responsibleScientistsList.append((person[0],person[1][0].getObject().getKey()))
|
|
164
|
|
165 return ret
|
|
166
|
|
167
|
|
168 def harvestProjects(self):
|
|
169 """harvest"""
|
|
170 folder="/tmp"
|
|
171 try:
|
|
172 os.mkdir("/tmp/harvest_MPIWG")
|
|
173 except:
|
|
174 pass
|
|
175 founds=self.ZopeFind(self.aq_parent.projects,obj_metatypes=['MPIWGProject'],search_sub=1)
|
|
176 for found in founds:
|
|
177 txt=found[1].harvest_page()
|
|
178
|
|
179 if txt and (txt != ""):
|
|
180 name=found[0].replace("/","_")
|
|
181 fh=file("/tmp/harvest_MPIWG/"+name,"w")
|
|
182 fh.write(txt)
|
|
183 fh.close()
|
|
184 |