Ik heb een txt file met een htlm format dat drie artikelen aangegeven met < doc> < /doc> bevat.
Nu moet ik de woorden tellen en de volgende output krijgen:
[on] -> [1, 20] -> [2, 34] -> [3, 12]
[escape] -> [1, 7] -> [3, 2]
Echter heb ik nu code geschreven die alle woorden in het txt telt en niet apart voor de 3 artikelen:
import re
import nltk
import numpy as np
import matplotlib.pyplot as plt
from operator import itemgetter
file=open('/Users/ch 1/Desktop/data.txt')
def unicount(file):
dic={}
for word in file.read().split():
word = word.lower()
if tekens(word) == False:
continue
elif word in dic:
dic[word] += 1
else:
dic[word] = 1
print dic
print len(dic)
#print sorted(dic.items(), key=itemgetter(1)) ## print words on sorted count
#plt.bar(dic.keys(), dic.values(), align='center')
#plt.show()
def tekens(word):
''' Filtering out all punctuation marks'''
regex = re.compile("^[A-Za-z0-9]+$")
if regex.match(word):
return True
else:
return False
unicount(file)
Waar is als output het volgende krijg:
'effect': 1, 'deficits': 1, 'provide': 1
Nu moet ik de woorden tellen en de volgende output krijgen:
[on] -> [1, 20] -> [2, 34] -> [3, 12]
[escape] -> [1, 7] -> [3, 2]
Echter heb ik nu code geschreven die alle woorden in het txt telt en niet apart voor de 3 artikelen:
import re
import nltk
import numpy as np
import matplotlib.pyplot as plt
from operator import itemgetter
file=open('/Users/ch 1/Desktop/data.txt')
def unicount(file):
dic={}
for word in file.read().split():
word = word.lower()
if tekens(word) == False:
continue
elif word in dic:
dic[word] += 1
else:
dic[word] = 1
print dic
print len(dic)
#print sorted(dic.items(), key=itemgetter(1)) ## print words on sorted count
#plt.bar(dic.keys(), dic.values(), align='center')
#plt.show()
def tekens(word):
''' Filtering out all punctuation marks'''
regex = re.compile("^[A-Za-z0-9]+$")
if regex.match(word):
return True
else:
return False
unicount(file)
Waar is als output het volgende krijg:
'effect': 1, 'deficits': 1, 'provide': 1