-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlingText.py
161 lines (113 loc) · 4.61 KB
/
lingText.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re
#,sys,copy
from UserString import UserString
#from string import punctuation
verbose = False
#verbose = True
class LingText(UserString):
def __init__(self, seq="") :
UserString.__init__(self, seq.decode("utf-8"))
#self.spaces = spaces
self.length = len(self)
self.wordSet = []
#if self.length<2:return
#self.makeParagText()
#print "<br>$$$",self.data
# the text-dictionary giving for a position (0.12309...) the corresponding paragraph:
self.parFracText=self._getParFracText()
self.parPos = sorted(self.parFracText.iterkeys())
#print self.parPos
#for k in self.parPos:
#print "<br>***",k,": ",self.parFracText[k],"<br>"
#print ")))))))))))))))))))init LIngtext fini<br><br><br>"
self.makeWordSet()
def makeWordSet(self,data=None):
if data == None : da=self.data
#da.decode('utf-8')
#da = unicode(da, "UTF-8")
expression = re.compile(ur'\W+', re.UNICODE+re.M+re.IGNORECASE)
wset = set(expression.split(da) )
wset.discard(u'')
#wlist = re.split(r"\w+", da, re.UNICODE+re.M+re.IGNORECASE)
#wlist = [item.encode('utf-8') for item in wlist]
#wset = set(wlist)
if data == None : self.wordSet=wset
return wset
def getText(self,pos):
"""
get paragraph text for a position
"""
if pos in self.parPos: return self.parFracText[pos].encode( "utf-8" )
else: return ""
def getWholeText(self):
return self.data.encode( "utf-8" )
def getParForPos(self,pos): #get paragraph containing position (fraction)
low = 0.0
for p in self.parPos:
if pos<p:return low
low = p
return p
def replaceWord(self,oldWord,beforeNewWord="",afterNewWord="",text=None,newWord=r"\2"):
"""
example:
replaceWord("le",beforeNewWord="before:",afterNewWord=":after")
"""
dat = text.decode("utf-8")
if text == None : dat=self.data
strOldWord = r"([\W])("+re.escape(oldWord)+r")(\W)"
expOldWord = re.compile(strOldWord, re.UNICODE+re.M+re.IGNORECASE)
expNewWord = r"\1"+beforeNewWord+newWord+afterNewWord+r"\3"
#print expOldWord.pattern
#print expNewWord
datNew = (expOldWord.sub(expNewWord," "+dat+" ")[1:-1]).encode( "utf-8" )
if text == None : self.data=datNew
return datNew
def markWords(self,text,wordlist,beforeMark,afterMark):
for w in wordlist:
text = self.replaceWord(w,beforeMark,afterMark,text)
return text
def getWordIndeces(self,word):
strWord = "([\W])("+re.escape(word)+")(\W)"
expWord = re.compile(strWord, re.UNICODE+re.M+re.IGNORECASE)
return [match.start() for match in expWord.finditer(" "+self.data+" ")]
def getWordFreq(self,word):
return len(self.getWordIndeces(word))/float(self.length)
def getWordPositions(self,word):
return [float(index)/self.length for index in self.getWordIndeces(word)]
def _getParagIndeces(self,newline="\n"):
expWord = re.compile(newline, re.UNICODE+re.M+re.IGNORECASE)
return [0]+[match.start()+1 for match in expWord.finditer(self.data[:-1])]
def _getParFracText(self,newline="\n"):
return dict(zip(self._getParagFractions(),re.split(newline,self.data[:-1])))
def _getParagFractions(self,newline="\n"):
return [float(i)/self.length for i in self._getParagIndeces(newline)]
if __name__ == "__main__":
#verbose = True
#if verbose: print "test:"
lingText = LingText(seq="""
le ..leùù** lele le$ leé Scepticisme ou occultisme ?
Le complot du 11-Septembre n’aura pas lieu
L’idée que les attentats du 11-Septembre auraient été manigancés par la Maison Blanche a fait son chemin. Or, réplique Alexander Cockburn, figure marquante de la gauche radicale aux Etats-Unis, une telle croyance témoigne, paradoxalement, d’une forme d’hébétement devant la puissance américaine, alors même que celle-ci échoue dans des entreprises bien moins herculéennes que l’éventuelle réalisation (puis la dissimulation) d’un tel complot.le""".decode("utf-8"))
print lingText.data
s = lingText.makeWordSet()
for w in s:
print w
#print lingText.getWordIndeces("\n")
print "\n"
oldWord = uword = r"pr".decode('utf-8')
beforeNewWord = " before:"
afterNewWord=":after "
newWord=r"\2"
dat = """président pr prù pr* pr= président pr+àà """.decode('utf-8')
#strOldWord = r"(\W)("+re.escape(oldWord)+r")(\W)"
dat = lingText.data
strOldWord = r"\W+"
expOldWord = re.compile(strOldWord, re.UNICODE+re.M+re.IGNORECASE)
#expNewWord = r"\1"+beforeNewWord+newWord+afterNewWord #+r"\3"
#expNewWord = beforeNewWord+r"XXX"+afterNewWord #+r"\3"
expNewWord = r"XXX"#+r"\3"
#print expOldWord.pattern
#print expNewWord
datNew = expOldWord.sub(expNewWord," "+dat+" ")[1:-1]