WSQ12 ‘Word Count’

The end of the semester is near, and we are seeing more advanced codes that in the beggining of the semester. This WSQ was kind of cool for me, since I enjoyed using the open files as read and writting. I do not know why, but do not judge me.

shiba inu.png

Majo —^

For testing purposes, the text used for this WSQ says the following:

ThisThis This is a test for wsq012 in order to count the words in this file
wsq012
words
thisThis
sentences
allwordstogethertoproveapointallall

I used two methods for this WSQ. The first one was with a line.split() while reading the document. This way the program separates and reads the document line by line. Then search the word in the document with a conditional if. Then it adds the total matches to the counter.

WSQ12v2

As you may notice, this method does not distinguishes the matches of the established word if this word is mixed between the text without any space to separate it. For example, “This” is the word to search. Text says “Thisthis” that are two matches, tecnically; however, the code only recognizes it as one.

This is why I made the second version of the WSQ. Ken suggested to use find() in order to avoid this problem; however, since I failed in using it correctly, I rather used count(). By the way, I also improved the code by using .lower(), so it does not matters if the text and words are submited in cappital letters. 

WSQ12v3

What is this? A picture for ants? Well… you can take a closer look at the code in my GitHub!

Now that this blog has finally reach it’s end. Let me introduce you to Cage the Elephant and their song called “Ain’t No Rest for the Wicked”. Released

no rest for the wicked

Continue reading “WSQ12 ‘Word Count’”

WSQ 12 – Word Count

En este ejercicio tenemos que buscar el numero de palabras que se encuentran en un documento.

Lo primero que hice es preguntarle al usuario que palabra quiere buscar, depuse cree una función que lo que hace es buscar la palabra en todo el documento renglón por renglon, antes de empezar a buscar hago que la palabra a comparar la convierta en minúsculas, cada vez que encuentre una se suma a mi contador “cantidad”

Código completo;

palabra=(input("¿Que palabra quieres buscar?:  "))
def numpalabra():
archivo=open('Hola.txt','r')
cantidad=0
for i in archivo:
minisculas=i.lower()
sub=minisculas.find(palabra)
while sub !=-1:
cantidad=cantidad+1
sub=minisculas.find(palabra,(sub+1))
return(cantidad)
close('Hola.txt')
ba=numpalabra()
print("las veces que se encuentra la palabra: ", palabra, " es ",ba)

Click en la imagen para ver el codigo en github:

Texto en el que busca:

hola como estas hola
que bien y tu
hace como loca hola
brandon es guapo HOLA

Compilado:

WSQ12, thanks Internet.

keep-calm-try-again

Helloooooo, I’m here to write about the last WSQ of the partial, and yeah, It gave me big troubles. Actually, I can’t make it work, but I got the logic and the explanation of what it has to do, so I still working on it. I search on the Internet and I saw some blogposts and other pages to get the result but it didn’t work.

The WSQ12 consists in make a program create that asks the user for a word which will be your search word and the name of a file to open and search for that word. Then create a function that will receive two parameters (both string) representing those two data points. This function returns the number of occurrences of that word in that file.

Well, I think that maybe I got the wrong libraries or something in my code is wrong but, this is what I have.

wsq12

 

Check the code and help me to fix it😀

https://github.com/eduardomrlsg/TC101/blob/master/WSQ12%20WordCount

Regards, EdMo.

WSQ12 (The Good One)

This page can help you to understand this assignment.

We needed to create a program that asks the user for a word which will be your search word and the name of a file to open and search for that word. Then create a function that will receive two parameters (both string) representing those two data points. This function returns the number of occurrences of that word in that file.

Take a look at this:

Captura de pantalla 2016-04-08 a las 11.59.12Captura de pantalla 2016-04-08 a las 12.38.25

First things first. You’ll need these libraries: iostream, string, fstream.

In the int main, you need to declare word, tipo and line as Strings. And the count, you can call it if you want it, but you don’t have to if you don’t wanna”.
And you ask for the word you will look for, and save it into “word”.
And then it asks for the name of the file, save it on tipo.

Well for the find_word() function you declare word and tipo. You equal the count to zero.
The ifstream my file.tipo will look for the file. If the code finds it, the file will open.
The you need to use a while loop (not do-while), with the parameter of getline(myline, line) that will read line by line the file. And if the line equals the word, the count will be count++. This will be ir until the file is done with lines. Then you need to close the file with myfile.close().
In case you couldn’t find it, the else should say “No se encontró” or whatever you want.

The function must return the total count.

Here’s my GitHub link to this code.

wsq12

#Contador de palabras

 

def contador(p):

return text.count(p) #cuenta las veces que aparece la palabra(p) en el texto

 

palabra = input(“Ingrese la palabra a buscar: “).lower() #lo convierte en minusculas

texto=input(“Cuál es el nombre del archivo?: “)

 

with open(texto, “rt”) as Txt:

text = Txt.read().lower() #abre el archivo y convierte todo en minusculas

 

print (“la palabra”, palabra, “se repite estas veces: “,(contador(palabra)))

WSQ12 Word count

This code is not really hard. It’s about asking the user for a word and count how many times does it appear in a .txt file.

Let’s start.

I’ll explain the code that Ken helped me with and then I’ll explain my code.

To count words in a text file you need to consider several things:

The word might have lower and upper case letters on it, or it could be next to other word without any space, so the “split” function won’t help a lot.

So here’s Ken’s code:

wsq1202

As you can see, our big_string has the word “bananas”, but not al the “bananas” have the same structure. So first of all. we shall convert every word to lowercase (line 3).

To give more flexibility to the program, we convert the letters of BaNana to lowercase.

Then, python has this beautiful function that will find the word that we’re looking for (line 7).

After that, what we’ll do is look for the word checking each character, and while the string is not empty, the program will check it and find all the bananas. Kind of funny, right? loved this exercise.

Aaaaand here we go. Here’s my code:

wsq1203

Check that I added an input to catch the wanted word. Also, as my file has several lines, it’s obvious that I need to evaluate them all. That’s why I inserted the “for” loop (line 5)

That’s all for today, fellows. Have a nice day.

Word, word, word…

image

(Credit goes to http://www.metamorphoselit.com/2016/02/7-submission-slush-sins-revealed.html)

This WSQ was about create a program that asks the user for a word which will be your search word and the name of a file to open and search for that word. Then create a function that will receive two parameters (both string) representing those two data points. This function returns the number of occurrences of that word in that file.

I have help from my students I really  liked the post of a classmate you can find it here.

And my code in GitHub.

Reading from file

#TC1017 #WSQ12

This was quite a cool and rather long problem. I feel quite happy about it, only case sensitivity is still an issue. But other than that it is quite comprehensive, I mean, it is user friendly and allows him to correct any input mistakes by checking whether the desired file was found or not.

To accomplish this many special functions were needed, such as .open, ifstream, .is_open, and.eof. But when I understood how these worked, the rest of the code was very intuitive to finsih.

wsq12.PNG

Source Code below: [GitHub Link: https://github.com/diegodamy/WSQ12 %5D

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int CheckFile(string textname3){
ifstream InList;
InList.open(textname3.c_str());

if (InList.is_open()) {
cout << “File was opened succesfully!” << endl;
return 1;
} else {
return 0;
}
}

int ReadFile(string word2, string textname2){
int counter;
string textword;
ifstream InList;
InList.open(textname2.c_str());

while (!InList.eof()) { // keep counting until you rach the last string
InList >> textword; // create a string out of all words in the text file
if (textword == word2) {
counter++;
}
}

InList.close();
return counter;
}
int KeepRunning(char decision){
if (decision == ‘Y’ || decision == ‘y’){
return 1;
} else {
return 0;
}
}

int main(){
string word;
string textname;
char decision;

do {
cout <<“Please enter the name of the text:” << endl;
cin >> textname;

if (CheckFile(textname)==0) {
cout << “Unable to open file.” << endl;
cout <<“Please enter the name of the text:” << endl;
cin >> textname;
}

cout <<“Please enter the word you wish to find in the text:” << endl;
cin >> word;

cout << endl << word << ” was found ” << ReadFile(word, textname) << ” time(s) in the text.” << endl;
cout <<

Continue reading “Reading from file”

WSQ12, Contador de palabras.

WSQ12
Contador de palabras, este rpograma encuentra la palabra que teclees, y te dice cuantas veces esta escrita en un editor de texto

#Contador de palabras

 

def contador(p):

return text.count(p) #cuenta las veces que aparece la palabra(p) en el texto

 

palabra = input(“Ingrese la palabra a buscar: “).lower() #lo convierte en minusculas

texto=input(“Cuál es el nombre del archivo?: “)

 

with open(texto, “rt”) as Txt:

text = Txt.read().lower() #abre el archivo y convierte todo en minusculas

 

print (“la palabra”, palabra, “se repite estas veces: “,(contador(palabra)))