How to Find the Longest Word in a Text File

How can I find the longest word in a text file?

So you want to find the longest sorted combination from a set of letters that exist in your dictionary.

To do so, you'd use itertools.combinations() with a length equal to the length of your string. You'd check all of those combinations against the sorted dictionary, and if you don't find a match, decrease the combination length.

You also want to load the entire dictionary into a set to decrease search times. I've loaded the set of words into a dictionary, where the key is the sorted string, and the value is a list of words that have the same sorted representation.

Something like this:

import itertools
from collections import defaultdict

words = defaultdict(list)
with open('/usr/share/dict/words') as qfile:
for word in qfile:
word = word.rstrip('\n').lower()
words[''.join(sorted(word))].append(word)

def longest_anagram(term, words):
search_length = len(term)
term = sorted(term) # combinations maintains sort order
while search_length > 0:
for combo in itertools.combinations(term, search_length):
search = ''.join(combo) # sort above means we dont need it here
if search in words:
return words[search]
search_length -= 1
return None

found = longest_anagram('qugteroda', words)
for w in found:
print(w)

For completeness I should mention that this approach is appropriate for a search string of 18 letters or less. If you need to find the longest anagram out of a string of letters that is greater than 18, you're better off flipping the algorithm so you sort the dictionary words by length into a list. You'd then iterate through all the words and check to see if they exist in the input search string - much like @abarnert's answer.

Finding the longest word in a text file

Normally, you'd want to use a while read loop instead of for i in $(cat), but since you want all the words to be split, in this case it would work out OK.

#!/bin/bash
longest=0
for word in $(<so.txt)
do
len=${#word}
if (( len > longest ))
then
longest=$len
longword=$word
fi
done
printf 'The longest word is %s and its length is %d.\n' "$longword" "$longest"

Finding the longest word in a .txt file without punctuation marks

You have to strip those characters from the words:

with open("original-3.txt", 'r') as file1:
lines = file1.readlines()
for line in lines:
if not line == "\n":
print(max(word.strip(",?;!\"") for word in line.split()), key=len))

or you use regular expressions to extract everything that looks like a word (i.e. consists of letters):

import re


for line in lines:
words = re.findall(r"\w+", line)
if words:
print(max(words, key=len))

Python - Finding the longest word in a text file error

This should work for you:

from functools import reduce

def find_longest_word(filename):
f = open(filename, "r")
s = [y for x in f.readlines() for y in x.split()]
longest_word = reduce(lambda x, y: y if len(x) < len(y) else x, s, "")
print("The longest word is", longest_word, "and it is", len(longest_word),"characters long")

return longest_word

print(find_longest_word('input.txt'))

Longest word in a txt file python code?

You can use list comprehensions to turn the input into a single list and then use reduce on the resulting list to find the longest string.

f = open("input.txt", "r")
s = [y for x in f.readlines() for y in x.split()]
longest = reduce(lambda x, y: y if len(x) < len(y) else x, s, "")
print("The longest word is", longest, "and it is", len(longest),"characters long")

find the longest word and the largest number in a text file

You can implement error handling and try to parse the str as int: "2" --> 2

def longest_integer(word):
max_int = 0
with open(word, 'r') as infile:
words = infile.read().split()
for word in words:
try:
int_val = int(word)
if int_val > max_int:
max_int = int_val
except:
pass
return max_int
print (("the longest integer is :"), longest_integer ('text.txt'))


Related Topics



Leave a reply



Submit