Войти

Показать полную графическую версию : [решено] Похожие слова в input


morgan1991
15-04-2019, 12:55
Здравствуйте!
Нужно реализовать подсказки в поле ввода с похожими словами.
Т.е. допустим пользователь ввел предложение в поле ввода и нажал сохранить. Скрипт должен сохранить данные и одновременно как то использовать введенный текст. Например разбить на слова и внести в базу новые слова. А когда пользователь повторно начнет вводить данные в поле ввода, то скрипт будет искать слова в БД и выдавать подсказки, на которую можно нажать, что бы не вводить слово целиком. Как в excel.
Помогите хотя бы набросать алгоритм действий, остальное сам напишу. Единственное, у меня проблема с регулярными выражениями. Все никак не могу освоить...

Iska
16-04-2019, 01:14
В чём именно проблема?

morgan1991
16-04-2019, 07:03
Не могу продумать алгоритм, что бы получить максимальное быстродействие. Где хранить базу. И как вывести подсказку.

morgan1991
19-04-2019, 14:43
Что никто не знает? Хотя бы блок схему составить.

Creat0R
22-04-2019, 22:27
Как то так:

#include (http://www.autoitscript.com/autoit3/docs/keywords.htm##include) <GuiComboBox.au3>
#include (http://www.autoitscript.com/autoit3/docs/keywords.htm##include) <GUIConstantsEx.au3>
#include (http://www.autoitscript.com/autoit3/docs/keywords.htm##include) <WindowsConstants.au3>

Global (http://www.autoitscript.com/autoit3/docs/keywords.htm#Global) $sDB_File = @ScriptDir (http://www.autoitscript.com/autoit3/docs/macros.htm#@DesktopDir) & '\DB.dat'
Global (http://www.autoitscript.com/autoit3/docs/keywords.htm#Global) $aDB = StringSplit (http://www.autoitscript.com/autoit3/docs/functions/StringSplit.htm)(StringStripCR (http://www.autoitscript.com/autoit3/docs/functions/StringStripCR.htm)(FileRead (http://www.autoitscript.com/autoit3/docs/functions/FileRead.htm)($sDB_File)), @LF (http://www.autoitscript.com/autoit3/docs/macros.htm#@LF))

GUICreate (http://www.autoitscript.com/autoit3/docs/functions/GUICreate.htm)('DB Auto Complete Example', 400, 300)
$iCombo = GUICtrlCreateCombo (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateCombo.htm)('', 20, 20, 360, 50)
$hCombo = GUICtrlGetHandle (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlGetHandle.htm)($iCombo)

_GUICtrlComboBox_BeginUpdate (http://www.autoitscript.com/autoit3/docs/libfunctions/_GUICtrlComboBox_BeginUpdate.htm)($hCombo)

For (http://www.autoitscript.com/autoit3/docs/keywords.htm#For) $i = 1 To (http://www.autoitscript.com/autoit3/docs/keywords.htm#To) $aDB[0]
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $aDB[$i] Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
_GUICtrlComboBox_AddString (http://www.autoitscript.com/autoit3/docs/libfunctions/_GUICtrlComboBox_AddString.htm)($hCombo, $aDB[$i])
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
Next (http://www.autoitscript.com/autoit3/docs/keywords.htm#Next)

_GUICtrlComboBox_EndUpdate (http://www.autoitscript.com/autoit3/docs/libfunctions/_GUICtrlComboBox_EndUpdate.htm)($hCombo)

$iSave_Bttn = GUICtrlCreateButton (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateButton.htm)('Save', 20, 270, 70, 20)
$iDel_Bttn = GUICtrlCreateButton (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateButton.htm)('Delete', 100, 270, 70, 20)

GUIRegisterMsg (http://www.autoitscript.com/autoit3/docs/functions/GUIRegisterMsg.htm)($WM_COMMAND, 'WM_COMMAND')
GUISetState (http://www.autoitscript.com/autoit3/docs/functions/GUISetState.htm)(@SW_SHOW (http://www.autoitscript.com/autoit3/docs/macros.htm#@SW_SHOW))

While (http://www.autoitscript.com/autoit3/docs/keywords.htm#While) 1
Switch (http://www.autoitscript.com/autoit3/docs/keywords.htm#Switch) GUIGetMsg (http://www.autoitscript.com/autoit3/docs/functions/GUIGetMsg.htm)()
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $GUI_EVENT_CLOSE
Exit (http://www.autoitscript.com/autoit3/docs/keywords.htm#Exit)
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $iSave_Bttn
_DB_Add(GUICtrlRead (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlRead.htm)($iCombo), False (http://www.autoitscript.com/autoit3/docs/keywords.htm#False))
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $iDel_Bttn
_DB_Delete(GUICtrlRead (http://www.autoitscript.com/autoit3/docs/functions/GUICtrlRead.htm)($iCombo))
EndSwitch (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndSwitch)
WEnd (http://www.autoitscript.com/autoit3/docs/keywords.htm#WEnd)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _DB_Delete($sText)
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) Not (http://www.autoitscript.com/autoit3/docs/keywords.htm#Not) $sText Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)

_GUICtrlComboBox_ResetContent (http://www.autoitscript.com/autoit3/docs/libfunctions/_GUICtrlComboBox_ResetContent.htm)($hCombo)

Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $aTmp[$aDB[0] + 1]
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $hFile = FileOpen (http://www.autoitscript.com/autoit3/docs/functions/FileOpen.htm)($sDB_File, 2)

For (http://www.autoitscript.com/autoit3/docs/keywords.htm#For) $i = 1 To (http://www.autoitscript.com/autoit3/docs/keywords.htm#To) $aDB[0]
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $aDB[$i] And (http://www.autoitscript.com/autoit3/docs/keywords.htm#And) $aDB[$i] <> $sText Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
$aTmp[0] += 1
$aTmp[$aTmp[0]] = $aDB[$i]

FileWriteLine (http://www.autoitscript.com/autoit3/docs/functions/FileWriteLine.htm)($hFile, $aDB[$i])
_GUICtrlComboBox_AddString (http://www.autoitscript.com/autoit3/docs/libfunctions/_GUICtrlComboBox_AddString.htm)($hCombo, $aDB[$i])
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
Next (http://www.autoitscript.com/autoit3/docs/keywords.htm#Next)

FileClose (http://www.autoitscript.com/autoit3/docs/functions/FileClose.htm)($hFile)

ReDim (http://www.autoitscript.com/autoit3/docs/keywords.htm#ReDim) $aTmp[$aTmp[0] + 1]
$aDB = $aTmp
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _DB_Add($sText, $fDuplicate = False (http://www.autoitscript.com/autoit3/docs/keywords.htm#False))
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) Not (http://www.autoitscript.com/autoit3/docs/keywords.htm#Not) $sText Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)

If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) Not (http://www.autoitscript.com/autoit3/docs/keywords.htm#Not) $fDuplicate Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
For (http://www.autoitscript.com/autoit3/docs/keywords.htm#For) $i = 1 To (http://www.autoitscript.com/autoit3/docs/keywords.htm#To) $aDB[0]
If (http://www.autoitscript.com/autoit3/docs/keywords.htm#If) $aDB[$i] = $sText Then (http://www.autoitscript.com/autoit3/docs/keywords.htm#Then)
Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)
Next (http://www.autoitscript.com/autoit3/docs/keywords.htm#Next)
EndIf (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndIf)

FileWriteLine (http://www.autoitscript.com/autoit3/docs/functions/FileWriteLine.htm)($sDB_File, $sText)
_GUICtrlComboBox_AddString (http://www.autoitscript.com/autoit3/docs/libfunctions/_GUICtrlComboBox_AddString.htm)($hCombo, $sText)
_GUICtrlComboBox_SetEditText (http://www.autoitscript.com/autoit3/docs/libfunctions/_GUICtrlComboBox_SetEditText.htm)($hCombo, '')

$aDB[0] += 1
ReDim (http://www.autoitscript.com/autoit3/docs/keywords.htm#ReDim) $aDB[$aDB[0] + 1]
$aDB[$aDB[0]] = $sText
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) _Edit_Changed()
_GUICtrlComboBox_AutoComplete (http://www.autoitscript.com/autoit3/docs/libfunctions/_GUICtrlComboBox_AutoComplete.htm)($iCombo)
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

Func (http://www.autoitscript.com/autoit3/docs/keywords.htm#Func) WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg

Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $hWndCombo = $hCombo
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $hWndFrom = $lParam

Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $iIDFrom = BitAND (http://www.autoitscript.com/autoit3/docs/functions/BitAND.htm)($wParam, 0xFFFF) ; Low Word
Local (http://www.autoitscript.com/autoit3/docs/keywords.htm#Local) $iCode = BitShift (http://www.autoitscript.com/autoit3/docs/functions/BitShift.htm)($wParam, 16) ; Hi Word

Switch (http://www.autoitscript.com/autoit3/docs/keywords.htm#Switch) $hWndFrom
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $iCombo, $hWndCombo
Switch (http://www.autoitscript.com/autoit3/docs/keywords.htm#Switch) $iCode
Case (http://www.autoitscript.com/autoit3/docs/keywords.htm#Case) $CBN_EDITCHANGE
_Edit_Changed()
EndSwitch (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndSwitch)
EndSwitch (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndSwitch)

Return (http://www.autoitscript.com/autoit3/docs/keywords.htm#Return) $GUI_RUNDEFMSG
EndFunc (http://www.autoitscript.com/autoit3/docs/keywords.htm#EndFunc)

morgan1991
29-04-2019, 13:07
Creat0R, все классно. Спасибо. Единственное можно ли заменить GUICtrlCreateCombo на GUICtrlCreateInput?

Creat0R
29-04-2019, 13:23
можно ли заменить »
Зачем?

morgan1991
29-04-2019, 13:26
Нашел на просторах:
; http://www.autoitscript.com/forum/topic/158070-autocomplete-input-text/

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <ListBoxConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

Global $Words1[20] = ["fight", "first", "fly", "third", "fire", "wall", "hi", "hello", "world", "window", _
"window 1", "window 2", "window 3", "window 4", "window 5", "window 6", "window 7", "window 8", "window 9", "window 10"]
Global $Words2[6] = ["Alain", "Aline", "Bernard", "Beatrice", "Chloe", "Caroline"]

Global $hGUI, $hList
Global $sChosen, $idCurInput, $sCurrInput = "", $hListGUI = -1

$hGUI = GUICreate("AutoComplete Input Text", 300, 200)
GUICtrlCreateLabel('lettres "w, f" ', 10, 10, 280, 20)
$hInput = GUICtrlCreateInput("", 10, 40, 280, 20)
GUICtrlCreateLabel('lettres "a, b, c" ', 10, 70, 280, 20)
$hInput2 = GUICtrlCreateInput("", 10, 100, 280, 20)
GUISetState(@SW_SHOW, $hGUI)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
WEnd



Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
Local $IdFrom = BitAnd($wParam, 0x0000FFFF)
Local $iCode = BitShift($wParam, 16)
Switch $IdFrom
Case $hInput, $hInput2
Switch $iCode
Case $EN_UPDATE
$idCurInput = $IdFrom
_Update($idCurInput)
EndSwitch
Case $hList
_Update($idCurInput)
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc


Func _Update($_input)
If GUICtrlRead($_Input) <> $sCurrInput Then
$sCurrInput = GUICtrlRead($_Input)
If $hListGUI <> -1 Then ; List is visible.
GUIDelete($hListGUI)
$hListGUI = -1
EndIf

Local $_array
Switch $_input
Case $hInput
$_array = $Words1
Case $hInput2
$_array = $Words2
EndSwitch

$hList = _PopupSelector($hGUI, $hListGUI, $_Input, _CheckInputText($_Input, $_array))
EndIf

If $hList <> -1 Then $sChosen = GUICtrlRead($hList)
If $sChosen <> "" Then
GuiCtrlSetData($_Input, $sChosen)
$sCurrInput = GUICtrlRead($_Input)
GUIDelete($hListGUI)
$hListGUI = -1
$sChosen = ""
EndIf
EndFunc


Func _PopupSelector($hMainGUI, ByRef $hListGUI, $_Input, $sCurr_List)
Local $hList = -1
If $sCurr_List = "" Then Return $hList

Local $pos = ControlGetPos($hMainGUI, "", $_Input)
$hListGUI = GUICreate("", 280, 160, $pos[0], $pos[1]+$pos[3], $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST, $WS_EX_MDICHILD), $hMainGUI)
$hList = GUICtrlCreateList("", 0, 0, 280, 150, BitOR(0x00100000, 0x00200000))

StringReplace($sCurr_List, "|", "|")
Local $iCurrHeight = @extended*GUICtrlSendMsg($hList, $LB_GETITEMHEIGHT, 0, 0)+10
WinMove($hListGUI, "", Default, Default, Default, $iCurrHeight)
GUICtrlSetPos($hList, 0, 0, 150, $iCurrHeight)

GUICtrlSetData($hList, $sCurr_List)
GUISetControlsVisible($hListGUI) ; To Make Control Visible And Window Invisible.
GUISetState(@SW_SHOWNOACTIVATE, $hListGUI)
Return $hList
EndFunc ;==>_PopupSelector


Func _CheckInputText($_Input, $array)
Local $sPartialData = ""
$aSelected = _GetSelectionPointers($_Input)
If (IsArray($aSelected)) And ($aSelected[0] <= $aSelected[1]) Then
$sCurrInput = GUICtrlRead($_Input)
Local $aSplit = StringSplit(StringLeft($sCurrInput, $aSelected[0]), " ")
$aSelected[0] -= StringLen($aSplit[$aSplit[0]])
If $aSplit[$aSplit[0]] <> "" Then
For $A = 0 To UBound($array)-1
If StringLeft($array[$A], StringLen($aSplit[$aSplit[0]])) = $aSplit[$aSplit[0]] _
And $array[$A] <> $aSplit[$aSplit[0]] Then
$sPartialData &= $array[$A] & "|"
EndIf
Next
EndIf
EndIf
Return $sPartialData
EndFunc ;==>_CheckInputText


Func _GetSelectionPointers($hEdit)
Local $aReturn[2] = [0, 0]
Local $aSelected = GUICtrlRecvMsg($hEdit, 0x00B0) ; $EM_GETSEL.
If IsArray($aSelected) Then
$aReturn[0] = $aSelected[0]
$aReturn[1] = $aSelected[1]
EndIf
Return $aReturn
EndFunc ;==>_GetSelectionPointers


Func GUISetControlsVisible($hWnd) ; By Melba23.
Local $aControlGetPos = 0, $hCreateRect = 0, $hRectRgn = _WinAPI_CreateRectRgn(0, 0, 0, 0)
Local $iLastControlID = _WinAPI_GetDlgCtrlID(GUICtrlGetHandle(-1))
For $i = 3 To $iLastControlID
$aControlGetPos = ControlGetPos($hWnd, '', $i)
If IsArray($aControlGetPos) = 0 Then ContinueLoop
$hCreateRect = _WinAPI_CreateRectRgn($aControlGetPos[0], $aControlGetPos[1], $aControlGetPos[0] + $aControlGetPos[2], $aControlGetPos[1] + $aControlGetPos[3])
_WinAPI_CombineRgn($hRectRgn, $hCreateRect, $hRectRgn, 2)
_WinAPI_DeleteObject($hCreateRect)
Next
_WinAPI_SetWindowRgn($hWnd, $hRectRgn, True)
_WinAPI_DeleteObject($hRectRgn)
EndFunc ;==>GUISetControlsVisible

Зачем? »
Не охота переделывать готовый код под GUICtrlCreateCombo.
Теперь будет два варианта, может кому пригодится.




© OSzone.net 2001-2012