Python ծրագիր՝ տեքստային ֆայլում եզակի բառերի քանակը գտնելու համար


Այս հոդվածում տրված խնդիրն է՝ գտնել տեքստային ֆայլում եզակի բառերի քանակը։ Python-ի այս հոդվածում, օգտագործելով երկու տարբեր օրինակներ, տրված են տեքստային ֆայլում եզակի բառերը գտնելու մոտեցումները և դրանց քանակը: Առաջին օրինակում տեքստային ֆայլից տրված բառերը բերվում են, և այնուհետև կազմվում է դրանց եզակի հավաքածուն՝ նախքան այս եզակի բառերը հաշվելը: Օրինակ 2-ում նախ ստեղծվում է բառերի ցանկը, այնուհետև այն տեսակավորվում է։ Այս տեսակավորված ցանկից հետո կրկնօրինակները հեռացվում են և վերջապես ֆայլում մնացած եզակի բառը հաշվվում է վերջնական արդյունքը տալու համար:

Նախամշակման ալգորիթմ

Քայլ 1 - Մուտք գործեք Google հաշվի միջոցով: Գնացեք Google Colab: Բացեք նոր Colab Notebook և դրա մեջ գրեք Python կոդը:

Քայլ 2 − Նախ վերբեռնեք «file1.txt» txt ֆայլը Google Colab:

Քայլ 3 - Բացեք txt ֆայլը կարդալու համար:

Քայլ 4 - Փոխակերպեք տեքստային ֆայլը փոքրատառի:

Քայլ 5 - txt ֆայլում տրված բառերն առանձնացնելու համար օգտագործեք split ֆունկցիան:

Քայլ 6 − Տպեք «words_in_file» կոչվող ցանկը՝ ունենալով բառերը տեքստային ֆայլից:

Տեքստային ֆայլ, որն օգտագործվում է այս օրինակների համար

file1.txt-ի բովանդակությունը տրված է այստեղ…

This is a new file.
This is made for testing purposes only.
There are four lines in this file.
There are four lines in this file.
There are four lines in this file.
There are four lines in this file.
Oh! No.. there are seven lines now.

Վերբեռնեք file1.txt-ը colab

Նկար. file1.txt-ի վերբեռնում Google Colab-ում

Մոտեցում 1.- Գտնել եզակի բառերի թիվը տեքստային ֆայլում՝ օգտագործելով Python Sets

Նախամշակման քայլերից հետո Մոտեցման 1-ի համար օգտագործվում են հետևյալ քայլերը

Քայլ 1 − Սկսեք «words_in_file» ցանկից՝ նախնական մշակման քայլերից հետո:

Քայլ 2 − Փոխակերպեք այս ցուցակը հավաքածուի: Այստեղ հավաքածուն կպարունակի միայն եզակի բառեր:

Քայլ 3 − Ցուցադրել հավաքածուն բոլոր եզակի բառերով՝ օգտագործելով տպագիր արտահայտությունը:

Քայլ 4 - Գտեք սահմանված երկարությունը:

Քայլ 5 - Տպեք սահմանված երկարությունը:

Քայլ 6 - Սա կտա տվյալ տողի եզակի բառերի թիվը:

Օրինակ

# Use open method to open the respective text file
file = open("file1.txt", 'r')

#Conversion of its content to lowercase
thegiventxtfile = file.read().lower()

#ALter the sentences to the list of words
words_in_file = thegiventxtfile.split()

print("The given txt file content is :\n")
print(thegiventxtfile)
print("\nThe words given in the txt file are :\n")
print(words_in_file)
print("\nThe unique words given in this txt file are :\n")

#Convert to the python set
uniqueWords=set(words_in_file)

print(uniqueWords) 

#Find the number of words left in this list
numberofuniquewords=len(uniqueWords)

print("\nThe number of unique words given in this txt file are :\n")
print(numberofuniquewords)

Արդյունք

The given txt file content is :

this is a new file.
this is made for testing purposes only.
there are four lines in this file.
there are four lines in this file.
there are four lines in this file.
there are four lines in this file.
oh! no.. there are seven lines now.


The words given in the txt file are :

['this', 'is', 'a', 'new', 'file.', 'this', 'is', 'made', 'for', 'testing', 'purposes', 'only.', 'there', 'are', 'four', 'lines', 'in', 'this', 'file.', 'there', 'are', 'four', 'lines', 'in', 'this', 'file.', 'there', 'are', 'four', 'lines', 'in', 'this', 'file.', 'there', 'are', 'four', 'lines', 'in', 'this', 'file.', 'oh!', 'no..', 'there', 'are', 'seven', 'lines', 'now.']

The unique words given in this txt file are :

{'there', 'only.', 'testing', 'new', 'is', 'for', 'oh!', 'this', 'a', 'made', 'seven', 'are', 'purposes', 'in', 'file.', 'four', 'now.', 'no..', 'lines'}

The number of unique words given in this txt file are :

19

Մոտեցում 2.- Տեքստային ֆայլում եզակի բառերի քանակը գտնելը Python բառարանի միջոցով

Քայլ 1 - Բացեք անհրաժեշտ ֆայլը:

Քայլ 2 - Տեսակավորեք այս ցուցակը և տպեք այս ցուցակը: Այբբենական կարգով դասավորված ցանկը ցույց կտա նաև կրկնվող բառերը:

Քայլ 3 − Այժմ կրկնօրինակ բառերից ազատվելու և միայն օգտագործված եզակի dict.fromkeys-ը (words_in_file) պահելու համար:

Քայլ 4 − Սա պետք է վերադառնա ցուցակին հիմա:

Քայլ 5 − Վերջապես տպեք եզակի բառեր պարունակող ցուցակը:

Քայլ 6 - Հաշվեք վերջնական ցուցակի երկարությունը և ցուցադրեք դրա արժեքը: Սա կտա տվյալ տողի եզակի բառերի թիվը:

Օրինակ

#Open the text file in read mode
file = open("file1.txt", 'r')

#Convert its content to lowercase
thegiventxtfile = file.read().lower()

#Change the sentences to the list of words
words_in_file = thegiventxtfile.split()

print("The given txt file content is :\n")
print(thegiventxtfile)
print("\nThe words given in the txt file are :\n")
print(words_in_file)
print("\nThe sorted words list from this txt file is :\n")

#Sort this words file now
words_in_file.sort()
  
print(words_in_file)
print("\nThe sorted words list after removing duplicates from this txt file is :\n")

#Get rid of the duplicate words
myuniquewordlist = list(dict.fromkeys(words_in_file))

#Count the number of words left
numberofuniquewords=len(uniqueWords)

print(myuniquewordlist) 
print("\nThe number of unique words given in this txt file are :\n") 

Արդյունք

The given txt file content is :

this is a new file.
this is made for testing purposes only.
there are four lines in this file.
there are four lines in this file.
there are four lines in this file.
there are four lines in this file.
oh! no.. there are seven lines now.


The words given in the txt file are :

['this', 'is', 'a', 'new', 'file.', 'this', 'is', 'made', 'for', 'testing', 'purposes', 'only.', 'there', 'are', 'four', 'lines', 'in', 'this', 'file.', 'there', 'are', 'four', 'lines', 'in', 'this', 'file.', 'there', 'are', 'four', 'lines', 'in', 'this', 'file.', 'there', 'are', 'four', 'lines', 'in', 'this', 'file.', 'oh!', 'no..', 'there', 'are', 'seven', 'lines', 'now.']

The sorted words list from this txt file is :

['a', 'are', 'are', 'are', 'are', 'are', 'file.', 'file.', 'file.', 'file.', 'file.', 'for', 'four', 'four', 'four', 'four', 'in', 'in', 'in', 'in', 'is', 'is', 'lines', 'lines', 'lines', 'lines', 'lines', 'made', 'new', 'no..', 'now.', 'oh!', 'only.', 'purposes', 'seven', 'testing', 'there', 'there', 'there', 'there', 'there', 'this', 'this', 'this', 'this', 'this', 'this']

The sorted words list after removing duplicates from this txt file is :

['a', 'are', 'file.', 'for', 'four', 'in', 'is', 'lines', 'made', 'new', 'no..', 'now.', 'oh!', 'only.', 'purposes', 'seven', 'testing', 'there', 'this']

The number of unique words given in this txt file are :

19

Եզրակացություն

Երկու տարբեր մոտեցումներ ցույց տալու, թե ինչպես գտնել եզակի բառեր տվյալ txt ֆայլում: Նախ, txt ֆայլը վերբեռնվում է colab նոթատետրում: Այնուհետև այս ֆայլը բացվում է կարդալու համար: Այնուհետև այս ֆայլը բաժանվում է, և բառերն առանձնացվում և պահվում են որպես ցուցակ: Python-ի այս հոդվածում այս բառերի ցանկն օգտագործվում է երկու օրինակներում:

Օրինակ 1-ում օգտագործվում է Python set հասկացությունը: Ցանկը կարող է պարունակել կրկնօրինակ բառեր: Երբ այս ցուցակը վերածվում է բազմության, մնում են միայն եզակի բառերը: Եզակի բառերի քանակը հաշվարկելու համար օգտագործվում է len() ֆունկցիան: Օրինակ 2-ում txt ֆայլից ստացված բառերի ցանկը նախ տեսակավորվում է՝ տեսնելու կրկնօրինակ բառերի թիվը, որոնք հավաքվում են տեսակավորվելուց հետո: Այժմ այս տեսակավորված ցուցակն օգտագործվում է dict.fromkeys(words_in_file) հետ կրկնօրինակ բառերը հեռացնելու համար: Այն հետագայում օգտագործվում է կրկնօրինակ բառերի քանակը գտնելու համար: