Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###NLTK part 1
- import nltk
- from nltk.corpus import stopwords
- f1=open("file1.txt","r")
- f2=open("file2.txt","w")
- stop = stopwords.words('english')
- for line in f1:
- w=line.split(" ")
- for word in w:
- if word not in stop:
- f2.write(word)
- f2.write(" ")
- f1.close()
- f2.close()
- ############################################ NLTK part 2
- from nltk.stem import PorterStemmer
- from nltk.tokenize import word_tokenize
- ps = PorterStemmer()
- example_words = ["python","pythoner","pythoning","pythoned","pythonly"]
- for w in example_words:
- print(ps.stem(w))
- ################### Travelling Salesman Problem
- from sys import maxsize
- from itertools import permutations
- V = 4
- def travellingSalesmanProblem(graph, s):
- vertex = []
- for i in range(V):
- if i != s:
- vertex.append(i)
- min_path = maxsize
- next_permutation=permutations(vertex)
- for i in next_permutation:
- current_pathweight = 0
- k = s
- for j in i:
- current_pathweight += graph[k][j]
- k = j
- current_pathweight += graph[k][s]
- min_path = min(min_path, current_pathweight)
- return min_path
- if __name__ == "__main__":
- graph = [[0, 10, 15, 20], [10, 0, 35, 25],[15, 35, 0, 30], [20, 25, 30, 0]]
- s = 0
- print(travellingSalesmanProblem(graph, s))
Add Comment
Please, Sign In to add comment