-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathread_dir.py
More file actions
39 lines (32 loc) · 1.16 KB
/
read_dir.py
File metadata and controls
39 lines (32 loc) · 1.16 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
"""
Created on 17 Feb 2016
@author: ankita
"""
import nltk
from collections import Counter
import os
import sys
def read_dir(path):
#newpath is the path where the folder for the output files is created
newpath = path
if not os.path.exists(newpath):
os.makedirs(newpath)
os.chdir(r"C:\Users\Ankita\Desktop\WS 2015_16\Python\Project\Proj\InputFiles")
for file in os.listdir("C:\Users\Ankita\Desktop\WS 2015_16\Python\Project\Proj\InputFiles"):
if file.endswith(".txt"):
f = open(file, "r")
y = "parsed_" + os.path.basename(f.name)
filename = open(os.path.join(newpath,y) ,'w')
sys.stdout = filename
line = f.readline()
while line:
text = nltk.word_tokenize(line)
print text
tags = nltk.pos_tag(text)
print tags
counts = Counter(tag for word,tag in tags)
print counts
print '\n'
line = f.readline()
f.close()
read_dir('C:\Users\Ankita\Desktop\WS 2015_16\Python\Project\Proj\InputFiles\ParsedFiles')