Python Forum
Grouping lots of words in lots of txt files: programmer needed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grouping lots of words in lots of txt files: programmer needed
#11
Is this your homework?


#!/usr/bin/env python3
from string import ascii_uppercase as upper

# creating a mapping for A-Z : 1-26
# access is very fast
mapping = {char: value for (char, value) in zip(upper, range(1, len(upper) + 1))}

# {'A': 1,
#  'B': 2,
#  'C': 3,
#  'D': 4,
#  'E': 5,
#  'F': 6,
#  'G': 7,
#  'H': 8,
#  'I': 9,
#  'J': 10,
#  'K': 11,
#  'L': 12,
#  'M': 13,
#  'N': 14,
#  'O': 15,
#  'P': 16,
#  'Q': 17,
#  'R': 18,
#  'S': 19,
#  'T': 20,
#  'U': 21,
#  'V': 22,
#  'W': 23,
#  'X': 24,
#  'Y': 25,
#  'Z': 26}

# functional approach
def word_sum(word):
    return sum(map(lambda x: mapping.get(x.upper(), 0), word))

# word_sum('!-.$%"§A')
# should return 1

# better to understand
def word_sum(word):
    result = 0
    for char in word:
        # make char uppercase, return 0 if char not in mapping
        # https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
        value = mapping.get(char.upper(), 0)
        result += value
    return result


def analyse_and_write(file_in):
    # using a context manager
    # leaving the context closes the file :-)
    with open(file_in) as fd:
        # this iterates line for line over the text.
        for line in fd:
            # calculates the sum and converts the integer into a string
            s = word_sum(line)
            if not s: #when s == 0, skip it
                continue
            s = str(s) # we need the number as string to write it into the textfile
            file_out = s + '.txt'
            # here a context manager again to write the file
            with open(file_out, 'a') as out:
            # 'a' is for append. If the file does not exists,
            # it will be created. If the file exists, it will append.
                out.write(line)


if __name__ == '__main__':
    print('Staring program')
    analyse_and_write('WordList100000Plus.txt')
    print('Done')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#12
Wow, what a patience - writing out this dictionary Doh
In [4]: def upper_index(c):
   ...:     return ord(c) - ord('A') + 1
   ...:

In [5]: upper_index('B')
Out[5]: 2

In [6]: upper_index('Z')
Out[6]: 26
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#13
I am not sure whether I keep the code which I've used to do it. Perhaps I could post it here
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#14
Just post it and we can tell you if you can improve it and if it's pythonic.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#15
It is Python, not Java. What do you mean 'pythonic'?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#16
My statement was more general.

Pythonic means, that you use the language concepts of Python. Sometimes newcomers (I don't know if you're), which came from other languages, are using the concepts of the language they already know. This produces unreadable code and sometimes you'll also have performance issues.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#17
"pythonic" is a lot like duck typing.  If it looks like python, then it's pythonic.  Semi-colons are legal statement terminators in python, but using them is most definitely not pythonic.  CamelCase variable names, for anything that's not a class definition, is likewise valid, yet is not pythonic.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Programmer looking for work Johndg20 1 2,244 Sep-12-2021, 01:21 PM
Last Post: Larz60+
  programmer for my project siliconvalleymusicbrokerscom 1 2,937 Feb-01-2018, 07:51 AM
Last Post: PythonMagic003
  Python programmer needed to integrate GUI in Linux/LinuxCNC blazini36 1 4,232 Aug-18-2017, 03:32 PM
Last Post: victor_cis
  Need hired help from python programmer oj43085 6 6,250 May-10-2017, 06:06 PM
Last Post: nilamo
  Summer programmer needed to teach camp Tampa, Florida MOSI MOSI 0 3,061 Apr-18-2017, 04:55 PM
Last Post: MOSI

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020