yesh666

SET 4 (NLTK and sales men)

Jan 4th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. ###NLTK part 1
  2.  
  3.  
  4. import nltk
  5. from nltk.corpus import stopwords
  6. f1=open("file1.txt","r")
  7. f2=open("file2.txt","w")
  8. stop = stopwords.words('english')
  9. for line in f1:
  10.     w=line.split(" ")
  11.     for word in w:
  12.         if word not in stop:
  13.             f2.write(word)
  14.             f2.write(" ")
  15. f1.close()
  16. f2.close()
  17.  
  18.  
  19. ############################################  NLTK part 2
  20. from nltk.stem import PorterStemmer
  21. from nltk.tokenize import word_tokenize
  22. ps = PorterStemmer()
  23. example_words = ["python","pythoner","pythoning","pythoned","pythonly"]
  24. for w in example_words:
  25.     print(ps.stem(w))
  26.  
  27.  
  28. ################### Travelling Salesman Problem
  29.  
  30. from sys import maxsize
  31. from itertools import permutations
  32. V = 4
  33. def travellingSalesmanProblem(graph, s):
  34.     vertex = []
  35.     for i in range(V):
  36.         if i != s:
  37.             vertex.append(i)
  38.     min_path = maxsize
  39.     next_permutation=permutations(vertex)
  40.     for i in next_permutation:
  41.         current_pathweight = 0
  42.         k = s
  43.         for j in i:
  44.             current_pathweight += graph[k][j]
  45.             k = j
  46.         current_pathweight += graph[k][s]
  47.         min_path = min(min_path, current_pathweight)
  48.          
  49.     return min_path
  50.  
  51.  
  52. if __name__ == "__main__":
  53.     graph = [[0, 10, 15, 20], [10, 0, 35, 25],[15, 35, 0, 30], [20, 25, 30, 0]]
  54.     s = 0
  55.     print(travellingSalesmanProblem(graph, s))
Add Comment
Please, Sign In to add comment