Topshirish uchun python kodini yozishga yordam kerak

Menga topshiriq bor va buni qanday amalga oshirishni tushunmayman. Mana bu savol:

Quyidagi xatboshidagi so'zlar sonini va sonini hisobga olgan va ko'rsatadigan dastur yozing:

     

Turing mashinasi qoidalar jadvali bo'yicha lenta chizig'ida simvollarni manipulyatsiya qiluvchi qurilma. Uning soddaligiga qaramay, Turing mashinasi har qanday kompyuter algoritmining mantig'ini taqlid qilish uchun moslashtirilishi mumkin va ayniqsa, kompyuter ichidagi CPU funksiyalarini tushuntirishda foydalidir. "Turing" mashinasini 1936-yili Alan Turing ta'riflab, uni "(utomatic) -china" deb atagan. Turing mashinasi amaliy hisoblash texnologiyasi sifatida emas, balki hisoblash mashinasini ifodalovchi faraziy bir qurilma sifatida mo'ljallangan. Turing mashinalari kompyuter olimlariga mexanik hisoblash chegaralarini tushunishlariga yordam beradi.

Va men allaqachon yozganman:

def main():
    def word_count(str):
        counts = dict()
        words = str.split()

        for word in words:
            if word in counts:
                counts[word] +=1
            else:
                counts[word] = 1

        return counts

Rahmat

1
Siz muntazam ifodalarni va re modulini o'rgandingizmi? Bu so'zlarni vergullar va vaqtlar bilan yopishgan narsalarsiz olishning eng oson yo'li.
qo'shib qo'ydi muallif tdelaney, manba
Bu yaxshi yozilgan savol. Ishga kirishga harakat qilayotganingiz haqidagi xabar mavjud bo'lsa, uy vazifalari topshirilishi mumkin.
qo'shib qo'ydi muallif tdelaney, manba
Biz uy vazifalari haqida yordam bermaymiz. Lekin, men hech narsaga yaramaydigan narsalardan nafratlanaman. Bu kabi so'zlarni hisoblash mashqlari juda keng va kengroq muhokama qilinadi. Bundan tashqari, Python uchun son-sanoqsiz kirishlar mavjud. Faqat Google. :)
qo'shib qo'ydi muallif Bill Bell, manba
Kechirasiz, Emma!
qo'shib qo'ydi muallif Bill Bell, manba
Rahmat, @ juanpa.arrivillaga.
qo'shib qo'ydi muallif Bill Bell, manba
Tashakkur, @Delaney.
qo'shib qo'ydi muallif Bill Bell, manba
Kechirasiz, lekin sizning savolingiz qanday? To'liq bajarish uchun strip punktuatsiyani .
qo'shib qo'ydi muallif juanpa.arrivillaga, manba
@BillBell homework savollari, ular yo'l-yo'riqlariga yopishgan ekan . Emma: Ehtimol, Qanday qilib so'rash mumkin
qo'shib qo'ydi muallif juanpa.arrivillaga, manba
Dictning nima uchun kerakligini aniq bilmayman. Savolda men ko'rmayotgan boshqa bir qism bo'lmasa, faqatgina paragrafdagi so'zlar va jumlalar sonini so'raydi. Faqat paragraflarni bo'shliqlar bilan ajratib bo'lmaydi va so'zlar uchun ro'yxatning len() ni va '. ?
qo'shib qo'ydi muallif Wright, manba
Kodlarni sinab ko'rdim va u kompyuterimda ishlaydi. Diqqatga olish kerak bo'lgan yagona narsa shundan iboratki, kodingizda Main() ga qo'ng'iroq qilishingiz kerak.
qo'shib qo'ydi muallif Wenlong Liu, manba

6 javoblar

so'zlarni sanashda tinish belgilarini ajratib turadigan va vaziyatni e'tiborsiz qoldiradigan yana bir regeks hal. Umumiy so'zlar soni yoki noyob so'zlar sonini xohlaysizmi, amin emasman, shuning uchun ham men ...

So'zlarni hisoblash uchun so'zlar va collections.Counter so'zlarini topish uchun kerakli ifodani foydalaning r "\ w +" .

import collections
import re

text = """A Turing machine is a device that manipulates symbols on a strip of tape according to a table of rules. Despite its simplicity, a Turing machine can be adapted to simulate the logic of any computer algorithm, and is particularly useful in explaining the functions of a CPU inside a computer. The "Turing" machine was described by Alan Turing in 1936, who called it an "a(utomatic)-machine". The Turing machine is not intended as a practical computing technology, but rather as a hypothetical device representing a computing machine. Turing machines help computer scientists understand the limits of mechanical computation."""

print("Number of words:", sum(1 for _ in re.finditer(r"\w+", text)))
unique_words = collections.Counter(match.group(0).lower() for match in re.finditer(r"\w+", text))
print("Number of unique words:", len(unique_words))
print("Unique words:", ', '.join(sorted(unique_words)))
print("Number of sentences:", sum(1 for _ in re.finditer(r"\.", text)))

Natijada ...

$ python3 test.py
Number of words: 100
Number of unique words: 63
Unique words: 1936, a, according, adapted, alan, algorithm, an, and, any, as, be, but, by, called, can, computation, computer, computing, cpu, described, despite, device, explaining, functions, help, hypothetical, in, inside, intended, is, it, its, limits, logic, machine, machines, manipulates, mechanical, not, of, on, particularly, practical, rather, representing, rules, scientists, simplicity, simulate, strip, symbols, table, tape, technology, that, the, to, turing, understand, useful, utomatic, was, who
Number of sentences: 5
1
qo'shib qo'ydi

Yaxshiroq echim, mening fikrimcha, re modulidan foydalanmasdan quyidagi funktsiyalarni bajaradi:

def getWordCount(self):
    return len(self.split())
def getSentenceCount(self):
    return self.count(".")
print("Word count:",getWordCount(str),"\nSentence Count:",getSentenceCount(str))

Ushbu bosma:

Word count: 98 
Sentence Count: 5

Note: For sentences, this assumes that the last sentence ends with a dot(.) and there are no other dots expect for the ones delimiting sentences.

Jazoni hisoblagichga muqobil:

def getSentenceCount(self):
        return len(self.split("."))-1

Handling !, ?, ; and ...

Yuqorida tilga olingan belgilarning paydo bo'lishi uchun siz shunga o'xshash biror narsani qilishingiz kerak va bu mumkin bo'lgan har qanday belgi uchun takrorlang:

def getSentenceCount(self):
    st=self.replace("?",".").replace("!",".").replace("...",".").replace(";",".").replace("?!",".")
    return st.count(".")

Umid qilamanki bu yordam!

1
qo'shib qo'ydi
Menimcha, bu yaxshi. Yaxshi.
qo'shib qo'ydi muallif Bhargav, manba
Barchangizga katta rahmat
qo'shib qo'ydi muallif Mr. Xcoder, manba

Hech bo'lmasa qisman tushuntirish kabi tuyulardi. Siz Python uchun yangi bo'lib ko'ringaningiz uchun men oddiy imkoniyatlar bilan qolaman.

Ko'pgina Python dasturlari ushbu asosiy tuzilishga ega. (Hammasi emas.)

def main():
    # do something
    return

if __name__ == '__main__':
    main()

Men sizning buning bir qismini to'g'ri o'qitganingizdan shubhalanaman.

main funktsiyasi o'ziga xos vazifalarni bajarish uchun o'zida boshqa vazifalarni chaqirishi mumkin. main chaqiruvchining kirishini qabul qilishi mumkin, bu holda baholanadigan xat. Buni eslatishni istagan asosiy narsalar.

Lekin, nihoyat, so'zlarni aniqlash va hisoblashning eng oddiy usuli (Pythonda) uning bo'sh joylarida mag'lubiyatni ajratish va olingan qatorning uzunligini qayd qilishdir. Doktor zarur emas. Bu "avtomat-mashinada" tushishi mumkin; Biroq, insoniy mavjudot bo'lishi mumkin.

def main(input):
    print ('word count:', word_count(input))
    print ('sentence count:', sentence_count(input))
    return

def word_count(str):
    a_count = len(str.split())
    return a_count

def sentence_count(str):
    # count the sentences
    a_count = None
    return a_count

if __name__ == '__main__':

    paragraph ='''A Turing machine is a device that manipulates symbols on a strip of tape according to a table of rules. Despite its simplicity, a Turing machine can be adapted to simulate the logic of any computer algorithm, and is particularly useful in explaining the functions of a CPU inside a computer. The "Turing" machine was described by Alan Turing in 1936, who called it an "a(utomatic)-machine". The Turing machine is not intended as a practical computing technology, but rather as a hypothetical device representing a computing machine. Turing machines help computer scientists understand the limits of mechanical computation.'''
    main(paragraph)
0
qo'shib qo'ydi

Pythonda tabiiy tillarni qayta ishlash uchun "ntk" deb nomlangan ajoyib kutubxona mavjud. U matnni ishlash uchun turli funktsiyalarni o'z ichiga oladi, ulardan biri "sentence_tokenize" deb ataladi. Tashqi kutubxonalardan foydalanishingiz mumkin bo'lsa, uni osongina o'rnatishingiz mumkin.

Cmd oching va pip o'rnatish nltk ni ishga tushiring Shundan keyin quyidagi kod bilan skript yaratish va ishga tushirish:

import nltk
nltk.download()

This will open a window like this one: enter image description here

Yuklab olish uchun bosing va tugatish uchun kuting.

Shundan so'ng ntk kutubxonasidan matn/tabiiy tillarni qayta ishlash uchun foydalanishingiz mumkin.

Quyidagi kod bilan skript yarating va uni ishga tushiring:

  import nltk


text = """A Turing machine is a device that manipulates symbols on a strip of tape according to a table of rules.
 Despite its simplicity, a Turing machine can be adapted to simulate the logic of any computer algorithm, and is particularly useful in explaining the functions of a CPU inside a computer. 
 The "Turing" machine was described by Alan Turing in 1936, who called it an "a(utomatic)-machine". 
 The Turing machine is not intended as a practical computing technology, but rather as a hypothetical device representing a computing machine.
 Turing machines help computer scientists understand the limits of mechanical computation.
 """

sentences = nltk.sent_tokenize(text) # this function will "tokenize" the text and pull all the sentences from it into a list

words = nltk.word_tokenize(text)

print("Number of sentences: " + len(sentences))
print("Number of words: " + len(words))
0
qo'shib qo'ydi

Men buni qanday amalga oshiraman:

 example_string = "This is an example string. it is cool." # This would be your very long string of text.

 words = example_string.split(" ") #splits the string around every whitespace character
 words = words.trim() #just in case there's extra whitespace

 sentences = example.split(".") #splits the string around every "." character
 sentences = sentences.trim() #just in case there's extra whitespace

 numOfSentences = len(sentences) #gets the length of the array (number of sentences as an int
 numOfWords = len(words) #gets the length of the array (number of sentences as an int

 print(numOfWords)
 print(numOfSentences)

Chiqish quyidagicha bo'lishi kerak:

 8
 2
0
qo'shib qo'ydi

Quyidagi Python skripti (keling, uni ./counter.py deb ataymiz) ishni bajaradi:

#!/usr/bin/python

import fileinput
import re

words =0
sents =0

for line in fileinput.input():
        words += len(line.split())
        sents += len(re.findall("(!\?)|(\.\.\.)|[\.?!]", line))

print 'Total words:     ', words
print 'Total sentances: ', sents

Paragrafni "/test.txt" faylida saqlaymiz.

monty:python%> cat ./test.txt | ./counters.py
Total words:      98
Total sentances:  5
monty:python%> 
0
qo'shib qo'ydi
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming