forked from Yunus0or1/Text-Classification-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNER.py
More file actions
73 lines (58 loc) · 2.62 KB
/
Copy pathNER.py
File metadata and controls
73 lines (58 loc) · 2.62 KB
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
import nltk
from nltk.tokenize import word_tokenize
sentence = "Huge Jam from Banani to Gulshan"
words = word_tokenize(sentence)
jam_place_integer = 0 # This will be needed for first and second phase
jam_place2_integer = 0 # This will be needed for third phase
jam_place3_integer = 0 # This will be needed for third phase because 'to' will have two places. The before and after there will be two place names.
enter_to_second_phase = 0
enter_to_third_phase = 0
enter_to_fourth_phase = 0
if 'near' in words or 'at' in words or 'of' in words:
for x in range(0, len(words)):
if words[x] == 'near' or words[x] == 'at' or words[x] == 'of':
jam_place_integer = x + 1
else:
enter_to_second_phase = 1
if enter_to_second_phase == 1:
if 'in' in words:
for x in range(0, len(words)):
if words[x] == 'in':
if (str(words[x + 1]) == 'traffic' or str(words[x + 1]) == 'jam'
or str(words[x + 1]) == 'grid' or str(
words[x + 1]) == 'lock'):
enter_to_third_phase = 1
else:
pos_tag_of_next_word = nltk.pos_tag(word_tokenize(words[x + 1]))
word, tag = zip(*pos_tag_of_next_word)
pos_tag_of_next_word_str = str(''.join(tag))
if pos_tag_of_next_word_str == 'NN' or pos_tag_of_next_word_str == 'NNP':
jam_place_integer = x + 1
else:
enter_to_third_phase = 1
if enter_to_third_phase == 1:
if 'to' in words:
for x in range(0, len(words)):
if words[x] == 'to':
jam_place2_integer = x + 1
# Now finding if previous word is a place also
pos_tag_of_previous_word = nltk.pos_tag(word_tokenize(words[x - 1]))
word, tag = zip(*pos_tag_of_previous_word)
pos_tag_of_previous_word_str = str(''.join(tag))
if pos_tag_of_previous_word_str == 'NN' or pos_tag_of_previous_word_str == 'NNP':
jam_place3_integer = x - 1
else:
enter_to_fourth_phase = 1
jam_place_final_result = ''
if jam_place_integer != 0:
jam_place = words[jam_place_integer]
jam_place_final_result = jam_place
if enter_to_third_phase == 1 and enter_to_fourth_phase == 0:
if jam_place3_integer ==0:
jam_place = words[jam_place2_integer]
jam_place_final_result = jam_place
if jam_place3_integer !=0:
jam_place2 = words[jam_place2_integer]
jam_place3 = words[jam_place3_integer]
jam_place_final_result = jam_place3 + ' to ' + jam_place2
print(jam_place_final_result)