forked from baya/simpleNLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlp.py
executable file
·74 lines (63 loc) · 1.53 KB
/
nlp.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#author: rex
#blog: http://iregex.org
#filename x.py
#created: 2010-09-26 19:15
import re
import sys
regex=re.compile(r"(?x) (?: [\w-]+ | [\x80-\xff]{3} )")
def init_wordslist(fn="./words.txt"):
f=open(fn)
lines=sorted(f.readlines())
f.close()
return lines
def words_2_trie(wordslist):
d={}
for word in wordslist:
ref=d
chars=regex.findall(word)
for char in chars:
ref[char]=ref.has_key(char) and ref[char] or {}
ref=ref[char]
ref['']=1
return d
DEBUG=1
def search_in_trie(chars, trie):
print
while len(chars)>=1:
ref=trie
index=0
state=[]
for char in chars:
if ref.has_key(char):
if ref[char].has_key(""):
state.append(index)
ref=ref[char]
else:
break
index += 1
if not state or state[-1]==0:
index=1
print chars[0],"*",
chars=chars[1:]
else:
index=state[-1]+1
print ''.join(chars[:index]), "*",
chars=chars[index:]
def main():
#init
words=init_wordslist()
i=0
trie=words_2_trie(words)
#read content
fn= sys.argv[1]
lines=open(fn).readlines()
index=1
for line in lines:
chars=regex.findall(line)
search_in_trie(chars, trie)
index += 1
if __name__=='__main__':
main()