Sign in to follow this  
Followers 0
face

extract all words from text files

12 posts in this topic

how can i extract words from text files and save them as a list. 

program gets access to the folder that contains the text files (more than 2 text files)

reads all the text files contents

ignores special characters and numbers

extracts all words (no duplicates)

saves all extracted words in a .txt file as a list

textfilefolder.rar

Share this post


Link to post
Share on other sites



words are sepearated by spaces, even made up words count

Share this post


Link to post
Share on other sites

Adapt this to your context:

#include <File.au3>
#include <Array.au3>

Local $oDictionary = ObjCreate("Scripting.Dictionary")
Local $mypath = @ScriptDir
Local $aFiles = _FileListToArray($mypath, "*.txt", 1, 1)
Local $aWords

For $i = 1 To $aFiles[0]
    $aWords = StringRegExp(FileRead($aFiles[$i]), "\w+", 3)     ; change pattern to fit your definition of "word"
    For $Word In $aWords
        $oDictionary.ADD($Word, $Word)
    Next
Next

$aWords = $oDictionary.Items
FileWrite("words.txt", _ArrayToString($aWords, @CRLF))
ConsoleWrite($oDictionary.Count & " words found in " & $aFiles[0] & " files." & @LF)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

 

Adapt this to your context:

#include <File.au3>
#include <Array.au3>

Local $oDictionary = ObjCreate("Scripting.Dictionary")
Local $mypath = @ScriptDir
Local $aFiles = _FileListToArray($mypath, "*.txt", 1, 1)
Local $aWords

For $i = 1 To $aFiles[0]
    $aWords = StringRegExp(FileRead($aFiles[$i]), "\w+", 3)     ; change pattern to fit your definition of "word"
    For $Word In $aWords
        $oDictionary.ADD($Word, $Word)
    Next
Next

$aWords = $oDictionary.Items
FileWrite("words.txt", _ArrayToString($aWords, @CRLF))
ConsoleWrite($oDictionary.Count & " words found in " & $aFiles[0] & " files." & @LF)

 

theres 1 error

i cant fix it

post-82531-0-70404500-1388403891_thumb.p

Edited by face

Share this post


Link to post
Share on other sites

face,

That syntax uses the new v3.3.10.# version of the function. If you have not yet updated (which from the error would seem to be the case) then change the line to read: :)

Local $aFiles = _FileListToArray($mypath, "*.txt", 1)
M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

i updated autoit version

now i get another error

post-82531-0-24012200-1388423943_thumb.p

 

Share this post


Link to post
Share on other sites

As you can see I omitted every error checking. Most probably no file match your search.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

face,

Then we need to add some errorchecking code to see where the problem lies: :)

#include <File.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>

Local $oDictionary = ObjCreate("Scripting.Dictionary")
Local $mypath = @ScriptDir
Local $aFiles = _FileListToArray($mypath, "*.au3", 1, 1)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Error", "No files found")
    Exit
Else
    MsgBox($MB_SYSTEMMODAL, "Found", $aFiles[0] & " files")
EndIf

Local $aWords
For $i = 1 To $aFiles[0]
    $aWords = StringRegExp(FileRead($aFiles[$i]), "\w+", 3)     ; change pattern to fit your definition of "word"
    Local $iError = @error
    If $iError = 0 Then
        For $Word In $aWords
            $oDictionary.ADD($Word, $Word)
        Next
    Else
        MsgBox($MB_SYSTEMMODAL, "Error", $aFiles[$i] & " - " & $i & @CRLF & "error: " & $iError)
    EndIf
Next

$aWords = $oDictionary.Items
FileWrite("words.txt", _ArrayToString($aWords, @CRLF))
What error MsgBoxes do you get - if any? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

thank you so much, it works

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Might want to look into only skipping spaces...(grabs special chars, that way...but then again, might not be what you expect...example: would return appostrophes )

$aWords = StringRegExp(FileRead($aFiles[$i]), "[^\s]+", 3)

or only including non spaces...

$aWords = StringRegExp(FileRead($aFiles[$i]), "\S+", 3)
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0