0
파일에서 읽고 계산을하는 프로그램을 작성했습니다. 추가 할 마지막 기능이 있습니다. 특정 단어가 몇개 나 있는지 계산해야합니다.문장의 문장에서 단어 발생을 계산하는 방법
# -*- coding: utf-8 -*-
from sys import argv # importing argv module so we can specify file to read
script, filename = argv # run program like this: python litvin.py filename.txt
txt = open(filename) # open file
text = txt.read() # get text from file
def count_letters(file):
""" function that counts symbols in text without spaces """
return len(file) - file.count(' ')
print "\nКількість слів в тексті: %d" % count_letters(text)
def count_sentences(file):
""" function that counts sentences through finding '.'(dots)
not quite cool but will work """
return file.count('.')
print "\nКількість речень в тексті: %d" % count_sentences(text)
def longest_word(file):
""" function that finds the longest word in the text """
return max(file.split(), key=len)
print "\nНайдовше слово в тексті: '%s'" % longest_word(text)
def shortest_word(file):
""" function that finds the longest word in the text """
return min(file.split(), key=len)
print "\nНайкоротше слово в тексті: '%s'" % shortest_word(text)
def word_occurence(file):
""" function that finds how many times specific word occurs in text """
return file.count("ноутбук")
print "\nКількість разів 'ноутбук' зустрічається в тексті: %d" %
word_occurence(text)
print "\n\n\t\tЩоб завершити програму натисніть 'ENTER' "
raw_input()
나는 당신이 제안한 텍스트를 분할 했으므로 지금은 항목 목록 (문장)을 가지고 있습니다. 각 항목에서 특정 단어를 찾는 방법은 무엇입니까? 나는 .count를 시도했으나 항목 만 찾고 무언가를 찾지 않아 작동하지 않는다. –
각 문장을 얻기 위해 (for를 사용하여)리스트를 반복 할 수있다. 그것은 당신이'count()'를 사용하는 곳입니다. – Fejs