Counting three letter acronyms in a line with Regex Python

NoviceProgrammer

I need to make a program in python which looks through a given file. Let's say acronyms.txt, and then returns a percentage value of how many lines contain at least 1 three letter acronym. For example:

NSW is a very large state.
It's bigger than TAS.
but WA is the biggest!

After reading this it should return 66.7% as 66.7% of the lines contain a three letter acronym. It is also rounded to the first decimal place as you can see. I am not very familiar with regex but I think it would be simplest with regex.

EDIT:

I have finished the code but i need it to recognize acronyms with dots between them, EG N.S.W should be recognized as an acronym. How do i do this?

Any help would be appreciated!

dzhioev

You can do something like:

total_lines = 0
matched_lines = 0
for line in open("filename"):
    total_lines += 1
    matched_lines += bool(re.search(r"\b[A-Z]{3}\b", line))
print "%f%%" % (float(matched_lines) / total_lines * 100)

Note '\b' in search pattern -- it matches empty string in beginning or end of word. It helps you to prevent unwanted matches with acronyms longer than 3 ('asdf ASDF asdf') or with acronyms inside word ('asdfASDasdf').

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Regex to extract acronyms

分類Dev

Split by regex of new line and capital letter

分類Dev

Javascript on counting occurence of letter 'Z' in for loop textfield

分類Dev

Javascript on counting occurence of letter 'Z' in for loop textfield

分類Dev

Python multi-line regex greedy group

分類Dev

Acronyms in CamelCase

分類Dev

Python : Regex search on a file, and another regex in the next line

分類Dev

Regex pattern counting with repetitive words

分類Dev

Three columns in line

分類Dev

Python regex replace each match with itself plus a new line

分類Dev

python reading the next n lines based on previous line using regex

分類Dev

Python regex: How to match a string at the end of a line in a file?

分類Dev

regex pattern a few numbers followed by the letter "k"

分類Dev

Find the first letter and sign of a sentence with Regex

分類Dev

Regex to validate information (Letter & Number only)

分類Dev

Regex: Match if strings starts with a letter or number and then

分類Dev

JavaScript regex match anything except a letter

分類Dev

How to find the last letter of a line with TUSTEP

分類Dev

Creating a function that replace a letter with a line break

分類Dev

Counting occurence within a string Python

分類Dev

Regex to match the end of line

分類Dev

Regex to Capture rest of the line

分類Dev

regex to include line breaks

分類Dev

Counting first letter of string and showing how many times it appears, but not in alphabetical order in R

分類Dev

Does python len() faster than just counting

分類Dev

counting and storing values in a dictionary using python

分類Dev

Counting the nodes in a Binary Search Tree in Python

分類Dev

counting occurence of a word in a text file using python

分類Dev

Python regex. Removing all characters after ':' (including at the end of line and except for a specific string)

Related 関連記事

  1. 1

    Regex to extract acronyms

  2. 2

    Split by regex of new line and capital letter

  3. 3

    Javascript on counting occurence of letter 'Z' in for loop textfield

  4. 4

    Javascript on counting occurence of letter 'Z' in for loop textfield

  5. 5

    Python multi-line regex greedy group

  6. 6

    Acronyms in CamelCase

  7. 7

    Python : Regex search on a file, and another regex in the next line

  8. 8

    Regex pattern counting with repetitive words

  9. 9

    Three columns in line

  10. 10

    Python regex replace each match with itself plus a new line

  11. 11

    python reading the next n lines based on previous line using regex

  12. 12

    Python regex: How to match a string at the end of a line in a file?

  13. 13

    regex pattern a few numbers followed by the letter "k"

  14. 14

    Find the first letter and sign of a sentence with Regex

  15. 15

    Regex to validate information (Letter & Number only)

  16. 16

    Regex: Match if strings starts with a letter or number and then

  17. 17

    JavaScript regex match anything except a letter

  18. 18

    How to find the last letter of a line with TUSTEP

  19. 19

    Creating a function that replace a letter with a line break

  20. 20

    Counting occurence within a string Python

  21. 21

    Regex to match the end of line

  22. 22

    Regex to Capture rest of the line

  23. 23

    regex to include line breaks

  24. 24

    Counting first letter of string and showing how many times it appears, but not in alphabetical order in R

  25. 25

    Does python len() faster than just counting

  26. 26

    counting and storing values in a dictionary using python

  27. 27

    Counting the nodes in a Binary Search Tree in Python

  28. 28

    counting occurence of a word in a text file using python

  29. 29

    Python regex. Removing all characters after ':' (including at the end of line and except for a specific string)

ホットタグ

アーカイブ