-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVisualization.py
More file actions
82 lines (65 loc) · 2.32 KB
/
Copy pathVisualization.py
File metadata and controls
82 lines (65 loc) · 2.32 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
74
75
76
77
78
79
import re
import string
import nltk
from nltk.tokenize import word_tokenize
import nltk
import os
import itertools
from nltk.stem import WordNetLemmatizer
from nltk.stem.porter import PorterStemmer
wn = nltk.WordNetLemmatizer()
ps = PorterStemmer()
lemmatized_words = []
stemmed_words = []
my_list = []
new_dict = {}
modified_words = []
text = []
tokens = []
allWords = []
duplicates_free = []
tokenizer = nltk.RegexpTokenizer(r"\w+")
curdir = os.getcwd()
textdir = os.path.join(os.getcwd(), "texts")
for filename in os.listdir(textdir):
filepath = os.path.join(textdir, filename)
if os.path.isfile(filepath):
text = open(filepath, "r")
text = text.read()
file = open("alltexts.txt", "a+") # Creating a new text file that contain all the texts
file.write(str(text) + "\n")
file.close()
with open("alltexts.txt", "r") as file:
text = file.read()
tokens = tokenizer.tokenize(text)
# Finding the words that stemmed and lemmatized and return them as final stem and leaf
for w in tokens:
stemmed_words.append(ps.stem(w))
for w in stemmed_words:
lemmatized_words.append(wn.lemmatize(w))
for i in range(len(lemmatized_words)): # Checking if lemmatized_words contain keys in new_dict
if lemmatized_words[i] not in new_dict.keys():
if lemmatized_words[i] != tokens[i]:
new_dict[lemmatized_words[i]] = [tokens[i]]
else: # Finding all the words which have the same root/stem
if lemmatized_words[i] != tokens[i]:
a = new_dict[lemmatized_words[i]]
if tokens[i] not in a:
a.append(tokens[i])
new_dict[lemmatized_words[i]] = a
allWords = []
for j, k in new_dict.items():
new_str = ""
newStr = ""
for i in range(len(k)):
new_str += k[i]
if i != (len(k) - 1):
new_str += ","
newStr = j + " " + "|" + " " + new_str
if newStr not in allWords:
allWords.append(newStr)
allWords.sort() # Sorts alphabetical order if the length of the leaf is the same
allWords.sort(key=len, reverse=True) # Sorts the leaf by descending length
print("NUMBER OF STEMS:", len(allWords))
for i in allWords:
print(i)