1

I'm trying to make a function that will take an argument that's a word (or set of characters) as well as the speech, and return a boolean expression saying whether the word is there or not, as a function.

speech2 = open("Obama_DNC.txt", "r")
speech2_words = speech2.read()
def search(word):
    if word in speech2_words:
        if len(word) == len(word in speech2_words):
            print(True)
        elif len(word) != len(word in speech2_words):
            print(False)
    elif not word in speech2_words:
        print(False)


word = input("search?")
search(word)

I want to make it so that the word that the program searches for in the text matches exactly as the input and that are not a part of another word ("America" in "American"). I thought of using the len() function but it doesn't seem to work and I am stuck. If anyone helps me figure this out that would be very helpful. Thank you in advance

eddie
  • 41
  • 1
  • 6
  • `len(word in speech2_words)` isn't valid: you're passing a boolean to `len`. You have to split your strings or use regex and search using `r"\b"+word+r"\b"` – Jean-François Fabre Apr 03 '17 at 19:05

2 Answers2

1

One option may be to use the the findall() method in the regex module, which can be used to find all occurrences of a specific string.

Optionally, you could include list.count() to check how many times the searched string occurs in the text:

import re

def search(word):
    found = re.findall('\\b' + word + '\\b', speech2_words)
    if found:
        print(True, '{word} occurs {counts} time'.format(word=word, counts=found.count(word)))
    else:
        print(False)

output:

search?America
(True, 'America occurs 28 time')
search?American
(True, 'American occurs 12 time')
1

You can use mmap also, for more information about the mmap

mmap in python 3 is treated differently that in python 2.7

Below code is for 2.7, what it does looking for a string in the text file.

#!/usr/bin/python

import mmap
f = open('Obama_DNC.txt')
s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
if s.find('blabla') != -1:
    print 'true'

Why mmap doesnt work with large files.

Community
  • 1
  • 1
danglingpointer
  • 4,708
  • 3
  • 24
  • 42