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

  • Home
  • SEARCH
  • 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 8641997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:40:03+00:00 2026-06-12T11:40:03+00:00

I have a code which takes as input two files: (1) a dictionary/lexicon (2)

  • 0

I have a code which takes as input two files:
(1) a dictionary/lexicon
(2) a text file (one sentence per line)

The first part of my code reads the dictionary in tuples so outputs something like:

('mthy3lkw', 'weakBelief', 'U')

('mthy3lkm', 'firmBelief', 'B')

('mthy3lh', 'notBelief', 'A')

The second part of the code is to search each sentence in the text file for the words in position 0 in those tuples and then print out the sentence, the search word and it’s type.

So given the sentence mthy3lkw ana mesh 3arif , desired output is:

[“mthy3lkw ana mesh 3arif”, ‘mthy3lkw‘, ‘weakBelief’, ‘U’] given that the highlighted word is found in the dictionary.

The second part of my code – the matching part – is TOO slow. How do I make it faster?

Here is my code

findings = [] 
for sentence in data:  # I open the sentences file with .readlines()
    for word in tuples:  # similar to the ones mentioned above
        p1 = re.compile('\\b%s\\b'%word[0])  # get the first word in every tuple
        if p1.findall(sentence) and word[1] == "firmBelief":
            findings.append([sentence, word[0], "firmBelief"])

print findings  
  • 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-06-12T11:40:04+00:00Added an answer on June 12, 2026 at 11:40 am

    Build a dict lookup structure so you can find the correct one from your tuples quickly. Then you can restructure your loops so that instead of going through your whole dictionary for each sentence, trying to match every entry up, you instead go over each word in the sentence and look it up in the dictionary dict:

    # Create a lookup structure for words
    word_dictionary = dict((entry[0], entry) for entry in tuples)
    
    findings = []
    word_re = re.compile(r'\b\S+\b') # only need to create the regexp once
    for sentence in data:
        for word in word_re.findall(sentence): # Check every word in the sentence
            if word in word_dictionary: # A match was found
                entry = word_dictionary[word]
                findings.append([sentence, word, entry[1], entry[2]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which takes a touch on one button and draws
I have written a program which takes a file as input and whenever it
I have to define a function called zeros which takes input of two lists
I have some code which takes strings representing hexadecimal numbers - hex colors, actually
Problem Hello all! I have this code which takes my jpg image loops through
I have the following code which takes List of boolean as a parameter then
I have created a piece of code which takes an IP address (from main
I have the following code: <div><strong>Name: </strong><?php echo anchor('http://'.$link, $row->Name); ?></div> Which takes a
I have an api which takes uni code data as c character array and
I have code, which i want to run, this takes a few mins so

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.