473,608 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to calculate the number of unique words just in a part of a file

3 New Member
I have a file in Persian (a Persian sentence, a "tab", then a Persian word, again a "tab" and then an English word). I have to calculate the number of unique words just in Persian sentences and not the Persian and English words after the tabs. Here's the code:


[from hazm import*

file = "F.txt"
def WordsProbs (file):
words = set()
with open (file, encoding = "utf-8") as f1:
normalizer = Normalizer()
for line in f1:
tmp = line.strip().sp lit("\t")
words = set(normalizer. normalize(tmp[0].split()))
print(len(words ), "unique words")
print (words) ]



To access just the sentences I have to split each line by "\t". And to access each word of the sentence I have to split tmp[0]. The problem is, when I run the code the error below occurs. It's because of the split after tmp[0]. But if I omit this split after tmp[0], it just counts the letters not unique words. How can I fix it? (Is there another way to write this code to calculate unique words?).

The error: Traceback (most recent call last): File "C:\Users\yasin i\Desktop\16.py ", line 15, in WordsProbs (file) File "C:\Users\yasin i\Desktop\16.py ", line 10, in WordsProbs words.update(se t(normalizer.no rmalize(tmp[0].split()))) File "C:\Python34\li b\site-packages\hazm\N ormalizer.py", line 46, in normalize text = self.character_ refinement(text ) File "C:\Python34\li b\site-packages\hazm\N ormalizer.py", line 65, in character_refin ement text = text.translate( self.translatio ns) AttributeError: 'list' object has no attribute 'translate'

sample file: https://www.dropbox.com/s/r88hglemg7aot0w/F.txt?dl=0
Oct 29 '16 #1
1 1315
dwblas
626 Recognized Expert Contributor
I can not make much sense of your post. The code should be in code tags to preserve indentation which is important in python (click on the "CODE/" icon and place the code in between). Also the error message is difficult to read because it is posted here all on one line. Finally, we do not know where the "Normalizer " package is from and since it has not been imported, have to assume that this is an error that has to be fixed first.

The error says
AttributeError: 'list' object has no attribute 'translate'
which means you are sending a list to Normalizer and it does not like that, but we have no way of knowing whether you have written a "Normalizer " program or if it is from somewhere else, and so don't know what it expects to receive.

when I run the code the error below occurs. It's because of the split after tmp[0]
First, get the "spliit into words" portion working correctly and then add the "Normalizer ". Do this split on a line by itself and print the result to see what it contains. There is not enough info in this post for anyone to test it, i.e. code not formatted properly, what is "Normalizer ",
Oct 29 '16 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3261
by: Gary Wessle | last post by:
hi I have a data file with equal number of columns for each row. I need to get the number of rows and columns to allocate a matrix in gsl. getline (in, line) and parse the line for the number of spaces then add one to get the number of words then number_of_rows = 1; while getline (in, line) and number_of_rows++ does the number of rows I am not sure how to go about the number of words task, is there a c++
0
1303
by: pchahar | last post by:
Write a program to process a text file. The program will determine how many unique words there are in the text file that begin with each letter of the alphabet. The text file name will be given as a command line argument to the program. Sample Output: 4 words beginning with 'a'/'A': a, also, and, always 2 words beginning with 'b'/'B': be, both :
2
1939
by: surekhareddy | last post by:
can i count the number of words in a file
2
1632
by: tim knox | last post by:
can someone help me! i am using ASP Upload to upload files to my server. Does anyone know how I could automatically work out the number of words in each file? The files could be a .doc, .xls, .txt or .pdf
6
9098
by: timor.super | last post by:
Hi group, I need a way of calculating an unique id for a file. I've seen things like Crc32, 64, checksum .... there's a list here : http://en.wikipedia.org/wiki/List_of_hash_functions What is the best option for me ? I have to identify larges files and small files. I need something fast if possible. How can I be sure that it is unique ?
6
2104
by: sarfraznawaz | last post by:
Hi, I am Web Designer. I am new to this coding related job. Can anyone help me how to sort this out. How to calculate number of words with given price? Thanks. Sarfraz
4
1685
by: karry086 | last post by:
For e.g. i have a log file in which there are many entries for appGUID. Now how many appGUID's are there, for this i have a code, but how many of them are unique, for this i don't have code. Please can anyone help me on this.
0
8025
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8493
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6847
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4053
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2493
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1620
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.