Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6602833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:55:30+00:00 2026-05-25T18:55:30+00:00

I wanna count words from text files which contain data as follows: ROK :

  • 0

I wanna count words from text files which contain data as follows:

ROK :
    ROK/(NN)
New :
    New/(SV)
releases, :
    releases/(NN) + ,/(SY)
week :
    week/(EP)
last :
    last/(JO)
compared :
    compare/(VV) + -ed/(EM)
year :
    year/(DT)
releases :
    releases/(NN)

The expressions like /(NN), /(SV), and /(EP) are considered category.
I wanna extract the words just before each of category and count how many words are in the whole text.

I wanna write a result in a new text file like this:

(NN)
releases 2
ROK 1

(SY)
New 1
, 1

(EP)
week 1

(JO)
last 1

......

Please help me out!

here is my garage code ;_; it doesn’t work.

import os, sys
import re

wordset = {}
for line in open('E:\\mach.txt', 'r'):
    if '/(' in line:
        word = re.findall(r'(\w)/\(', line)
        print word
        if word not in wordset: wordset[word]=1
        else: wordset[word]+=1

f = open('result.txt', 'w')
for word in wordset:
    print>> f, word, wordset[word]
f.close()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:55:31+00:00Added an answer on May 25, 2026 at 6:55 pm
    from __future__ import print_function                                                                                                                                                                                                                                  
    import re                                                                                                                                                                                                                                                              
    
    
    REGEXP = re.compile(r'(\w+)/(\(.*?\))')                                                                                                                                                                                                                                
    
    
    def main():                                                                                                                                                                                                                                                            
        words = {}                                                                                                                                                                                                                                                         
    
        with open('E:\\mach.txt', 'r') as fp:
            for line in fp:                                                                                                                                                                                                                                                    
                for item, category in REGEXP.findall(line):                                                                                                                                                                                                                    
                    words.setdefault(category, {}).setdefault(item, 0)                                                                                                                                                                                                         
                    words[category][item] += 1                                                                                                                                                                                                                                 
    
        with open('result.txt', 'w') as fp:                                                                                                                                                                                                                                       
            for category, words in sorted(words.items()):                                                                                                                                                                                                                      
                print(category, file=fp)                                                                                                                                                                                                                                       
                for word, count in words.items():                                                                                                                                                                                                                              
                    print(word, count, sep=' ', file=fp)                                                                                                                                                                                                                       
                print(file=fp)                                                                                                                                                                                                                                                 
        return 0                                                                                                                                                                                                                                                           
    
    if __name__ == '__main__':                                                                                                                                                                                                                                             
        raise SystemExit(main())
    

    You’re welcome (=
    If you will want also count that weird “-ed” or “,”, tune regexp to match any character except whitespace:

    REGEXP = re.compile(r'([^\s]+)/(\(.*?\))')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanna do a application with C# ; it will count correct words and
I wanna get the Timedate value from another page using request.querystring and then use
I wanna stop the reading of my text input file when the word synonyms
Total records in table are 10. Select count(ID) from table1 where col1 = 1
I have a query that takes 2minutes to count from table A and update
Sql: select distinct DateAdd(Day, DateDiff(Day, 0, m.Receive_date), 0) as Date, (select count(*) from Raw_Mats
i have a table array dynamically generated from a data query and stored in
How to count no. of characters entered in multiline textbox, i wanna enter maximum
I have already created a report using list of data from my database. i
SELECT COUNT(*) as totalHappenings FROM `happenings` WHERE `userId` = ? UNION SELECT COUNT(*) as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.